核心動畫——彈簧動畫二,核心動畫彈簧

來源:互聯網
上載者:User

核心動畫——彈簧動畫二,核心動畫彈簧

核心動畫——彈簧動畫一主要介紹彈簧動畫的一些屬性,下面介紹一下彈簧動畫的另一種效果。

首先在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的時候它會變顏色,並且彈跳。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.