iOS Developer - AnimationiOS Developer

iOS之CoreAnimation(基础动画)笔记

2016-08-19  本文已影响28人  我是三人禾

在iOS中CALayer的设计主要是了为了内容展示和动画操作,CALayer本身并不包含在UIKit中,它不能响应事件。由于CALayer在设计之初就考虑它的动画操作功能,CALayer很多属性在修改时都能形成动画效果,这种属性称为“隐式动画属性”。但是对于UIView的根图层而言属性的修改并不形成动画效果,因为很多情况下根图层更多的充当容器的做用,如果它的属性变动形成动画效果会直接影响子图层。另外,UIView的根图层创建工作完全由iOS负责完成,无法重新创建,但是可以往根图层中添加子图层或移除子图层

Snip20160819_3.png

以上这些都是支持“隐式动画”

- (void)viewDidLoad {

    [super viewDidLoad];
    //实例化自定义图层
    self.myLayer = [CALayer layer];
    //设置大小
    [self.myLayer setBounds:CGRectMake(100, 100, 100, 100)];
    //设置背景颜色
    [self.myLayer setBackgroundColor:[UIColor redColor].CGColor];
    // 中心点 Position 相当于View.center
    [self.myLayer setPosition:CGPointMake(100, 100)];
    
    self.myLayer.shadowColor = [UIColor  orangeColor].CGColor;
    
    self.myLayer.shadowOffset = CGSizeMake(10, 10);
    
    [self.view.layer addSublayer:self.myLayer];
}

#warning - CALayer 属性不能响应事件

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    UITouch *touch = touches.anyObject;
    
    CGPoint location = [touch locationInView:self.view];

    //位置
    [self.myLayer setPosition:location];

    //尺寸
    NSInteger size = arc4random_uniform(50) + 51;
    
    [self.myLayer setBounds:CGRectMake(0, 0, size, size)];
    //圆角
    NSInteger rotation = arc4random_uniform(30);
    
    [self.myLayer setCornerRadius:rotation];
    
    //旋转角度
    CGFloat angle = arc4random_uniform(180) /180.0 * M_PI;
    
    [self.myLayer setTransform:CATransform3DMakeRotation(angle, 0, 0, 1)];

    self.myLayer.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255.0)/255.0 green:arc4random_uniform(255.0)/255.0 blue:arc4random_uniform(255.0)/255.0 alpha:1].CGColor;
    
}

Snip20160819_7.png

Core Animation是苹果公司给我们提供的一套强大的核心动画类库,而核心动画又分为基础动画、关键帧动画、动画组、转场动画。这里只做 基础动画(CABasicAnimation)的笔记。

    [super viewDidLoad];
    
    self.view.backgroundColor=[UIColor orangeColor];
    
    // 自定义一个图层
    _layer=[[CALayer alloc]init];
    
    _layer.bounds=CGRectMake(0, 0, 30, 30);
    
    _layer.position=CGPointMake(50, 150);
    
    _layer.backgroundColor = [UIColor redColor].CGColor;

    [self.view.layer addSublayer:_layer];
    
    [self createYellowView];
}
- (void)createYellowView {
    
    _yellowView = [[UIView alloc] initWithFrame:CGRectMake(100, 150, 30, 30)];
    
    _yellowView.backgroundColor = [UIColor yellowColor];
    
    [self.view addSubview:_yellowView];
    
}

- (void)yellowRunPoint:(CGPoint)location {
    
    [UIView animateWithDuration:3.0 animations:^{
        
        _yellowView.center = location;
        
    }];
}

#pragma mark 移动动画
-(void)translatonAnimation:(CGPoint)location{
    //1.创建动画并指定动画属性
    CABasicAnimation *basicAnimation=[CABasicAnimation animationWithKeyPath:@"position"];
    
    //2.设置动画属性初始值和结束值
    
    // basicAnimation.fromValue=[NSNumber numberWithInteger:50];//可以不设置,默认为图层初始状态
    basicAnimation.toValue=[NSValue valueWithCGPoint:location];
    
    //设置其他动画属性
    basicAnimation.duration=3.0;//动画时间5秒
    
   // basicAnimation.repeatCount=HUGE_VALF;//设置重复次数,HUGE_VALF可看做无穷大,起到循环动画的效果
    
    //basicAnimation.removedOnCompletion=YES;//运行一次是否移除动画
    
    //3.添加动画到图层,注意key相当于给动画进行命名,以后获得该动画时可以使用此名称获取
    [_layer addAnimation:basicAnimation forKey:@"BasicAnimation_Translation"];
    
    
}
#pragma mark 点击事件
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    UITouch *touch=touches.anyObject;
    
    CGPoint location= [touch locationInView:self.view];
    //创建并开始动画
    [self translatonAnimation:location];
    
    [self yellowRunPoint:location];
    
}

先写这么多,写博客太累。。。

上一篇 下一篇

猜你喜欢

热点阅读