2011-03-15 135 views
0

我在按钮上使用动画首次显示视图,第二次隐藏视图。 这里是我的隐藏视图简单动画问题

-(IBAction)clickme 
{ 
[UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.3]; 

    [UIView setAnimationCurve:UIViewAnimationCurveLinear]; 
    [UIView setAnimationDelegate:self]; 


    [view1 setAlpha:0.0]; 
[UIView commitAnimations]; 
} 

类似的代码是有显示的视图代码。

但是,当用户多次单击按钮时会出现问题....意味着我使用了2秒的动画,但如果用户在动画过程中按下相同的按钮,则输出结果非常糟糕。

我不想在动画期间禁用该按钮。

有没有其他办法?

回答

2

您需要跟踪是否有动画正在进行,如果是,请忽略点击。

在类标题中声明实例变量BOOL animating;,并在init中将其初始化为NO

然后,

-(IBAction)clickme 
{ 
    if (animating) return; 
    animating = YES; 

[UIView beginAnimations:nil context:self]; 
    [UIView setAnimationDuration:0.3]; 

    [UIView setAnimationCurve:UIViewAnimationCurveLinear]; 
    [UIView setAnimationDelegate:self]; 

    [view1 setAlpha:0.0]; 
[UIView commitAnimations]; 
} 

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context { 
    if (context == self) 
     animating = NO; 
} 
+1

你错过[UIView的setAnimationDidStopSelector:...] ;. ;) – hennes 2011-03-15 13:07:45

+0

@hennes不,我不这么认为,'[UIView setAnimationDelegate:self];'表示这些消息是在没有明确设置选择器的情况下发送的。 – joerick 2011-03-15 17:32:18

+0

你确定吗?我一直认为我需要手动设置这些。那么请原谅我的缺乏经验。 – hennes 2011-03-15 18:25:03

2

尝试使用+(无效)setAnimationBeginsFromCurrentState:(BOOL)fromCurrentState:

-(IBAction)clickme 
{ 
[UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.3]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 

    [UIView setAnimationCurve:UIViewAnimationCurveLinear]; 
    [UIView setAnimationDelegate:self]; 


    [view1 setAlpha:0.0]; 
[UIView commitAnimations]; 
}