动画


#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController

@end




#import "SecondViewController.h"

@interface SecondViewController ()
{
   
    
    
}
@property (nonatomic, retain) UIView *animationView;

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"CALayer";
    //CALayer, 继承于NSObject;
    //UIView 和 CALayer 的关系:
    //1. UIView可以响应事件, CALayer不可以;
    //2. UIView侧重于内容的管理, CALayer侧重于内容的绘制
    //3. 两者紧密联系, 缺一不可;
    //4. 总之一句话, 发之于肤, 血之于肉, 灵之于魂,
    
    //UIView自带一个readly的属性layer(CALayer), 大小和UIView的大小一致;
    
    self.animationView = [[UIView alloc] initWithFrame:CGRectMake(100, 300, 100, 100)];
    self.animationView.backgroundColor = [UIColor redColor];
    //比较重要layer属性
    //设置圆角弧度;
//    self.animationView.layer.cornerRadius = 50;
    //设置描边:
    self.animationView.layer.borderWidth = 3;
    //设置颜色;
    self.animationView.layer.borderColor = [UIColor greenColor].CGColor;
    //设置偏移:
    self.animationView.layer.shadowOffset = CGSizeMake(10, 100);
    //设置阴影颜色:
    self.animationView.layer.shadowColor = [UIColor blueColor].CGColor;
    //设置阴影透明度;(默认0)
    self.animationView.layer.shadowOpacity = 1;
    
    
    
    
    //位置:锚点距superLayer原点的距离
    self.animationView.layer.position = CGPointMake(150, 300);
    
    //锚点; 默认规定, 一个视图的左上角为(0, 0), 右下角为(1, 1), 锚点默认值为(0.5, 0.5), 与视图的中心点重合, 锚点关系着视图做变型(比如:旋转)的基准点;
    self.animationView.layer.anchorPoint = CGPointMake(0, 0);
    
    //锚点和position, 两者任意一个发生变化, 不影响另外一个, 并且为了保证另一个不变, 要改变view的frame位置
    
    [self.view addSubview:self.animationView];
    [self.animationView release];
    
    
    
    
    
    
    
    // CAAnimation: 作用于CALayer层的动画
    // CAPropertyAnimation, 属性动画, 是抽象子类;
    // CABasieAnimation, 基本动画
    // CAKeyframeAnimation
    
    UIButton *basicButton = [UIButton buttonWithType:UIButtonTypeSystem];
    basicButton.frame = CGRectMake(0, 300, 375, 50);
    basicButton.titleLabel.font = [UIFont systemFontOfSize:30];
    [basicButton setTitle:@"CABasicAnimation" forState:UIControlStateNormal];
    [basicButton addTarget:self action:@selector(basic) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:basicButton];
    
    UIButton *keyButton = [UIButton buttonWithType:UIButtonTypeSystem];
    keyButton.frame = CGRectMake(0, 150, 375, 50);
    keyButton.titleLabel.font = [UIFont systemFontOfSize:30];
    [keyButton setTitle:@"CAKeyAnimation" forState:UIControlStateNormal];
    [keyButton addTarget:self action:@selector(keyFrame) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:keyButton];
     
    UIButton *transitionButton = [UIButton buttonWithType:UIButtonTypeSystem];
    transitionButton.frame = CGRectMake(0, 450, 375, 50);
    transitionButton.titleLabel.font = [UIFont systemFontOfSize:30];
    [transitionButton setTitle:@"CATranAnimation" forState:UIControlStateNormal];
    [transitionButton addTarget:self action:@selector(transition) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:transitionButton];
    
    
    UIButton *groupButton = [UIButton buttonWithType:UIButtonTypeSystem];
    groupButton.frame = CGRectMake(0, 550, 375, 50);
    groupButton.titleLabel.font = [UIFont systemFontOfSize:30];
    [groupButton setTitle:@"CAGroupAnimation" forState:UIControlStateNormal];
    [groupButton addTarget:self action:@selector(group) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:groupButton];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (void)basic
{
    //CAAnimation没有真实改变属性值, 只是在模拟属性值改变,
    //KeyPath, 写属性的名字;(属性的属性)或路径
//    CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"cornerRadius"];
       CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"bounds.size"];
    //设置时长
//    basic.duration = 10;
    //设置
    basic.speed = 0.1;
    
//    basic.fromValue = @10;
//    basic.toValue = @200;
    
//    basic.fromValue = [NSValue valueWithCGSize:CGSizeMake(10, 20)];
//    basic.toValue = [NSValue valueWithCGSize:CGSizeMake(100, 100)];
    
    basic.fromValue = @[@10, @10];
    basic.toValue = @[@100, @100];
    basic.delegate = self;
    
    [self.animationView.layer addAnimation:basic forKey:@"aa"];
}

- (void)keyFrame
{
    //CAKeyframeAnimation  关键帧动画
    CAKeyframeAnimation *keyframe = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    
    //keyframe.values
    //keyframe.keyTimes
    //设置时间;
    keyframe.duration = 5;
    keyframe.values = @[[NSValue valueWithCGPoint:CGPointMake(0, 0)],
                        [NSValue valueWithCGPoint:CGPointMake(10, 10)],
                        [NSValue valueWithCGPoint:CGPointMake(40, 40)],
                        [NSValue valueWithCGPoint:CGPointMake(160, 160)],
                        [NSValue valueWithCGPoint:CGPointMake(370, 558)]];
    
    //keyTimes,数组中的值不能够大于1, 并且必须是递增的;
    keyframe.keyTimes = @[@0.1, @0.5, @0.7, @0.8, @1];
    [self.animationView.layer addAnimation:keyframe forKey:@"bb"];
}

- (void)transition
{
//    CATransition,过渡动画;
     CATransition *transition = [CATransition animation];
    
//    transition.type
//    transition.subtype
    transition.type =
    kCATransitionMoveIn;
//    kCATransitionFade;
//    kCATransitionPush;
//    kCATransitionReveal;
    transition.subtype =
//    kCATransitionFromTop;
//    kCATransitionFromRight;
//    kCATransitionFromLeft;
    kCATransitionFromBottom;
    
    [self.animationView.layer addAnimation:transition forKey:@"cc"];
}

- (void)group
{
//    CAAnimationGroup ,动画组
    CAAnimationGroup *group = [CAAnimationGroup animation];
//    group.animations = @[basic, transition, keyframe];
    [self.animationView.layer addAnimation:group forKey:@"dd"];
}
//-(void)animationDidStart:(CAAnimation *)anim
//{
//    NSLog(@"%%@", )
//}
@end


原文地址:https://www.cnblogs.com/chushenruhua/p/4314263.html