48.核心动画之基础动画

来源:互联网 发布:免费远程监控软件 编辑:程序博客网 时间:2024/06/03 18:26

Core Animation的动画执行过程都是在后台操作的,不会阻塞主线程。
要注意的是,Core Animation是直接作用在CALayer上的,并非UIView。
keyPath:通过指定CALayer的一个属性名称为keyPath(NSString类型),并且对CALayer的这个属性的值进行修改,达到相应的动画效果。比如,指定@”position”为keyPath,就修改CALayer的position属性的值,以达到平移的动画效果

1.平移:- (void)test{    // 1. 创建核心动画    CABasicAnimation *anima = [CABasicAnimation animation] ;    // 1.1告诉系统要执行什么样的动画    anima.keyPath = @"position";    // 设置通过动画将layer从哪    anima.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, 0)];    // 到哪(到指定的位置)    anima.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 300)];    //在当前位置的基础上增加多少    //anima.byValue = [NSValue valueWithCGPoint:CGPointMake(0, 300)];    // 设置动画时间    anima.duration = 5;    // 1.2 设置动画执行完毕之后不删除动画    anima.removedOnCompletion = NO;    // 1.3 设置保存动画的最新状态    anima.fillMode = kCAFillModeForwards;    // 2.添加核心动画到Layer    [self.myLayer addAnimation:anima forKey:nil];}
2.缩放:- (void)test1{    // 1. 创建核心动画    CABasicAnimation *anima = [CABasicAnimation animation] ;    // 1.1设置动画类型    anima.keyPath = @"bounds";    // 1.2 设置动画执行完毕之后不删除动画    anima.removedOnCompletion = NO;    // 1.3 设置保存动画的最新状态    anima.fillMode = kCAFillModeForwards;    // 1.4设置动画时间    anima.duration = 1;    // 1.5修改动画    anima.toValue =[NSValue valueWithCGRect: CGRectMake(0, 0, 200, 200)];    // 2.添加核心动画到Layer    [self.myLayer addAnimation:anima forKey:nil];}
3.旋转:- (void)test2{    // 1. 创建核心动画    CABasicAnimation *anima = [CABasicAnimation animation] ;    // 1.1设置动画类型    anima.keyPath = @"transform";    // 1.2 设置动画执行完毕之后不删除动画    anima.removedOnCompletion = NO;    // 1.3 设置保存动画的最新状态    anima.fillMode = kCAFillModeForwards;    // 1.4设置动画时间    anima.duration = 1;    // 1.5修改动画    anima.toValue =  [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_4, 0, 0, 1)];    // 2.添加核心动画到Layer    [self.myLayer addAnimation:anima forKey:nil];}
4.transform动画:- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{    // 1. 创建核心动画    CABasicAnimation *anima = [CABasicAnimation animation] ;    // 1.1设置动画类型    //anima.keyPath = @"transform.translation.x";    anima.keyPath = @"transform.scale.y";    // 1.2 设置动画执行完毕之后不删除动画    anima.removedOnCompletion = NO;    // 1.3 设置保存动画的最新状态    anima.fillMode = kCAFillModeForwards;    // 1.4设置动画时间    anima.duration = 1;    // 1.5如何动画    //anima.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeTranslation(0, 100, 1)];    //anima.toValue = @(100);    anima.toValue = @(1.5);    // 2.添加核心动画到Layer    [self.myLayer addAnimation:anima forKey:nil];}
0 0