按钮触发双击而不是单击

问题描述:

我有视图动画在Xcode中完美地工作(用CGRect按钮单击滑下屏幕)。不幸的是,我必须单击按钮两次才能执行动画。我做错了什么?我明白任何反馈按钮触发双击而不是单击

这里是我的.H代码

@interface AnimationBlocksViewController : UIViewController{ 
    IBOutlet UIView *theview; 
    IBOutlet UIView *theview2; 

    BOOL isAnimated; 
    BOOL switchback; 
} 

-(IBAction)animate:(id)sender; 
-(IBAction)change:(id)sender; 

这里是我的.M码

-(IBAction)animate:(id)sender;{ 
    if (isAnimated) { 

     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; 
     [UIView setAnimationDuration:0.5]; 
     [theview setFrame:CGRectMake(0, 0, 320, 460)]; 
     [theview2 setFrame:CGRectMake(320, 0, 320, 460)]; 
     [UIView commitAnimations]; 
     isAnimated=NO; 

    } 


    else{ 

     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; 
     [UIView setAnimationDuration:0.5]; 
     [theview setFrame:CGRectMake(-320, 0, 320, 460)]; 
     [theview2 setFrame:CGRectMake(0, 0, 320, 460)]; 
     [UIView commitAnimations]; 
     isAnimated=YES; 

    } 
} 

-(IBAction)change:(id)sender;{ 
    if (switchback) { 

     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; 
     [UIView setAnimationDuration:0.5]; 
     [theview2 setFrame:CGRectMake(0, 0, 320, 460)]; 
     [UIView commitAnimations]; 
     switchback=NO; 

    } 


    else{ 

     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; 
     [UIView setAnimationDuration:0.5]; 
     [theview2 setFrame:CGRectMake(320, 0, 320, 460)]; 
     [theview setFrame:CGRectMake(0, 0, 320, 460)]; 
     [UIView commitAnimations]; 
     switchback=YES; 

    } 
} 

它的工作原理,但我需要双击来触发动画,我做了什么错? 谢谢

你最初双击后,做一个后续点击触发动画是否正常?如果是这样,可能是因为您没有明确初始化变量BOOL,但它们应该默认为NO。尝试这样做,看看它是否有帮助。

如果你总是需要做两次水龙头动画,也许尝试插入一个调用运行循环,看看它是否只处理动画后,你的应用程序线程上运行了一段时间,即它可能是,只有当它来处理第二个点击事件是否实际发送并获取任何待处理的动画并执行它们。该代码是(你设置isAnimated并调用commitAnimations后):

[[NSRunLoop mainRunLoop] runMode:NSRunLoopCommonModes beforeDate:[NSDate date]]; 

不知道你是否可以只使用nil的日期,但尝试,太多,如果你喜欢。

+0

嗯让我改变这些动画状态,看看会发生什么。该按钮目前仅通过双击动画观看 –

+0

YES!有效!更改我的按钮的动画状态解决了问题,谢谢darvidsOn –

+0

(我将所有动画状态更改为NO –