Core Animation Programming Guide--动画编程指南

本文深入探讨了iOS应用开发中CALayer的动画特性,包括基于Layer属性的动画、三维空间操作、背景与边框效果、阴影设置等。详细介绍了CABasicAnimation与CATransition的基本用法,以及如何在iOS中修改UIView的默认layer动画。同时,文章还覆盖了动画结束通知、同时动画、布局属性调整和手动布局实现等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、CALayer
(一)基于Layer属性的动画
Core Animation Programming Guide--动画编程指南 - supershll - 记忆里
更改Layer的属性更改可引起动画,如上图所示列出了常用的可动画属性。全部的属性有:anchorPoint、backgroundColor、backgroundFilters、borderColor、borderWidth、bounds、compositingFilter、contents、contentsRect、corderRadius、doubleSided、filters、hidden、mask、masksToBounds、opacity、postion、shadowColor、shadowOffset、shadowOpacity、shadowPath、shadowRadius、sublayers、sublayerTransform、transform、zPosition。
其中更改不同的属性可引起的动画类型不同,有CABasicAnimation(duration是0.25秒或者当前动画事务的duration,key path就是layer的属性名),还有CATransition(duration是0.25秒或当前动画事务的duration,Type是kCATransitionFade,起始progress是0.0,终止progress是1.0)。

(二)Layer对象有其自己的坐标系,同时使用基于点的坐标系和unit坐标系,他们有不同的作用。
    
Core Animation Programming Guide--动画编程指南 - supershll - 记忆里
 
Core Animation Programming Guide--动画编程指南 - supershll - 记忆里
(三)Layer可以在三维上操作,layer的transform和sublayerTransform属性
     Core Animation Programming Guide--动画编程指南 - supershll - 记忆里
(四)在IOS中,UIView的默认layer的CALayer类实例。如果你想更改它,就就重载UIView的layerClass方法,并返回你自定义的Layer类。系统提供的CALayer子类有:CAEmitterLayer、CAGradientLayer、CAEAGLLayer、CAReplicatorLayer、CAScrollLayer、CAShapeLayer、CATextLayer、CATiledLayer、CATransformLayer。
(五)Layer的内容提供方法:
    1、设置layer.contents=[CGImageRef ...];和layer.contentsGravity=kCAGravityCenter...;layer.contentsScale=...;适用于静态的或很少更改的layer。
    2、layer.delegate=...;指定layer的delegate,让delegate绘制layer的内容。
    3、定义一个layer子类,并自己绘制layer的内容。
 
(五)Layer有其自己的Background和Border
Core Animation Programming Guide--动画编程指南 - supershll - 记忆里
layer.backgourndColor、borderColor、borderWidth、corderRadius
layer.masksToBounds属性为yes时,corderRadius会影响到contents的显示,否则不影响。

(六)Layer支持内建的shadow
layer shadows的opacity默认为0,同样的masksToBounds属性为yes时,shadow的变化会影响contents的显示。

二、CABasicAnimation
CABasicAnimation* fadeAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];
fadeAnim.fromValue = [NSNumber numberWithFloat:1.0];
fadeAnim.toValue = [NSNumber numberWithFloat:0.0];
fadeAnim.duration = 1.0;
[theLayer addAnimation:fadeAnim forKey:@"opacity"];
// Change the actual data value in the layer to the final value.
theLayer.opacity = 0.0;

三、CAKeyframeAnimation
// create a CGPath that implements two arcs (a bounce)
CGMutablePathRef thePath = CGPathCreateMutable();
CGPathMoveToPoint(thePath,NULL,74.0,74.0);
CGPathAddCurveToPoint(thePath,NULL,74.0,500.0,
                                   320.0,500.0,
                                   320.0,74.0);
CGPathAddCurveToPoint(thePath,NULL,320.0,500.0,
                                   566.0,500.0,
                                   566.0,74.0);
CAKeyframeAnimation * theAnimation;
// Create the animation object, specifying the position property as the key path.
theAnimation=[CAKeyframeAnimation animationWithKeyPath:@"position"];
theAnimation.path=thePath;
theAnimation.duration=5.0;
// Add the animation to the layer.
[theLayer addAnimation:theAnimation forKey:@"position"];
四、停止动画:
 1、[layer removeAnimationForKey:]
 2、[layer removeAllAnimations]
 
五、同时动画:CAAnimationGroup

// Animation group
CAAnimationGroup* group = [CAAnimationGroup animation];
group.animations = [NSArray arrayWithObjects:colorAnim, widthAnim, nil];
group.duration = 5.0;
[myLayer addAnimation:group forKey:@"BorderChanges"];

六、动画结束时得到通知:

  1、[animation setCompletionBlock:]

  2、[animation setDelegate:]通过代理方法animationDidStop:finished:实现。

  3、如果你想连接2个动画,那就不要使用通知了,使用[animation setBeginTime:]实现就可以了。

 

七、在IOS中修改UIView的Layer动画,必须在UIView的animation block中执行。UIView类默认禁用了layer动画,但在animation block中又重新启用了layer动画。

 

八、Layer的某系属性会影响到subLayer,例如speed属性,默认是1,如果设置成2,那么动画时subLayer的动画速度会加快一倍,并且时间缩短一半。

九、调整Layout属性:使用一个或多个CAConstraint对象。

