实例讲解iOS应用UI开发之基础动画的创建

2020-01-14 17:18:39刘景俊

    anima.toValue=[NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_2+M_PI_4, 1, 1, 0)];
    //1.3设置动画执行完毕后不删除动画
    anima.removedOnCompletion=NO;
    //1.4设置保存动画的最新状态
    anima.fillMode=kCAFillModeForwards;
    
    //2.添加动画到layer
    [self.myLayer addAnimation:anima forKey:nil];
}
@end


实现效果:

 

实例讲解iOS应用UI开发之基础动画的创建

补充:

可以通过transform(KVC)的方式来进行设置。

代码示例(平移):

复制代码
#import "YYViewController.h"

 

@interface YYViewController ()
@property(nonatomic,strong)CALayer *myLayer;
@end


复制代码
@implementation YYViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //创建layer
    CALayer *myLayer=[CALayer layer];
    //设置layer的属性
    myLayer.bounds=CGRectMake(0, 0, 150, 60);
    myLayer.backgroundColor=[UIColor yellowColor].CGColor;
    myLayer.position=CGPointMake(50, 50);
    myLayer.anchorPoint=CGPointMake(0, 0);
    myLayer.cornerRadius=40;
    //添加layer
    [self.view.layer addSublayer:myLayer];
    self.myLayer=myLayer;
}

 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //1.创建动画
    CABasicAnimation *anima=[CABasicAnimation animation];
    anima.keyPath=@"transform";
    //1.1设置动画执行时间
    anima.duration=2.0;
    //1.2修改属性,执行动画
  
    anima.toValue=[NSValue valueWithCATransform3D:CATransform3DMakeTranslation(0, 100, 1)];