Core animation-spring Animation 2, core animation spring

Source: Internet
Author: User

Core animation-spring Animation 2, core animation spring

Spring animation, the core animation, mainly introduces some attributes of spring animation. Next we will introduce another effect of spring animation.

Create a UIButton in the Main. storyboard file. ViewController inherits from ViewController. Set UIButton as an attribute, right-click UIButton, and drag it to the @ interface of the ViewController. m file, and name it. At the same time, give it a trigger event and drag it to @ implementation. For specific operations, see the code:

# Import "ViewController. h "@ interface ViewController () @ property (weak, nonatomic) IBOutlet UIButton * annimationButton; @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad];}-(IBAction) action :( id) sender {UIButton * button = sender; button. selected =! Button. selected; button. backgroundColor = button. selected! = YES? [UIColor colorWithRed: 1.000 green: 0.435 blue: 0.812 alpha: 1.000]: [UIColor colorWithRed: 0.400 green: 0.800 blue: 1.000 alpha: 1.000]; [self jump];} -(void) jump {CASpringAnimation * animation = [CASpringAnimation animationWithKeyPath: @ "bounds"]; animation. toValue = [NSValue valueWithCGRect: CGRectMake (0, 0, self. annimationButton. frame. size. width * 1.5, self. annimationButton. frame. size. height * 1.5)]; animation. mass = 2; animation. stiffness = 100; animation. damping = 3; animation. initialVelocity = 30; animation. duration = animation. settlingDuration; [self. annimationButton. layer addAnimation: animation forKey: @ "jump"];}-(void) move :( CGPoint) toPoint {CABasicAnimation * basicAnimation = [CABasicAnimation placement: @ "position"]; basicAnimation. toValue = [NSValue valueWithCGPoint: toPoint]; basicAnimation. duration = 3; basicAnimation. removedOnCompletion = NO; basicAnimation. fillMode = kCAFillModeBoth; [self. annimationButton. layer addAnimation: basicAnimation forKey: @ "move"];}-(void) touchesBegan :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event {[self move: [[touches anyObject] locationInView: self. view]; NSLog (@ "center point x: % f y: % f", self. annimationButton. center. x, self. annimationButton. center. y); NSLog (@ "center point x: % f y: % f", self. annimationButton. layer. position. x, self. annimationButton. layer. position. y); // CAAnimation only changes the animation effect of the layer, and does not actually change the attribute values of the view and layer} @ end

The effect is that it will change color and bounce when you click the button.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.