美文网首页
iOS核心动画介绍

iOS核心动画介绍

作者: 恋空K | 来源:发表于2022-12-06 12:07 被阅读0次


   基础动画
    //1.创建动画对象
    CABasicAnimation *anim  = [CABasicAnimation animation];
    //2.设置动画属性.
    anim.keyPath = @"position.y";
    anim.toValue = @(400);
    
    //动画完成时会自动删除动画
    anim.removedOnCompletion = NO;
    //动画完成时保持什么状态
    anim.fillMode = kCAFillModeForwards;
    
    //添加动画
    [self.redView.layer addAnimation:anim forKey:@"anim1"];
上述代码修改看起来了改变了view的位置,但是实际view还是在原来的位置,只是操作了view里面的图层layer
    心跳效果
    //1.创建动画对象
    CABasicAnimation *anim = [CABasicAnimation animation];
    //2.设置动画属性
    anim.keyPath = @"transform.scale";
    anim.toValue = @0;
    
    //设置执行次数
    anim.repeatCount = HUGE;
    anim.duration = 0.5;
    
    //自动返转(怎么去,怎么回来)
    anim.autoreverses = YES;
    
    //3.添加动画
    [self.imageView.layer addAnimation:anim forKey:@"scaleAnim"];
删除app时左右摆动的动画
#define  angleToRadio(angle) ((angle) * M_PI / 180.0)
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    //可以根据路径做动画
    CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
    
    anim.keyPath = @"transform.rotation";
    //进行多个值之间的动画
    anim.values = @[@(angleToRadio(-5)),@(angleToRadio(5)),@(angleToRadio(-5))];
    anim.repeatCount = MAXFLOAT;
    
    //anim.autoreverses = YES;

    anim.duration = 0.1;
   
    //添加动画
    [self.iconImageView.layer addAnimation:anim forKey:nil];
}
CABasicAnimation可以看做是一种CAKeyframeAnimation的简单动画,因为它只有头尾的关键帧(
keyframe)。也就是说CABasicAnimation可看做是最多只有2个关键帧的CAKeyframeAnimation
路径动画
#import "ViewController.h"

@interface ViewController ()

@property (nonatomic ,weak) CALayer *fistLayer;

@property (strong, nonatomic)  NSMutableArray *imageArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置背景
    self.view.layer.contents = (id)[UIImage imageNamed:@"bg"].CGImage;
    
    CALayer *fistLayer = [CALayer layer];
    fistLayer.frame = CGRectMake(100, 288, 89, 40);
    //fistLayer.backgroundColor = [UIColor redColor].CGColor;
    [self.view.layer addSublayer:fistLayer];
    self.fistLayer = fistLayer;
    
    //加载图片
    NSMutableArray *imageArray = [NSMutableArray array];
    for (int i = 0; i < 10; i++) {
       UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"fish%d",i]];
        [imageArray addObject:image];
    }
    self.imageArray = imageArray;
    //添加定时器
    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(update) userInfo:nil repeats:YES];
    
    //添加核心动画
    CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
    anim.keyPath = @"position";
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(100, 288)];
    [path addLineToPoint:CGPointMake(100, 100)];
    [path addLineToPoint:CGPointMake(300, 100)];
    [path addQuadCurveToPoint:CGPointMake(300, 600) controlPoint:CGPointMake(400, 600)];
    [path addLineToPoint:CGPointMake(100, 288)];
    
    //设置路径
    anim.path = path.CGPath;
    anim.duration = 5.0;
    anim.repeatCount = HUGE;
    //根据路径,自动旋转
    anim.rotationMode = @"autoReverse";
    
    //设置时间计算模型
    anim.calculationMode = @"cubic";
    
    [self.fistLayer addAnimation:anim forKey:@"moveAnim"];
}

static int _imageIndex = 0;
- (void)update {
    //从数组当中取出图片
    UIImage *image = self.imageArray[_imageIndex];
    self.fistLayer.contents = (id)image.CGImage;
    _imageIndex++;
    if (_imageIndex > 9) {
        _imageIndex = 0;
    }
}

@end
CALayer的contents属性:即要在layer上展示的内容,必须是CGImage属性,否则显示空白。
self.exampleView.layer.contents = image.CGImage
理论上来讲, 既然是 id 类型, 那么应该可以接收任何类型的对象, 但实际上并不是如此, 实际上是, 如果我们
给 contents 赋的不是 CGImage, 那么图层将会是空白的

相关文章

网友评论

      本文标题:iOS核心动画介绍

      本文链接:https://www.haomeiwen.com/subject/czuvfdtx.html