核心動畫——彈簧動畫二,核心動畫彈簧
核心動畫——彈簧動畫一主要介紹彈簧動畫的一些屬性,下面介紹一下彈簧動畫的另一種效果。
首先在Main.storyboard檔案裡面建立一個UIButton,ViewController繼承於ViewController。將UIButton設定為一個屬性,選中UIButton右擊不放手拖到ViewController.m檔案的@interface 裡面並給它起名。同時,給它一個觸發事件,將它拖到@implementation裡面。具體操作看代碼:
#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 animationWithKeyPath:@"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(@"button改變位置之前的中心點x:%f y:%f",self.annimationButton.center.x,self.annimationButton.center.y); NSLog(@"button上面Layer的中心點x:%f y:%f",self.annimationButton.layer.position.x,self.annimationButton.layer.position.y); //CAAnimation 只是改變圖層的動畫效果,並沒有真實的改變視圖、圖層的屬性值 }@end
它的效果就是點擊button的時候它會變顏色,並且彈跳。