CAAnimation——基本动画,关键帧动画和贝塞尔路径

iOS动画学习笔记 从易到难分为三个方面CABasicAnimation,UIBezierPath,CAShapeLayer 从CABasicAnimation开始 这个部分也有三个概念CALayer、CAAnimation、CAAnimationGroup CALayer 在最开始学习iOS开发很长的一段时间都只在与view对对碰,只会在很少的角落里看到layer的字眼,所以对于layer的首要问题是,他和view有什么关系 来自苹果官方文档 Layers provide infrastructure for your v 阅读详情

概述


在做对于图层的动画效果时,往往直接改变属性或者使用隐式动画是不能满足我们的需求的,所以我们就用到了显式动画,CAAnimation。它可以管理重复动画、准确的控制时间和步调,并且能设定图层过渡。当然,所有隐式动画能做到的,显式动画也都能做到。

来看下CAAnimation的继承体系



CABasicAnimation


- (void)demoViewBasicAnimation
{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
    
    animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(_demoView.center.x, _demoView.center.y)];  //可以省略...
    animation.toValue = [NSValue valueWithCGPoint:CGPointMake(270, 410)];
    animation.duration = 3.0f;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];    //动画速度设置
    
    [_demoView.layer addAnimation:animation forKey:nil];
}

编译运行,会发现动画在执行后demoView回到了起点,这里需要对动画的两个属性进行设置,让图层在完成动画后停留在动画后的状态。

......    

   animation.fillMode = kCAFillModeForwards;
   animation.removedOnCompletion = NO;

......

加入这两行代码后就简单的完成了位移动画

注意这里填写的keyPath是一个NSString类型的字符串,这是系统封装好的键路径,2维常用的主要有 transform opacity position等。

在CAAnimation中可以实现代理方法
- animationDidStart:
- animationDidStop:finished:
分别对应刚启动时的状态和完成后的状态

在addAnimation:forKey:方法中,也可以给这个动画设置一个键,可以在其他地方将其取出来,进行一些操作,比如删除。这也充分体现了kvc的灵活。
用到CALayer的 removeAnimationForKey:方法。

CAKeyframeAnimation


如果是简单的动画CABasicAnimation就能完成,CAKeyframeAnimation(关键帧动画)弥补了基本动画只能传入一对对应值的不足,关键帧动画支持传入一套数值或一个路径来完成动画。

- (void)demoViewKeyframeAnimation
{
    _demoView.layer.anchorPoint = CGPointMake(0.5, 0.0);
    CAKeyframeAnimation *animaiton = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
    
    NSArray *rotationVelues = @[@(M_PI_4), @(-M_PI_4), @(M_PI_4)];
    animaiton.values = rotationVelues;
    animaiton.duration = 3.0f;
    animaiton.repeatCount = HUGE_VALF;      //    #define    HUGE_VALF    1e50f
    
    [_demoView.layer addAnimation:animaiton forKey:nil];
}

编译运行后可以发现demoView像一个钟摆一样不停摇晃,如果想效果更逼真,可以使用连续动画配合timingFunction来实现。

动画添加贝塞尔路径


- (void)demoViewBezierPathAnimation
{
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:_demoView.center];    //一定要设置 不然底层的CGPathRef找不到起始点,将会崩溃
    [path addCurveToPoint:CGPointMake(270, 410) controlPoint1:CGPointMake(0, Screen_Height) controlPoint2:CGPointMake(Screen_Width, 0)];    //以左下角和右上角为控制点
    
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    animation.path = path.CGPath;
    animation.duration = 3.0f;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    
    [_demoView.layer addAnimation:animation forKey:nil];
}

设置了一个贝塞尔路径,赋给动画的路径属性,两个控制点为左下以及右上

Demo源码 点击打开链接




以上为本篇博客全部内容,欢迎指正和交流。转载请注明出处~
CAAnimation 这个demo主要实现了,单个动画的移动,缩放,翻转,以及将这些动画组合起来的效果 立即下载

相关推荐

核心动画demo

核心动画中基本动画,关键帧,贝塞尔路径的使用。

iOS CAAnimation使用详解

理解了隐式动画后,显式动画就更加通俗易懂了。区别于隐式动画的特点,显式动画就是需要我们明确指定类型、时间等参数来实现效果的动画。除此之外,我们也可以创建非线性动画,比如沿着任意一条曲线运动等; 我们平时最常用的也是显式动画,不仅系统为我们的视图提供了UIViewAnimationWithBlock的动画封装,而且我们在熟悉了Core Animation的动画属性后也可以很方便的设置显式动画; 一、动画的分类 1、实现动画的方式 如果根据实现动画时直接操作对象的类型,我们可以简单的将动画分为视图和图层两种;但

