首先让我们了解下什么是 Core Animation,Core Animation 为核心动画,他为图形渲染和动画提供了基础。使用核心动画,我们只需要设置起点、终点、关键帧等一些参数,剩下的工作核心动画会自动帮我们处理。(学过 Flash 的朋友会有种似曾相识的感觉)

核心动画开发动画的本质是将 CALayer 中的内容转换为位图从而供硬件操作,他不用消耗 CPU 资源,合理使用他能提高 App 性能。

核心动画的几大核心类:

CAAnimation「核心动画基类」:不能直接使用,他遵循并实现了 CAMediaTiming 协议,负责动画运行时间和速度的控制。

CAPropertyAnimation「属性动画基类」:不能直接使用,他通过可动画属性进行动画设置。

CAAnimationGroup「动画组」:他是一种组合模式,可以组合多个动画进行动画行为的统一控制。

CATransition「转场动画」:主要通过滤镜进行动画效果设置。

CABasicAnimation「基础动画」:通过起点和终点状态属性参数进行动画控制。

CAKeyframeAnimation「关键帧动画」:同 CABasicAnimation 一样通过属性参数进行动画控制,但不同的是他可以有多个状态控制,不单单只有起点和终点。

CABasicAnimation 和 CAKeyframeAnimation 都属于 CAPropertyAnimation,他们通过修改属性参数产生动画效果。在两个状态点中间过程的动画,可以称为「补间动画」,他由系统自动计算产生。CABasicAnimation 只有起点和终点状态,从某种角度来说,他相当于有两个关键帧的 CAKeyframeAnimation。

下面让我们通过例子,了解如何使用 CAKeyframeAnimation「关键帧动画」、CABasicAnimation「基础动画」、CAAnimationGroup「动画组」吧。

效果如下:

ViewController.h

 #import <UIKit/UIKit.h>

 @interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIImageView *imgVAnimation;
@property (strong, nonatomic) IBOutlet UIButton *btnAnimation1;
@property (strong, nonatomic) IBOutlet UIButton *btnAnimation2; @end

ViewController.m

 #import "ViewController.h"

 @interface ViewController ()
