Animation : How to make UIbutton move from right t

2019-01-22 09:01发布

问题:

I have not done any animation stuff with iphone development so far.

Can anybody help me how to make UIbutton move from right to left when view is loaded?

I want to generate effect like that : when view is loaded, the button appears moving from right end to left & stops at its place. Is it possible?

Any help regarding this animation stuff is appreciated. Please help with some sample if available.

Thanks in advance.

回答1:

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];


回答2:

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];


回答3:

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"];


回答4:

/* 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"];