UIButton没有在第一次使用时工作

时间:2013-01-17 07:54:27

标签: iphone ios objective-c xcode

我制作了一个自定义开关来打开和关闭我的游戏Optic Combat中的声音效果,但由于某种原因,按下时按钮,动画开关移动到不同的位置,在第一次点击时不起作用。随后的水龙头工作,而不是第一个。

在按钮的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"];
}

2 个答案:

答案 0 :(得分: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中它是不同的。如果buttonAction正确,则在viewDidLoad中互换。

答案 1 :(得分: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
}

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