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万+

Halcon像素比例标定(误差大)-相机标定

一种简易标定: 像素比例标定,如果需要精确一些则需要在高度和宽度上都需要计算,然后求出比例系数。需要高精度标定板和相机,以及多次标定才能达到稳定的状态,这种标定简单,但是误差大。 read_image (Img0008, 'D:/00项目代码/000视觉软件/02Halocn算法20190501/04测量20190424/07_比例计算测量标定/IMG0008.jpg') rgb1_to_...

SSSS的博客 3984

2024高校网络安全管理运维赛题目--复现+题目+wp

官方writeup集思广益比较好的备份。

书山有路勤为径,学海无涯苦作舟 1323

iOS动画之CAAnimation

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

YouAreUmbrella的博客 1021

磁编码器选型避坑指南:为什么MT6816比霍尔传感器更适合你的伺服系统?

本文深入对比了MT6816磁编码器与传统霍尔传感器在伺服系统中的应用。MT6816基于AMR技术,对磁场方向敏感而非强度,具备高精度、强抗干扰、高安装容差和极低延时等优势,尤其适合高转速、恶劣环境下的工业场景,能有效提升系统可靠性与长期稳定性。

rainy的博客 1103

iOS - CAAnimation动画集合

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

chaneylauok的博客 461

ESP32-P4实战:LVGL V9.4 PPA加速配置与性能对比

本文详细介绍了如何在ESP32-P4平台上配置LVGL V9.4的PPA硬件加速功能,通过启用PPA加速显著提升图形处理性能并降低CPU占用率。文章包含环境准备、配置步骤、性能对比及常见问题解决方案,帮助开发者高效利用ESP32-P4的PPA加速器优化嵌入式GUI应用。

rice5的博客 1190

DSShop移动商城网店系统 反序列化RCE漏洞复现

DSShop单店铺移动商城网店系统的getCartList方法的cart参数存在php反序列化漏洞,攻击者可以通过漏洞执行任意代码,获取服务器权限。

0xSec 1471

iOS CAAnimation核心动画

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

Future_One的专栏 917

SAP Retail 补货里的 Requirement Groups,站点和商品太多时,主数据维护如何做减法

SAP Retail补货主数据设计中,Requirement Groups(需求组)是常被低估但至关重要的分类机制。它通过将具有相同补货需求的接收方(如门店)按商品分类(Merchandise Category)分组,大幅降低主数据维护量。在大型零售场景中(如800家门店、6万商品),直接维护每个门店和商品的组合参数会导致维护量失控。Requirement Groups允许企业按品类特性(如乳制品、饮料等)为不同门店组设置统一补货参数,并随业务变化灵活调整。该机制与商品分类紧密绑定,不同于MRP Group

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 68

iOS Animation讲解

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

liuzhiwei_051512的专栏 542

ZYNQ PL通过EMIO ETHE1外接PHY由GMII转RGMII

1,设置EMIO方式引出ETHE1 2,从GMII过度到RGMII GMII: 发送 gmii_tx_clk gmii_tx_d[7:0] gmii_tx_en gmii_tx_er 接收 gmii_rx_clk gmii_rx_d[7:0] gmii_rx_dv gmii_rx_er RGMII: 发送 tx_clk tx_d[3:0] tx_ctrl 接收 rx_clk rx_d[3:0] rx_ctrl 首先数据总线从时钟单边沿采样8bit转变为了双边沿采样4bit,从88E1

wangjie36的博客 1万+

流体仿真软件:COMSOL Multiphysics二次开发_(3).流体仿真基础理论

流体仿真基础理论包括流体的基本性质、流体运动方程、边界条件、初始条件、湍流模型和网格生成等。在使用COMSOL Multiphysics进行流体仿真时,正确设置这些参数和条件是获得准确仿真结果的关键。通过实际应用案例,可以更好地理解如何在COMSOL中进行流体仿真,并提取和分析仿真结果。希望本文对流体仿真初学者和从业人员有所帮助。

kkchenjj的博客 1341

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

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

249

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

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

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

weixin_29214691的博客 291

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

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

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

gitblog_00756的博客 956

深度学习工具包 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")

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、付费专栏及课程。

余额充值