Xcode 按钮在双击而不是单击时触发
我用CGRect点击一个按钮,就可以在Xcode中完美地播放视图动画。不幸的是,我必须点击两次按钮来执行动画。我做错了什么?我感谢您的反馈 这是我的.H代码Xcode 按钮在双击而不是单击时触发,xcode,Xcode,我用CGRect点击一个按钮,就可以在Xcode中完美地播放视图动画。不幸的是,我必须点击两次按钮来执行动画。我做错了什么?我感谢您的反馈 这是我的.H代码 @interface AnimationBlocksViewController : UIViewController{ IBOutlet UIView *theview; IBOutlet UIView *theview2; BOOL isAnimated; BOOL switchback; } -(I
@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后,将显示该操作的代码:
不确定是否可以使用nil进行约会,但如果愿意,也可以尝试一下。Hmm让我更改这些动画状态,看看会发生什么。该按钮当前仅在双击“是”时设置视图动画!成功了!更改按钮的动画状态修复了问题,谢谢darvidsOnI将所有动画状态更改为NO
[[NSRunLoop mainRunLoop] runMode:NSRunLoopCommonModes beforeDate:[NSDate date]];