假装你是大灰狼的专栏 1486

Directx关键帧动画

开发工具: Microsoft Visual Studio 2005 Microsoft DirectX 9.0 SDK Update

iOS 动画篇 - CAAnimation初识

CAAnimation 是一个抽象动画类,无法直接使用,需要使用它的子类,动画分为两大类:属性动画和转场动画。属性动画顾名思义,针对图层可动画属性进行动画,转场动画通常用于视图切换,例如A视图切换为B视图。CAAnimation 针对的是图层而非视图,因此默认情况下,它在完成动画后恢复到最初状态,且不会改变视图的相关属性。

LOLITA0164的博客 1万+

SAP-ML章<<<<第二节:物料账报错处理ABAP编程错误:RAISE_EXCEPTION>>>>

#SAP-ML章<<<<第一节:物料账报错处理ABAP编程错误:RAISE_EXCEPTION>>>> 1.物料账报错提示: ABAP编程错误:RAISE_EXCEPTION ABAP程序:SAPLFCML4H_RUN_PRICES应用组件CO-PC-ACT 2. 物料账报错背景: CKMLCP运行结算时 3. 解决方案: 通过DEBUG查看错误的地方 OKTZ:分析原因为:可能打包或者配置传输时弄错 ...

SAP.David.Gao的博客 1858

ESP32学习,驱动LED点阵屏,SD卡播放GIF动图

搬运就可以实现效果,如果有不足,请批评指正。下步该学一下如何文章上视频了,老是截图不方便展示效果。

u011625956的博客 1868

iOS动画之CAAnimation

iOS动画之CAAnimation,也就是CABasicAnimation、CAKeyframeAnimation、CAAnimationGroup或CATransition的父类

YouAreUmbrella的博客 1021

王爽《汇编语言》课程设计2(不涉及软盘版本)

assume cs:code,ds:data,ss:stack stack segment db 128 dup (0) stack ends data segment db 256 dup (0) data ends code segment start: mov ax,stack mov ss,ax mov sp,128 ...

weixin_41944412的博客 1178

iOS - CAAnimation动画集合

QuartzCore框架下CAAnimation以及UIViewAnimation的动画集合:移动、旋转、缩放、弹簧、组合动画以及各种转场效果、漂移动画和常见动画示例。

chaneylauok的博客 461

【数据结构】汉诺塔问题(丰富图解)

经典汉诺塔问题,丰富图解,助你理解。

℉f的博客 4105

coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?

🏆本文收录于《优快云问答解惑-专业版》专栏,主要记录项目实战过程中的Bug之前因后果及提供真实有效的解决方案,希望能够助你一臂之力,帮你早日登顶实现财富自由🚀;同时,欢迎大家关注&&收藏&&订阅!持续更新中,up!up!up!!

**My Coding Family** 2669

iOS CAAnimation核心动画

##前言 Core Animation核心动画主要是采用CAAnimation抽象超级类的子类实现动画,它是一组非常强大的动画处理API.使用它能做出非常绚丽的动画效果.Core Animation可以用在Mac OS X和iOS平台. Core Animation的动画执行过程是在后台操作的.不会阻塞主线程.要注意的是, Core Animation是直接作用在CALayer上的.并非UIVie...

Future_One的专栏 917

课堂练习及课后作业2.1:外部中断

本关任务:通过相关知识以及实验回答:版本 1 内核的第 6 次时钟中断发生时,断点和恢复点(指令地址)分别是多少?本关任务:修改版本 0 内核,使得每发生 100 次时钟中断,就在屏幕上输出一个字符‘t’和当时的进程号,如“t(0)”表示0号进程运行时发生了时钟中断。本关任务:通过实际操作回答在输出第一行 0/1 字符的过程中(如下图所示),共发生了几次时钟中断?本关任务:修改版本 1 内核源码,使得每次时钟中断发生时,都在屏幕上输出字符‘t’。分析版本1内核,找到第一次时钟中断的恢复点地址。

Junds0的博客 1万+

iOS Animation讲解

CAAnimation:核心动画的基础类,不能直接使用,负责动画运行时间,速度控制,本身实现了CAMediaTiming协议 CAPropertyAnimation:属性动画的基类(通过属性进行动画设置,是可动画属性),不能直接使用 CAAnimationGroup:动画组,动画组是一种组合设计模式,可以通过动画组来进行所有动画行为的统一控制,组中所有动画效果可以并发执行, CATransi

liuzhiwei_051512的专栏 542

R语言中的summarise函数和mutate函数用于计算分组占比。下面是关于这两个函数的详细介绍以及相应的源代码示例:

