gpt4 book ai didi

ios - UISwitch 双击

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:06:18 29 4
gpt4 key购买 nike

我目前有一个 UISwitch,它在打开和关闭时分别递增和递减一个计数器。

当计数器为 0 时,计数器不会递减。从功能上讲,这工作得很好,但是我注意到一个错误,想知道是否有人遇到过这个问题。

本质上,如果你非常非常快地双击 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 --;
}
}

最后是 slider 在某个位置时发生的结果:

// 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;
}
}

有没有办法确保开关在返回之前从开启状态完全变为关闭状态,或者有更好的方法吗?

最佳答案

检测是否确实发生了变化的解决方案是保留一个额外的 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];
}
}

这只会在开关状态实际改变时执行。

关于ios - UISwitch 双击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12943062/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com