Core Animation学习笔记—第五节Advanced Animation Tricks

各位iOS开发大佬们好:
我是一名swift+swiftUI栈的iOS小白,目前还在上大三,最近准备实习,面试的过程中发现现在大公司很多还在用OC + UIKit的技术栈,OC我还在考虑要不要学,目前想先把UIKit学完,这是我在官网学习UIKit英文文档时摘录的本人认为的重点,如果你们也觉得对你们有用的话欢迎持续关注,我大概一天更一节,有事除外。格式什么的我也就不做了,翻译都是我自己翻译的,哪里不对欢迎在评论区指正,感谢各位大佬支持

今天2021年12月5日
这一章叫做Advanced Animation Tricks

To perform a transition animation, you create a CATransition object and add it to the layers involved in the transition. You use the transition object to specify the type of transition to perform and the start and end points of the transition animation. You do not need to use the entire transition animation either. The transition object lets you specify the start and end progress values to use when animating. These values let you do things like start or end an animation at its midpoint.
通过创建CATransition对象之后添加到layer上执行转换动画,指定转换类型,开始和结束点,transition对象使你能够控制转换动画的过程,比如在动画进行中停止动画

Timing is an important part of animations, and with Core Animation you specify precise timing information for your animations through the methods and properties of the CAMediaTiming protocol. Two Core Animation classes adopt this protocol. The CAAnimation class adopts it so that you can specify timing information in your animation objects. The CALayer also adopts it so that you can configure some timing-related features for your implicit animations, although the implicit transaction object that wraps those animations usually provides default timing information that takes precedence.
两个类遵守了CAMediaTiming协议,一个是CAAnimation,一个是CALayer,目的都是为了和时间有关的一些操作,比如动画时间函数,和CALayer有关的一些操作
以下三个属性更多时间有关的操作
Use the beginTime property to set the start time of an animation. Normally, animations begin during the next update cycle. You can use the beginTime parameter to delay the animation start time by several seconds. The way to chain two animations together is to set the begin time of one animation to match the end time of the other animation.
If you delay the start of an animation, you might also want to set the fillMode property to kCAFillModeBackwards. This fill mode causes the layer to display the animation’s start value, even if the layer object in the layer tree contains a different value. Without this fill mode, you would see a jump to the final value before the animation starts executing. Other fill modes are available too.
The autoreverses property causes an animation to execute for the specified duration and then return to the starting value of the animation. You can combine this property with the repeatCount property to animate back and forth between the start and end values. Setting the repeat count to a whole number (such as 1.0) for an autoreversing animation causes the animation to stop on its starting value. Adding an extra half step (such as a repeat count of 1.5) causes the animation to stop on its end value.
Use the timeOffset property with group animations to start some animations at a later time than others.
Begintime用于设置动画开始时间,记得设置fillmode为KCAFillModeBackwards防止在动画开始执行前跳到其他值
Autoreverse会让动画执行完毕后自动跳回到开始值,整数repeatCount会让动画停在开始值,多0.5个周期会让动画停在结束值
组动画中timeoffset可以让一些动画延时执行

可以通过设置speed为0暂停动画

Explicit Transactions Let You Change Animation Parameters
Every change you make to a layer must be part of a transaction. The CATransaction class manages the creation and grouping of animations and their execution at the appropriate time. In most cases, you do not need to create your own transactions. Core Animation automatically creates an implicit transaction whenever you add explicit or implicit animations to one of your layers. However, you can also create explicit transactions to manage those animations more precisely.
You create and manage transactions using the methods of the CATransaction class. To start (and implicitly create) a new transaction call the begin class method; to end that transaction, call the commit class method. In between those calls are the changes that you want to be part of the transaction.

CATransaction类管理动画的创建和组合以及他们的执行,默认情况下,当你为layer添加显式或隐式动画时,Core Animation自动创建隐式transaction
开启新的transaction用begin方法,结束一个transaction用commit方法,在这两个方法之间完成transaction的操作
You can also assign a completion block to the entire transaction so that your app can be notified when the group of animations finishes. Changing animation parameters requires modifying the appropriate key in the transaction dictionary using the setValue:forKey: method
通过transaction闭包可以在一组动画完成之时通知你的app做一些相应操作,通过KVC完成

Each begin call must be matched by a corresponding call to the commit method. Only after you commit the changes for the outermost transaction does Core Animation begin the associated animations.
可以嵌套transaction,begin和commit总是需要匹配出现,commit之后动画才会被执行

When modifying the perspective of a scene, you need to modify the sublayerTransform matrix of the superlayer that contains the layers being viewed. Modifying the superlayer simplifies the code you have to write by applying the same perspective information to all of the child layers. It also ensures that the perspective is applied correctly to sibling sublayers that overlap each other in different planes.
当同级layer相互重叠需要看到底层layer时,可以修改透明度,在父layer上可以对所有子layer执行相同的透明度

猜你喜欢

转载自blog.csdn.net/Programmer_Roy/article/details/121681825