我目前有一个UISwitch,当打开和关闭时,分别递增和递减一个计数器。
当计数器为0时,计数器不会递减。在功能上,这是完美的工作,但我注意到一个bug,并想知道是否有人经历过这一点。
基本上,如果你非常快速地双击UISwitch的远端位置(完全打开或关闭),计数器将递增两次,因为我想象UISwitch没有完全进入关闭状态,因此只是简单地再次添加到计数器,而不是第一次递减它。
下面是我用来检查交换机的代码:
// Sliders modified
- (IBAction)personalityChanged:(id)sender {
if ([personality isOn] ){
[[[GlobalData sharedGlobalData]personalitySliderValue] replaceObjectAtIndex:currentRecord-1 withObject:@"1"];
rating ++;
NSLog(@"The value of personality slider is %@", [[[GlobalData sharedGlobalData]personalitySliderValue] objectAtIndex:currentRecord-1]);
[personality set]
}
else {
[[[GlobalData sharedGlobalData]personalitySliderValue] replaceObjectAtIndex:currentRecord-1 withObject:@"0"];
[self subtractFromRating:nil];
NSLog(@"The value of personality slider is %@", [[[GlobalData sharedGlobalData]personalitySliderValue] objectAtIndex:currentRecord-1]);
}
[self checkRating:nil];
}然后减去评级:
// subtract from rating
-(void)subtractFromRating:(id)sender{
if (rating == 0) {
// do nothing
}
else
{
rating --;
}
}最后,如果滑块位于某个位置,会发生什么情况:
// check rating
-(void)checkRating:(id)sender{
switch (rating) {
case 0:
[matchRating setText:@""];
[ratingGraphic setImage:[UIImage imageNamed:@""]];
NSLog(@"rating is 0");
break;
case 1:
[matchRating setText:@"Single Match"];
[ratingGraphic setImage:[UIImage imageNamed:@"ratinggraphic1.png"]];
NSLog(@"rating is 1");
break;
case 2:
[matchRating setText:@"Potential Match"];
[ratingGraphic setImage:[UIImage imageNamed:@"ratinggraphic2.png"]];
NSLog(@"rating is 2");
break;
case 3:
[matchRating setText:@"Great Match"];
[ratingGraphic setImage:[UIImage imageNamed:@"ratinggraphic3.png"]];
NSLog(@"rating is 3");
break;
case 4:
[matchRating setText:@"Hot Match"];
[ratingGraphic setImage:[UIImage imageNamed:@"ratinggraphic4.png"]];
NSLog(@"rating is 4");
break;
default:
break;
}
}有没有一种方法可以确保开关在返回之前完全从on状态转换到off状态,或者是更好的方法?
发布于 2012-10-18 09:05:52
检测是否确实发生了变化的一个解决方案是保留一个额外的BOOL变量,用于跟踪最后一个开关状态。
BOOL lastValue = NO; // initial switch state
- (IBAction)personalityChanged:(id)sender {
if (personality.isOn != lastValue) {
lastValue = personality.isOn;
if ([personality isOn] ){
[[[GlobalData sharedGlobalData]personalitySliderValue] replaceObjectAtIndex:currentRecord-1 withObject:@"1"];
rating ++;
NSLog(@"The value of personality slider is %@", [[[GlobalData sharedGlobalData]personalitySliderValue] objectAtIndex:currentRecord-1]);
[personality set]
}
else {
[[[GlobalData sharedGlobalData]personalitySliderValue] replaceObjectAtIndex:currentRecord-1 withObject:@"0"];
[self subtractFromRating:nil];
NSLog(@"The value of personality slider is %@", [[[GlobalData sharedGlobalData]personalitySliderValue] objectAtIndex:currentRecord-1]);
}
[self checkRating:nil];
}
}然后,这将仅在开关状态实际发生变化时执行。
https://stackoverflow.com/questions/12943062
复制相似问题