Core Animation Programming Guide--动画编程指南 - supershll - 记忆里

 

Core Animation Programming Guide--动画编程指南 - supershll - 记忆里

 

// Create and set a constraint layout manager for the parent layer.
theLayer.layoutManager=[CAConstraintLayoutManager layoutManager];

 // Create the first sublayer.
CALayer *layerA = [CALayer layer];
layerA.name = @"layerA";
layerA.bounds = CGRectMake(0.0,0.0,100.0,25.0);
layerA.borderWidth = 2.0;

 // Keep layerA centered by pinning its midpoint to its parent's midpoint.
[layerA addConstraint:[CAConstraint constraintWithAttribute:kCAConstraintMidY relativeTo:@"superlayer" attribute:kCAConstraintMidY]];
[layerA addConstraint:[CAConstraint constraintWithAttribute:kCAConstraintMidX relativeTo:@"superlayer" attribute:kCAConstraintMidX]];
[theLayer addSublayer:layerA];

// Create the second sublayer
CALayer *layerB = [CALayer layer];
layerB.name = @"layerB";
layerB.borderWidth = 2.0;

// Make the width of layerB match the width of layerA.
[layerB addConstraint:[CAConstraint constraintWithAttribute:kCAConstraintWidth relativeTo:@"layerA" attribute:kCAConstraintWidth]];
 
// Make the horizontal midpoint of layerB match that of layerA
[layerB addConstraint:[CAConstraint constraintWithAttribute:kCAConstraintMidX relativeTo:@"layerA" attribute:kCAConstraintMidX]];
 
// Position the top edge of layerB 10 points from the bottom edge of layerA.
[layerB addConstraint:[CAConstraint constraintWithAttribute:kCAConstraintMaxY relativeTo:@"layerA" attribute:kCAConstraintMinY offset:-10.0]];
 
// Position the bottom edge of layerB 10 points
//  from the bottom edge of the parent layer.
[layerB addConstraint:[CAConstraint constraintWithAttribute:kCAConstraintMinY relativeTo:@"superlayer" attribute:kCAConstraintMinY offset:+10.0]];
[theLayer addSublayer:layerB];

十、还可以使用delegate的layoutSubLayersOfLayer:来手动实现布局。

       如果你有自己的Layer子类,你可以重载layoutSublayers方法来处理任何的layout任务。

十一、不像UIView,父Layer不自动clip子Layer的内容。通过设置masksToBounds属性为YES来修改此行为。

十二、转换坐标:convertPoint:fromLayer:、convertPoint:toLayer:、convertRect...、convertTime...

十三、CATransition

CATransition* transition = [CATransition animation];
transition.startProgress = 0;
transition.endProgress = 1.0;
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromRight;
transition.duration = 1.0;


// Add the transition animation to both layers
[myView1.layer addAnimation:transition forKey:@"transition"];
[myView2.layer addAnimation:transition forKey:@"transition"];
 
// Finally, change the visibility of the layers.
myView1.hidden = YES;
myView2.hidden = NO;

 

十四、显式Transaction

[CATransaction begin]; // Outer transaction


// Change the animation duration to two seconds
[CATransaction setValue:[NSNumber numberWithFloat:2.0f] forKey:kCATransactionAnimationDuration];

// Move the layer to a new position
theLayer.position = CGPointMake(0.0,0.0);
 
[CATransaction begin]; // Inner transaction
// Change the animation duration to five seconds
[CATransaction setValue:[NSNumber numberWithFloat:5.0f] forKey:kCATransactionAnimationDuration];
 
// Change the zPosition and opacity
theLayer.zPosition=200.0;
theLayer.opacity=0.0;
 
[CATransaction commit]; // Inner transaction
[CATransaction commit]; // Outer transaction


资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在当今的软件开发领域,自动化构建与发布是提升开发效率和项目质量的关键环节。Jenkins Pipeline作为一种强大的自动化工具,能够有效助力Java项目的快速构建、测试及部署。本文将详细介绍如何利用Jenkins Pipeline实现Java项目的自动化构建与发布。 Jenkins Pipeline简介 Jenkins Pipeline是运行在Jenkins上的一套工作流框架,它将原本分散在单个或多个节点上独立运行的任务串联起来,实现复杂流程的编排与可视化。它是Jenkins 2.X的核心特性之一,推动了Jenkins从持续集成(CI)向持续交付(CD)及DevOps的转变。 创建Pipeline项目 要使用Jenkins Pipeline自动化构建发布Java项目,首先需要创建Pipeline项目。具体步骤如下: 登录Jenkins,点击“新建项”,选择“Pipeline”。 输入项目名称和描述,点击“确定”。 在Pipeline脚本中定义项目字典、发版脚本和预发布脚本。 编写Pipeline脚本 Pipeline脚本是Jenkins Pipeline的核心,用于定义自动化构建和发布的流程。以下是一个简单的Pipeline脚本示例: 在上述脚本中,定义了四个阶段:Checkout、Build、Push package和Deploy/Rollback。每个阶段都可以根据实际需求进行配置和调整。 通过Jenkins Pipeline自动化构建发布Java项目,可以显著提升开发效率和项目质量。借助Pipeline,我们能够轻松实现自动化构建、测试和部署,从而提高项目的整体质量和可靠性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值