R语言中的summarise函数和mutate函数用于计算分组占比。

CodeByte的博客 1183

BLDC控制实验:方波、霍尔、开环、定速、正转

本文记录博主学习BLDC控制软件的过程。 文章目录1 概述2 软件架构及工具链2.1 软件架构2.2 工具链3 底层代码生成3.1 时钟配置3.2 按键输入配置 1 概述 本文是博主第一次做BLDC控制实验,目的仅仅是写代码让BLDC转起来,而暂时不考虑其他复杂的需求。 控制的方法是比较简单的六步换相法。利用电机内部的霍尔传感器,通过参考电机厂家给出的二二导通换相表,生成方波来控制电机。第一次实验为了简化需求,只考虑开环、定速、正转的情况: 开环:不考虑速度反馈,直接给PWM让电机转起来; 定速:不考虑速

u013288925的博客 1万+

[转]iOS动画专题·UIView二维形变动画与CAAnimation核心动画(transform动画,基础,关键帧,组动画,路径动画,贝塞尔曲线)...

1. iOS动画 总的来说,从涉及类的形式来看,iOS动画有:基于UIView的仿射形变动画,基于CAAnimation及其子类的动画,基于CG的动画。这篇文章着重总结前两种动画。 2. UIView动画 设置UIView形变动画有两种常见用到的属性,.frame,.transform,所以有的人也可以分别称之为: ① frame动画 ② transform动画 ...

249

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

ios开发 方形到圆的动画_iOS利用UIBezierPath + CAAnimation实现路径动画效果

前言上次给大家介绍了iOS利用UIBezierPath + CAAnimation实现路径动画效果的相关内容,今天实现一个根据心跳路径实现一个路径动画,让某一视图沿着路径进行运动.。效果图如下:核心代码1-首先通过 drawRect 绘制心形路径- (void)drawRect:(CGRect)rect {// Drawing code// 初始化UIBezierPathUIBezierPath ...

weixin_29214691的博客 291

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Cheetah vs UIView.animate vs CAAnimation:iOS动画方案如何选?一文讲透3种方案优劣

做 iOS 开发时,**UIView.animate、CAAnimation 和 Cheetah** 是三种最常见的动画实现方案,选错方案往往意味着返工。本文结合开源动画库 **Cheetah**(一个用 Swift 编写、基于 CADisplayLink 运行循环的 iOS 轻量动画工具)的源码实现,帮新手和普通开发者快速搞懂三种 iOS 动画方案的原理、优劣与适用场景,让你在面对"按钮点击动效

gitblog_00756的博客 956

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

iOS动画专题·UIView二维形变动画与CAAnimation核心动画(transform动画,基础,关键帧,组动画,路径动画,贝塞尔曲线)...

1. iOS动画 总的来说,从涉及类的形式来看,iOS动画有:基于UIView的仿射形变动画,基于CAAnimation及其子类的动画,基于CG的动画。这篇文章着重总结前两种动画。 2. UIView动画 设置UIView形变动画有两种常见用到的属性,.frame,.transform,所以有的人也可以分别称之为: ① frame动画 ② transform动画 这两种动画只需要在动画语法中适...

weixin_33772645的博客 313

核心动画--关键帧动画

#pragma mark - 动画代理方法 - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag { // 取出动画类型 NSString *type = [anim valueForKey:@"animationType"]; if ([type isEqualToStr...

weixin_30823683的博客 148

组合动画CAAnimationGroup

先在自定义的ViewController里声明定义一个UIImageView 1 @property (nonatomic,retain) UIImageView *imgView; 1 @synthesize imgView; 在viewDidLoad函数里添加图片,并执行组合动画 1    //添加图片 2 imgView = [[UIImage...

aa631555244的博客 241

iOS开发CAAnimation类动画, CATransition动画

#pragma mark - CAAnimation类、实现动画 #pragma mark ** CABasicAnimation 动画 - (IBAction)handleCABasicAnimation:(id)sender { /* 创建动画类的对象 */ CABasicAnimation *basic = [CABasicAnimati...

weixin_30588907的博客 106

Objective-C 使用核心动画CAAnimation实现动画

先来看看效果吧 整个核心动画就不多做介绍了,随便一搜就能有很多很详细的解释,主要使用以下四种 CABasicAnimation //经典动画 CAKeyframeAnimation //关键帧动画 CATransition //转场动画 CAAnimationGroup //组动画 分析下本次demo的动画构成 主要动画是...

weixin_30266885的博客 170
上一篇: CoreAnimation —— CALayer
下一篇: 核心动画中的动画组和转场动画
RannieR
博客等级 码龄14年 180粉丝 68原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值