码迷,mamicode.com
首页 > 其他好文 > 详细

CABasicAnimation-04-核心动画-day4

时间:2016-04-18 15:31:45      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:

//
//  ViewController.m
//  04-CABasicAnimation
//
//  Created by mac on 16/4/18.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    [self createBasicAnimation];
}
- (void)createBasicAnimation {
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    view.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:view];
    
    //1. 初始化动画对象 : keyPath是在动画过程中,需要改变的值
    CABasicAnimation *basicAnim = [CABasicAnimation animationWithKeyPath:@"position"];
    
    //2. 设置属性改变的值
    basicAnim.fromValue = [NSValue valueWithCGPoint:CGPointZero];
    basicAnim.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 200)];
    
    //3. 持续时间
    basicAnim.duration = 1;
    
    //4. 将动画添加到图层上
    [view.layer addAnimation:basicAnim forKey:@"basic"];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

//    [self transitionAnimation];
    [self scaleAnimation];
}

- (void)transitionAnimation {
    
    //1. 初始化动画对象 : keyPath是在动画过程中,需要改变的值
    CABasicAnimation *basicAnim = [CABasicAnimation animationWithKeyPath:@"position"];
    
    //2. 设置属性改变的值
    basicAnim.fromValue = [NSValue valueWithCGPoint:CGPointZero];
    basicAnim.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 200)];
    basicAnim.byValue = [NSValue valueWithCGPoint:CGPointMake(100, 400)];
    
    //3. 持续时间
    basicAnim.duration = 1;
    //4. 将动画添加到图层上
    [self.imageView.layer addAnimation:basicAnim forKey:@"basic"];
}

- (void)scaleAnimation {
    
    CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    
    basic.fromValue = @1;
    basic.toValue = @5;
    
    basic.duration = 1.0f;
    
    //开启动画回放
    basic.autoreverses = YES;
    
    [self.imageView.layer addAnimation:basic forKey:@"scaleAnimation"];
}

@end

 

 1 /**
 2  *  缩放
 3  */
 4 - (void)scaleAnimation {
 5     
 6     CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
 7     
 8     basic.fromValue = @1;
 9     basic.toValue = @5;
10     
11     basic.duration = 1.0f;
12     
13     //开启动画回放
14     basic.autoreverses = YES;
15     
16     basic.repeatCount = 3;
17     basic.repeatDuration = 6;
18     basic.beginTime = CACurrentMediaTime() + 3;
19     
20     /*       kCAFillModeForwards //在动画结束播放之后,将图层保留结束为止
21              kCAFillModeBackwards //在动画没开始之前,将图层保留在起点位置
22              kCAFillModeBoth //上面两者集合
23              kCAFillModeRemoved //动画结束之后,移除动画效果,图层返回最初状态
24      */
25     basic.removedOnCompletion = YES;
26     basic.fillMode = kCAFillModeBoth;
27     
28     [self.imageView.layer addAnimation:basic forKey:@"scaleAnimation"];
29 }

 

CABasicAnimation-04-核心动画-day4

标签:

原文地址:http://www.cnblogs.com/foreveriOS/p/5404428.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!