2012-01-14 65 views
0

我有一组UIButtons和UILabels,我希望在淡入淡出时选择一个按钮并且它是正确的按钮。UIButtons不会在动画块中淡出

我已经尝试了一些东西(在代码块中对它们进行了评论),并且只有UILabel会淡出。我在这里错过了什么?

-(IBAction)answerButtonPressed:(UIButton *)sender { 
NSLog(@"Game Question Answer Pressed: %i", sender.tag); 
NSLog(@"%@", sender.titleLabel.text); 

int selectedAnswer =0; 
selectedAnswer = [question.answer intValue]; 

if (selectedAnswer == sender.tag) { 
    NSLog(@"GameQ %@ is the correct answer", sender.titleLabel.text); 

    //self.toggleView; 

    [labelA setAlpha:0]; 
    [labelB setAlpha:0]; 
    [labelC setAlpha:0]; 
    [labelD setAlpha:0]; 


    /* 
    [buttonA setAlpha:0]; 
    [buttonB setAlpha:0]; 
    [buttonC setAlpha:0]; 
    [buttonD setAlpha:0]; 

    [buttonA setHidden:YES]; 
    [buttonB setHidden:YES]; 
    [buttonC setHidden:YES]; 
    [buttonD setHidden:YES]; 
    */   
    [sender setAlpha:1]; 
    [sender setHidden:NO]; 

    [[UIApplication sharedApplication] beginIgnoringInteractionEvents]; 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:1.5]; 

    [UIView animateWithDuration:2.0 
          delay:0.0 
         options: UIViewAnimationCurveEaseOut 
        animations:^{buttonA.alpha = 0;} 
        completion:nil]; 

    [UIView setAnimationDelegate:[UIApplication sharedApplication]]; 
    [UIView setAnimationDidStopSelector:@selector(endIgnoringInteractionEvents)]; 

    [UIView commitAnimations]; 

我已经清理了这个方法,只使用了一种类型的动画块。 UIButton仍然不会淡出,但标签确实如此。这里是我作为动画块的东西:

 [UIView animateWithDuration:2.0 
          delay:0.0 
         options: UIViewAnimationCurveEaseOut 
        animations:^{buttonA.alpha = 0;} 
        completion:nil]; 

    [UIView animateWithDuration:2.0 
          delay:0.0 
         options: UIViewAnimationCurveEaseOut 
        animations:^{labelA.alpha = 0;} 
        completion:nil]; 

回答

9

我把几件事情注意到了蝙蝠。您正在结合两种类型的动画技巧。新老:

要么是:

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:1.5]; 

buttonA.alpha = 0; 

[UIView commitAnimations]; 

或(和首选,因为它是“现代”的方式)

[UIView animateWithDuration:2.0 
         delay:0.0 
        options: UIViewAnimationCurveEaseOut 
       animations:^{buttonA.alpha = 0;} 
       completion:nil]; 

结合使用这两种,因为你有可能会导致因为第一个动画技术插入了第二个块,所以要被多次调用。基本上,将许多动画放在队列中,将按钮淡入淡出速度非常快。

此外,默认情况下(至少使用该块)默认情况下禁用用户交互。不需要明确地做。

+0

我已经清理了这个方法,并采取了更多“现代”方法的建议。仍然按钮仍然存在,但标签像以前一样消失。东西一定是过度的?请在最初的帖子中查看我的编辑。 – Nungster 2012-01-16 02:34:30

+0

你不需要多块你想改变的一切。你可以把你的labelA.alpha = 0;和你的buttonA.alpha = 0;在同一个块中。你确定你已经将buttonA连接到界面生成器中的按钮实例吗?即现在是零吗? – MarkPowell 2012-01-16 02:50:00

+0

大声笑我是个白痴。是的,因为我使用ID发送者传递方法,所以我不需要连接我的ref插座。现在我需要传达信息,我检查了一下,笔尖没有连线。 – Nungster 2012-01-16 03:33:41