Core Animation学习笔记—第六节Layer Style Property Animations

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

今天2021年12月6日
这一章叫做Layer Style Property Animations

Core Animation通过action对象实现隐式对象,action对象遵守CAAction协议,并且定义了一些相关layer操作,当layer的属性值更改时会触发action执行
可以通过遵守CAAction自定义action,实现runActionForKey:object:arguments:方法,使用可用的信息做任何想做的动画

When you define an action object, you must decide how you want that action to be triggered. The trigger for an action defines the key you use to register that action later. Action objects can be triggered by any of the following situations:
The value of one of the layer’s properties changed. This can be any of the layer’s properties and not just the animatable ones. (You can also associate actions with custom properties you add to your layers.) The key that identifies this action is the name of the property.
The layer became visible or was added to a layer hierarchy. The key that identifies this action is kCAOnOrderIn.
The layer was removed from a layer hierarchy. The key that identifies this action is kCAOnOrderOut.
The layer is about to be involved in a transition animation. The key that identifies this action is kCATransition.
Action的触发可在以下情况下发生,action的触发器定义了用来确定动画的key
属性值改变
当layer可见时或被加入到layer层级中时
从layer层级中移除时
layer需要transition动画时

When an appropriate event occurs on the layer, the layer calls its actionForKey: method to search for the action object associated with the key.
当事件发生在layer上时,layer调用actionForKey: 方法查找与key相关的action对象

Core Animation looks for action objects in the following order:
If the layer has a delegate and that delegate implements the actionForLayer:forKey: method, the layer calls that method. The delegate must do one of the following:
Return the action object for the given key.
Return nil if it does not handle the action, in which case the search continues.
Return the NSNull object, in which case the search ends immediately.
The layer looks for the given key in the layer’s actions dictionary.
The layer looks in the style dictionary for an actions dictionary that contains the key. (In other word, the style dictionary contains an actions key whose value is also a dictionary. The layer looks for the given key in this second dictionary.)
The layer calls its defaultActionForKey: class method.
The layer performs the implicit action (if any) defined by Core Animation.
通过以下顺序查找action
如果layer有代理并且代理实现了actionForLayer:forKey:方法,layer则会调用它,代理必须实现其中之一任务:
返回给定key的action对象
没找到action的话返回nil,会导致一直查找
返回NSNull,查找即刻终止
在layer的 actions 字典中查找key
在layer的style 字典中查找actions字典中的key
调用 defaultActionForKey: 方法
执行Core Animation定义的隐式action

If you provide an action object at any of the appropriate search points, the layer stops its search and executes the returned action object. When it finds an action object, the layer calls that object’s runActionForKey:object:arguments: method to perform the action. If the action you define for a given key is already an instance of the CAAnimation class, you can use the default implementation of that method to perform the animation. If you are defining your own custom object that conforms to the CAAction protocol, you must use your object’s implementation of that method to take whatever actions are appropriate.
Where you install your action objects depends on how you intend to modify the layer.
For actions that you might apply only in specific circumstances, or for layers that already use a delegate object, provide a delegate and implement its actionForLayer:forKey: method.
For layer objects that do not normally use a delegate, add the action to the layer’s actions dictionary.
For actions related to custom properties that you define on the layer object, include the action in the layer’s style dictionary.
For actions that are fundamental to the behavior of the layer, subclass the layer and override the defaultActionForKey: method.
如果你在查找时发出了一个action对象,layer会停止查找先执行action,调用action的runActionForKey:object:arguments: 方法执行action,如果action已经是 CAAnimation的实例,那么会执行默认实现,如果自定义了遵守CAAction协议的action对象,则必须使用对象对该方法的实现来采取任何适当的操作。

You can temporarily disable layer actions using the CATransaction class. When you change the property of a layer, Core Animation usually creates an implicit transaction object to animate the change. If you do not want to animate the change, you can disable implicit animations by creating an explicit transaction and setting its kCATransactionDisableActions property to true.
可以使用CATransaction暂时取消layer的action,通过显式创建transaction并设置其kCATransactionDisableActions 属性为true来取消隐式动画

猜你喜欢

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