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.