簡體   English   中英

UIButton在第一次點擊時不起作用

[英]UIButton not working on the first tap

我在游戲《光學戰斗》中做了一個自定義開關,以打開和關閉聲音效果,但是由於某些原因,在輕按時,將開關移動到另一個位置的動畫按鈕無法使用。 隨后的水龍頭工作,但不是第一個。

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

在你的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互換。

我懷疑Anusha知道了。 但是我也懷疑,如果您選擇了稍有不同的樣式,您自己會很容易找到它。 切換是打開還是關閉,換句話說就是布爾值的完美匹配。 如果您有一個名為swicthedOn的BOOL,則需要檢查是否為(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
}

是的,我對其進行了重構(並可能引入了一些新的錯誤)。 對不起,我沒辦法...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM