Animation : How to make UIbutton move from right to left when view is loaded?
From top to bottom animation
CATransition *animation = [CATransition animation];
[animation setDuration:0.4];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromBottom];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[[TopButton layer] addAnimation:animation forKey:@"SwitchToDown"];
/* Common transition subtypes. */
kCATransitionFromRight
kCATransitionFromLeft
kCATransitionFromTop
kCATransitionFromBottom
Swift:
let transition1: CATransition = CATransition()
let timeFunc1 : CAMediaTimingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
transition1.duration = 1.0
transition1.timingFunction = timeFunc1
transition1.type = kCATransitionPush
transition1.subtype = kCATransitionFromRight
self.yourBtnORViewRef.layer.addAnimation(transition1, forKey: kCATransition)
Objective-C
CATransition *animation = [CATransition animation];
[animation setDuration:1.0];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromRight];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[[self.yourBtnORViewRef layer] addAnimation:animation forKey:@"SwitchToRight"];
Swift 5:
//Animate Your Button From Left to Right
let transition1: CATransition = CATransition()
let timeFunc1 : CAMediaTimingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
transition1.duration = 1.5
transition1.timingFunction = timeFunc1
transition1.type = CATransitionType.push
transition1.subtype = CATransitionSubtype.fromLeft
self.yourBtnRef.layer.add(transition1, forKey: kCATransition)
Just to make it even slicker, I would suggest not changing the UIView's frame but its center;
CGPoint newLeftCenter = CGPointMake( 20.0f + myButton.frame.size.width / 2.0f, myButton.center.y);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0f];
myButton.center = newLeftCenter;
[UIView commitAnimations];
in your viewDidAppear:(BOOL)animated method you can change frame of your button inside animation block. so you'll see what you want. Check documentation about animations in UIView class reference. The simpliest animation block will be like that(sorry, i have no mac nearby right now, so code may not be working just after copy and paste)
[UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:1.0f]; [myButton setFrame:newFrame]; [UIView commitAnimations];