0

我在我的游戏《光学战斗》中制作了一个自定义开关来打开和关闭音效,但由于某种原因,当点击时将动画开关移动到不同位置的按钮在第一次点击时不起作用。随后的水龙头工作,只是不是第一个。

在按钮的 IBAction 中,我有:

- (IBAction)theSwitch:(id)sender {
NSUserDefaults *toggle = [NSUserDefaults standardUserDefaults];

if (_toggle == 1) {

    [UIView animateWithDuration:0.3 animations:^{
        audioToggle.frame = CGRectMake(985.0f, 59.0f, audioToggle.frame.size.width, audioToggle.frame.size.height);
    }];

    _toggle ++;
    [toggle setInteger:_toggle forKey:@"toggleState"];
    [toggle synchronize];
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"audioOn"];

}

else {

    [UIView animateWithDuration:0.3 animations:^{
        audioToggle.frame = CGRectMake(924.0f, 59.0f, audioToggle.frame.size.width, audioToggle.frame.size.height);
    }];

    _toggle --;
    [toggle setInteger:_toggle forKey:@"toggleState"];
    [toggle synchronize];
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"audioOn"];

}
}

在 viewDidLoad 中,检查开关的状态并调整它以显示正确的位置(打开或关闭):

- (void)viewDidLoad
{
[super viewDidLoad];

if ([[NSUserDefaults standardUserDefaults] boolForKey:@"audioOn"]) {
    [audioToggle setFrame:CGRectMake(924.0f, 59.0f, audioToggle.frame.size.width, audioToggle.frame.size.height)];
    _toggle = 2;
}
else {
    [audioToggle setFrame:CGRectMake(985.0f, 59.0f, audioToggle.frame.size.width, audioToggle.frame.size.height)];
    _toggle = 1;
}

编辑:我用它来解决我的问题,在按钮的 IBAction 中:

if ([[NSUserDefaults standardUserDefaults] boolForKey:@"audioOn"]) {
    [UIView animateWithDuration:0.3 animations:^{
    audioToggle.frame = CGRectMake(985.0f, 59.0f, audioToggle.frame.size.width, audioToggle.frame.size.height);
    }];
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"audioOn"];
}
else {
    [UIView animateWithDuration:0.3 animations:^{
    audioToggle.frame = CGRectMake(924.0f, 59.0f, audioToggle.frame.size.width, audioToggle.frame.size.height);
    }];
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"audioOn"];
}
4

2 回答 2

3

在你的viewDidLoad

 if _toggle = 2; 
 frame = CGRectMake(924.0f, 59.0f, audioToggle.frame.size.width, audioToggle.frame.size.height)

if _toggle = 1; 
 frame = CGRectMake(985.0f, 59.0f, audioToggle.frame.size.width, audioToggle.frame.size.height)

但在你的 buttonAction 中它是不同的。viewDidLoad如果 buttonAction 正确,则进行交换。

于 2013-01-17T08:07:58.907 回答
1

我怀疑阿努沙明白了。但我也怀疑如果你选择了一种稍微不同的风格,你自己很容易找到它。切换开关可以打开或关闭,换句话说,与布尔值完美匹配。如果您有一个名为 swicthedOn 的 BOOL,那么您将检查读取 if (switchedOn),这将大大提高可读性。

可能是这样的:

- (IBAction)theSwitch:(id)sender { // Not using the sender for anything (?)
    NSUserDefaults *toggle = [NSUserDefaults standardUserDefaults];
    BOOL audioOn = NO; 
    float toggleX = 924.0f;

    if (_switchIsOn) {
        toggleX = 985.0f;
        audioOn = NO; // not strictly necessary but improves readability       
    }
    else {
        toggleX = 924.0f; // not strictly necessary but improves readability
        audioOn = YES;
    }

    _switchIsOn = audioOn;
    [UIView animateWithDuration:0.3 animations:^{
        audioToggle.frame = CGRectMake(toggleX, 59.0f, audioToggle.frame.size.width, audioToggle.frame.size.height);
    }];

    [toggle setBool:_switchIsOn forKey:@"toggleState"];
    [toggle synchronize];
    [[NSUserDefaults standardUserDefaults] setBool:audioOn forKey:@"audioOn"]; // Not being synchronized
}

是的,我重构了它(并且可能引入了几个新错误)。对不起,我忍不住...

于 2013-01-17T08:30:47.873 回答