I got view animations to work perfectly in Xcode (slide off the screen on a button click with CGRect.) Unfortunately I have to click the button twice to execute the animation. What have I done wrong? I appreciate any feedback
Here’s my .H code
@interface AnimationBlocksViewController : UIViewController{
IBOutlet UIView *theview;
IBOutlet UIView *theview2;
BOOL isAnimated;
BOOL switchback;
}
-(IBAction)animate:(id)sender;
-(IBAction)change:(id)sender;
Here’s my .M code
-(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;
}
}
It works but I have to double click to trigger the animation, what have I done wrong?
Thank you
After your initial double click, does a subsequent single click trigger the animation properly? If so, it may be that you haven’t explicitly initialised your
BOOLvariables, though they should default toNO. Try doing that and see if it helps.If you always need to do two taps to animate, maybe try inserting a call to the run loop to see if it’s only processing the animation after running on your application thread for a while i.e. it could be that only when it comes to process a second click event does it actually go and fetch any pending animations and execute them. The code for that would be (after you set
isAnimatedand callcommitAnimations):Not sure whether you can just use
nilfor a date but try that too if you like.