Core Animation -关键帧动画

简介: Core Animation -关键帧动画

关键帧动画如其名是由Core Animation在每帧之间插入的动画,它不同于隐式动画在动画结束后才执行操作,也不限于起始和结束的值,而是根据一连串随意的值来进行的。它依赖于CAKeyframeAnimation,是UIKit一个没有暴露出来的类,把它运用于前面改变颜色的工程中,会发现颜色是渐变切换的,唯一的问题就是颜色在换的瞬间没有缓冲,后面会说到:

//create a keyframe animation
      CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
      animation.keyPath = @"backgroundColor";
      animation.duration = 2.0;
      animation.values = @[
                           (__bridge id)[UIColor blueColor].CGColor,
                           (__bridge id)[UIColor redColor].CGColor,
                           (__bridge id)[UIColor greenColor].CGColor,
                           (__bridge id)[UIColor blueColor].CGColor ];
      //apply animation to layer
      [self.colorLayer addAnimation:animation forKey:nil];

这里设置颜色的数组并通过关键帧动画来播放。这里起始帧和结束帧设置一样的颜色是因为动画在开始时会直接跳转到第一帧,在结束时如果和启动帧颜色不一致最后会直接跳变回启动帧颜色,为了平滑特性,起始和结束颜色最好设为一致。


提供一个数组就可以按照颜色变化来做动画,但这样并不够直观,所以这里CAKeyframeAnimation引入CGPath,这是一种直观的方式,它是用Core Graphics函数定义运动序列来绘制动画,下面我们用Core Graphics来绘制动画效果:

 //create a path
    UIBezierPath *bezierPath = [[UIBezierPath alloc] init];
    [bezierPath moveToPoint:CGPointMake(100, 150)];
    [bezierPath addCurveToPoint:CGPointMake(300, 300) controlPoint1:CGPointMake(100, 450) controlPoint2:CGPointMake(200, 600)];
    //draw the path using a CAShapeLayer
     CAShapeLayer *pathLayer = [CAShapeLayer layer];
     pathLayer.path = bezierPath.CGPath;
     pathLayer.fillColor = [UIColor clearColor].CGColor;
     pathLayer.strokeColor = [UIColor redColor].CGColor;
     pathLayer.lineWidth = 3.0f;
     [self.view.layer addSublayer:pathLayer];
     //add the ship
     CALayer *shipLayer = [CALayer layer];
     shipLayer.frame = CGRectMake(0, 0, 32, 32);
     shipLayer.position = CGPointMake(100, 150);
     shipLayer.contents = (__bridge id)[UIImage imageNamed: @"mycenter_favorite.png"].CGImage; [self.view.layer addSublayer:shipLayer];
    //create the keyframe animation
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
    animation.keyPath = @"position";
    animation.duration = 4.0;
    animation.path = bezierPath.CGPath;
    //设置朝向沿切线方向
    animation.rotationMode = kCAAnimationRotateAuto;
    [shipLayer addAnimation:animation forKey:nil];

这里有个属性叫做rotationMode,没有这个属性的话,动画的朝向是不变的,加上后动画的朝向将朝向贝赛尔曲线的切线方向,就像开车转弯一样,车头朝着路的方向,而不可能车头朝向不变,车身横移。代码请查看:https://github.com/codeliu6572/CAKeyframeAnimation

目录
相关文章
|
3天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8097 2
|
12天前
|
域名解析 人工智能 Linux
|
20天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
10645 13
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
6天前
|
人工智能 安全 网络安全
|
11天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1605 5
|
10天前
|
云安全 数据采集 人工智能
|
17天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2757 8
|
10天前
|
Web App开发 缓存 安全
巨魔商店 TrollStore 安装教程(2026):免电脑在线安装与全机型安装器矩阵_iOS巨魔安装
巨魔商店(TrollStore)是iOS 14–17.0(特定版本)的永久签名工具,无需越狱,利用CoreTrust漏洞实现IPA签名长期有效,彻底告别7天重签。支持免电脑在线安装(TrollHelperOTA),适配多机型/系统组合,开源免费,安全可控。(239字)
2727 0