I’ve managed to make these views push each other off the screen with a button click.
However when the device is changed to landscape orientation, the view only pushes half way off the screen and sticks. I understand why this happens but not how to fix it.
Is there a way to make the view slide entirely off the screen on a button click in both portrait and orientation mode? That’s all I need it to do.
My code reads like this
.h file
@interface AnimationBlocksViewController : UIViewController{
IBOutlet UIView *theview;
IBOutlet UIView *theview2;
BOOL isAnimated;
BOOL switchback;
}
-(IBAction)animate:(id)sender;
-(IBAction)change:(id)sender;
@end
.m file
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
isAnimated = NO;
}
-(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=YES;
}
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=NO;
}
}
-(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=NO;
}
}
I appreciate any feedback, samples, or links to similar questions or tutorials.
Thank you
Rather than rely on these constant values you might simplify this behavior by setting the coordinates of
theviewandtheview2based on the current bounds of your view controller’s view ie[theview2 setFrame:CGRectMake(self.view.bounds.size.width, 0, 320, 460)];. That way you don’t need to rely on keeping track of fixed sizes (which will change if for example your app is run while a user is on a call, if you later choose to hide the status bar, or if you put this view controller inside a navigation or tab bar controller).Going further we could base the sizes of these views entirely on the bounds of the view controller’s view:
That way you not only make sure to move the view completely outside it’s parent view but can also make sure that your subviews fill their parent when you move them back (because you are setting their width and height equal to that parent view’s width and height).