UISwitch双击

3

我目前有一个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 --;
    }
}

最后,如果滑块处于某个位置时会发生什么的结果:

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

有没有一种方法可以确保开关在返回之前完全从开启状态转换为关闭状态,或者有更好的方法来解决这个问题?

1个回答

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

这将仅在开关状态实际更改时执行。

谢谢wsidell,完美解决了问题,现在滑块的功能正常了...哇呜! - SmokersCough

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接