- (void)modifyLayerForButton:(UIButton *)btn;
- (void)layoutUI;
@end @implementation ViewController
#define kCornerRadiusOfImage CGRectGetWidth(_imgVAnimation.frame)/2.0 - (void)viewDidLoad {
[super viewDidLoad]; [self layoutUI];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} - (void)modifyLayerForButton:(UIButton *)btn {
btn.layer.masksToBounds = YES;
btn.layer.cornerRadius = 5.0;
btn.layer.borderColor = [UIColor grayColor].CGColor;
btn.layer.borderWidth = 1.0;
} - (void)layoutUI {
//图片视图
_imgVAnimation.layer.masksToBounds = YES;
_imgVAnimation.layer.cornerRadius = kCornerRadiusOfImage;
_imgVAnimation.layer.borderColor = [UIColor orangeColor].CGColor;
_imgVAnimation.layer.borderWidth = 2.0; //按钮
[self modifyLayerForButton:_btnAnimation1];
[self modifyLayerForButton:_btnAnimation2];
} - (IBAction)btnAnimation1DidPush:(id)sender {
//移到右下角;使用关键帧动画,移动路径为预定的贝塞尔曲线路径
CGPoint fromPoint = _imgVAnimation.center;
CGFloat toPointX = self.view.frame.size.width - kCornerRadiusOfImage;
CGFloat toPointY = self.view.frame.size.height - kCornerRadiusOfImage;
CGPoint toPoint = CGPointMake(toPointX, toPointY);
CGPoint controlPoint = CGPointMake(toPointX, 0.0); UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:fromPoint];
[path addQuadCurveToPoint:toPoint controlPoint:controlPoint]; CAKeyframeAnimation *positionAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
positionAnimation.path = path.CGPath;
positionAnimation.removedOnCompletion = YES; //变小;使用基础动画
CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
transformAnimation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
transformAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]; //设置 X 轴和 Y 轴缩放比例都为1.0,而 Z 轴不变
transformAnimation.removedOnCompletion = YES; //透明;使用基础动画
CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
opacityAnimation.fromValue = [NSNumber numberWithFloat:1.0];
opacityAnimation.toValue = [NSNumber numberWithFloat:0.1];
opacityAnimation.removedOnCompletion = YES; //组合效果;使用动画组
CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
animationGroup.animations = @[ positionAnimation, transformAnimation, opacityAnimation ];
animationGroup.duration = 1.0; //设置动画执行时间;这里设置为1.0秒
animationGroup.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]; //设置媒体调速运动;默认为 kCAMediaTimingFunctionLinear,即为线型间隔;这里设置为 kCAMediaTimingFunctionEaseIn,即先慢后快,相当于有个加速度
animationGroup.autoreverses = YES; //设置自动倒退,即动画回放;默认值为NO
[_imgVAnimation.layer addAnimation:animationGroup forKey:nil];
} - (IBAction)btnAnimation2DidPush:(id)sender {
//向右移动;使用关键帧动画,移动路径为预定的直线路径
CGPoint fromPoint = _imgVAnimation.center;
CGPoint toPoint = CGPointMake(fromPoint.x + 100.0, fromPoint.y); UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:fromPoint];
[path addLineToPoint:toPoint]; CAKeyframeAnimation *positionAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
positionAnimation.path = path.CGPath;
positionAnimation.removedOnCompletion = YES; //旋转;使用基础动画
CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
transformAnimation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
transformAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_2, 0.0, 0.0, 1.0)]; //设置沿着 Z 轴顺时针旋转90度;注意 CATransform3DMakeRotation 总是按最短路径来选择,当顺时针和逆时针的路径相同时(e.g. M_PI),会使用逆时针
transformAnimation.repeatCount = 8.0; //设置动画播放重复次数;这里设置为8.0次,共720度
transformAnimation.duration = 0.5; //设置动画执行时间;这里设置为0.5秒
transformAnimation.cumulative = YES; //设置是否累积;默认值为NO,这里设置为YES,看起来才动画效果连贯
transformAnimation.removedOnCompletion = YES; //组合效果;使用动画组
CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
animationGroup.animations = @[ positionAnimation, transformAnimation ];
animationGroup.duration = 4.0; //设置动画执行时间;这里设置为4.0秒
animationGroup.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]; //设置媒体调速运动;默认为 kCAMediaTimingFunctionLinear,即为线型间隔;这里设置为 kCAMediaTimingFunctionEaseIn,即先慢后快,相当于有个加速度
animationGroup.autoreverses = YES; //设置自动倒退,即动画回放;默认值为NO //以下两句是『动画结束后回到初始状态的现象』的解决方法;这里没用到
//animationGroup.removedOnCompletion = NO; //设置是否完成后从对应的所属图层移除他,默认为YES
//animationGroup.fillMode = kCAFillModeForwards; //设置动画填充模式;默认值为 kCAFillModeRemoved,即动画执行完就移除,变回原来的状态,这里设置为 kCAFillModeForwards,即保持向前的状态
[_imgVAnimation.layer addAnimation:animationGroup forKey:nil];
} @end

Main.storyboard

 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="7706" systemVersion="14E46" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="vXZ-lx-hvc">
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7703"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="ufC-wZ-h7g">
<objects>
<viewController id="vXZ-lx-hvc" customClass="ViewController" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="jyV-Pf-zRb"/>
<viewControllerLayoutGuide type="bottom" id="2fi-mo-0CV"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="kh9-bI-dsS">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" image="Emoticon_tusiji_icon2" translatesAutoresizingMaskIntoConstraints="NO" id="j2r-O5-Hj2">
<rect key="frame" x="20" y="40" width="150" height="150"/>
</imageView>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="aiO-kP-xCF">
<rect key="frame" x="20" y="243" width="150" height="50"/>
<state key="normal" title="移到右下角变小透明">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
<action selector="btnAnimation1DidPush:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="7Z2-yc-1vS"/>
</connections>
</button>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="图片操作:" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="YH5-Oi-KEH">
<rect key="frame" x="20" y="208" width="150" height="21"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="hSL-o5-Ism">
<rect key="frame" x="20" y="311" width="150" height="50"/>
<state key="normal" title="旋转并向右移动">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
<action selector="btnAnimation2DidPush:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="lC4-zx-uIb"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
</view>
<connections>
<outlet property="btnAnimation1" destination="aiO-kP-xCF" id="kSp-82-S2R"/>
<outlet property="btnAnimation2" destination="hSL-o5-Ism" id="6Mz-Wd-xfN"/>
<outlet property="imgVAnimation" destination="j2r-O5-Hj2" id="Gmp-iW-kaX"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="x5A-6p-PRh" sceneMemberID="firstResponder"/>
</objects>
</scene>
</scenes>
<resources>
<image name="Emoticon_tusiji_icon2" width="150" height="150"/>
</resources>
</document>

Core Animation 动画的使用:关键帧动画、基础动画、动画组的更多相关文章

  1. Core Animation 文档翻译 (第八篇)—提高动画的性能

    前言 核心动画是提高基于APP动画帧率的好方式,但是核心动画的使用不代表性能的提升的保证.尤其在OSX,当使用核心动画时,我们仍需选择最有效的方式.和所有的性能相关的问题一样,我们应该使用工具时时的评 ...

  2. 动画基础--基于Core Animation(1)

    1.简介 上一篇文章[New learn]动画-基于UIView了解到了一些直接由UIView这个在UIKIT提供的类中提供的一些动画方法. 使用UIView的动画特性已经能够满足我们很多的需求,它是 ...

  3. 使用Core Animation对象来实现动画

    转载保留原文地址:http://blog.csdn.net/kqjob/article/details/10417461,转载的 在iOS中如果使用普通的动画则可以使用UIKit提供的动画方式来实现, ...

  4. 简析iOS动画原理及实现——Core Animation

    本文转载至 http://www.tuicool.com/articles/e2qaYjA 原文  https://tech.imdada.cn/2016/06/21/ios-core-animati ...

  5. Core Animation学习总结

    文件夹: The Layer Beneath The Layer Tree(图层树) The Backing Image(寄宿层) Layer Geometry(图层几何学) Visual Effec ...

  6. iOS——Core Animation 知识摘抄(四)

    原文地址http://www.cocoachina.com/ios/20150106/10840.html 延迟解压 一旦图片文件被加载就必须要进行解码,解码过程是一个相当复杂的任务,需要消耗非常长的 ...

  7. 说说Core Animation

    前言 本次分享将从以下方面进行展开: 曾被面试官问倒过的问题:层与视图的关系 CALayer类介绍及层与视图的关系 CAShapeLayer类介绍 UIBezierPath贝塞尔曲线讲解 CoreAn ...

  8. iOS Core Animation之CALayer心得

    使用CALayer的mask实现注水动画效果 Core Animation一直是iOS比较有意思的一个主题,使用Core Animation可以实现非常平滑的炫酷动画.Core animtion的AP ...

  9. 转 iOS Core Animation 动画 入门学习(一)基础

    iOS Core Animation 动画 入门学习(一)基础 reference:https://developer.apple.com/library/ios/documentation/Coco ...

随机推荐

  1. 部分手机(如三星)的Listview列表会自动加上黑线解决办法

    部分手机(如三星)的Listview列表会自动加上黑线,这里将其去掉部分手机(如三星)的列表会自动加上黑线. 因为三星手机会自动加上分割线. // 部分手机(如三星C9 Pro)的设置项列表会自动加上 ...

  2. PHPMailer 使用方法(支持群发):

    一.下载函数包 地址:https://github.com/PHPMailer/PHPMailer 二.测试服务器环境 通过phpinfo()函数打印出是否支持Sockets(Socket属于php的 ...

  3. JDBC(6)—BeanUtils

    1.简介: BeanUtils工具是一种方便我们对JavaBean进行操作的工具,是Apache组织下的产品 2.Java 类的属性: 1>.在JavaEE中java类的属性通过getter和s ...

  4. Android 编程下 WebView 加载一个网页如何得到网页的 Cookie 值

    http://www.cnblogs.com/sunzn/archive/2013/04/03/2998113.html mWebView.setWebViewClient(new MyWebView ...

  5. Java 语法糖详解

    语法糖 语法糖(Syntactic Sugar),也称糖衣语法,是由英国计算机学家 Peter.J.Landin 发明的一个术语,指在计算机语言中添加的某种语法. 这种语法对语言的功能并没有影响,但是 ...

  6. 一篇文章让你学透Linux系统中的more命令

    Linux 下有很多实用工具可以让你在终端界面查看文本文件.其中一个就是 more. more 跟我之前另一篇文章里写到的工具 —— less 很相似.它们之间的主要不同点在于 more 只允许你向前 ...

  7. MDX Cookbook 12 - 计算 SMA 简单移动平均 LastPeriods() 函数的使用

    先认识一下这几个名词 Moving Average (MA) 移动平均,或者叫做移动平均线,是技术分析中一种分析时间序列数据的工具.最常见的就是利用股价,回报或交易量等变数计算出移动平均.可以利用移动 ...

  8. LeetCode Permutations问题详解

    题目一 permutations 题目描述 Given a collection of numbers, return all possible permutations. For example,[ ...

  9. Backbone hello world

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta http ...

  10. C#中Post请求的两种方式发送参数链和Body的

    POST请求 有两种方式 一种是组装key=value这种参数对的方式 一种是直接把一个字符串发送过去 作为body的方式 我们在postman中可以看到 sfdsafd sdfsdfds publi ...