UIKit学习笔记—第四节Animations

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

今天2021年11月30日
这一章叫做Animations
在这里插入图片描述

UIKit提供内置动画的属性,根据值的改变隐式平滑过渡

我其实用Core Animation更多一些
core Animation提供了更改layer的方法,layer的可操作性更好一些
UIKit的动画其实也是Core Animation封装上来的
Core Animation提供了以下更多的支持
The size and position of the layer
The center point used when performing transformations
Transformations to the layer or its sublayers in 3D space
The addition or removal of a layer from the layer hierarchy
The layer’s Z-order relative to other sibling layers
The layer’s shadow
The layer’s border (including whether the layer’s corners are rounded)
The portion of the layer that stretches during resizing operations
The layer’s opacity
The clipping behavior for sublayers that lie outside the layer’s bounds
The current contents of the layer
The rasterization behavior of the layer

Z坐标系下的layer位置
阴影,圆角,3D变换

开始动画的方法

  1. 类方法 + 闭包

animateWithDuration:animations:
animateWithDuration:animations:completion:
animateWithDuration:delay:options:animations:completion:
UIKit提供了这些类方法来开始动画,因为这些都是类方法,所以不会绑定到单个视图,意思是你可以在这个闭包内处理多个视图

  1. Begin/Commit 方法
    在 beginAnimations:context: 和 commitAnimations 类方法之间完成所有值的改变来引发一些动画,他会自动开启一个线程,不会阻塞当前线程或主线程。
    如果想为一些更改添加动画效果,但不想为其他更改添加动画效果,可以使用setAnimationsEnabled:方法暂时禁用动画,进行任何不需要动画的更改,然后再次调用setAnimationsEnabled:以重新启用动画。您可以通过调用reAnimationsEnabled类方法来确定动画当前是否启用

在这里插入图片描述

以上这些方法可以配置Begin/Commit动画闭包,懒得翻表了

配置动画代理
动画委托可在动画执行前后执行一些操作,通过setAnimationDelegate:关联一个代理对象,在动画开始或完成前后setAnimationWillStartSelector: 通知selector执行操作
Ps:官网又说了,可以不用代理,把要执行的东西写到动画闭包里去就行

动画闭包可嵌套,可拥有独立配置

When creating reversible animations in conjunction with a repeat count, consider specifying a non integer value for the repeat count. For an autoreversing animation, each complete cycle of the animation involves animating from the original value to the new value and back again. If you want your animation to end on the new value, adding 0.5 to the repeat count causes the animation to complete the extra half cycle needed to end at the new value. If you do not include this half step, your animation will animate to the original value and then snap quickly to the new value, which may not be the visual effect you want
如果想实现可逆动画则不需要设置repeatcount,如果想让你的动画的同时新值也设置完毕,则需设置repeat count 为0.5,新值设置完毕需要额外的0.5个动画周期

View transitions help you hide sudden changes associated with adding, removing, hiding, or showing views in your view hierarchy. You use view transitions to implement the following types of changes:
Change the visible subviews of an existing view. You typically choose this option when you want to make relatively small changes to an existing view.
Replace one view in your view hierarchy with a different view. You typically choose this option when you want to replace a view hierarchy that spans all or most of the screen.
Important: View transitions should not be confused with transitions initiated by view controllers, such as the presentation of modal view controllers or the pushing of new view controllers onto a navigation stack. View transitions affect the view hierarchy only, whereas view-controller transitions change the active view controller as well. Thus, for view transitions, the view controller that was active when you initiated the transition remains active when the transition finishes
Transition屏蔽了与在视图层级中添加,删除,显示视图相关的突变,transition可以实现以下两种变化
对子视图进行微小改变
替换整个视图
视图转换与VC里的视图转换有些区别,视图转换仅仅影响视图层级,VC中的转换还会影响VC,因此,视图转换将会从你初始化转换开始转为 active状态,并且在转换完毕之后依旧保持active状态

you use the transitionWithView:duration:options:animations:completion: method to initiate a transition animation for a view. In the animations block passed to this method, the only changes that are normally animated are those associated with showing, hiding, adding, or removing subviews. Limiting animations to this set allows the view to create a snapshot image of the before and after versions of the view and animate between the two images, which is more efficient. However, if you need to animate other changes, you can include the UIViewAnimationOptionAllowAnimatedContent option when calling the method. Including that option prevents the view from creating snapshots and animates all changes directly.
使用transitionWithView:duration:options:animations:completion: 方法初始化转换动画,在此方法中仅可做与增删显示或移除视图有关的动画,转换将会在两个屏幕快照之间完成,效率更高,如果你想做更多的操作,可以在调用期间设置 UIViewAnimationOptionAllowAnimatedContent选项,避免快照和直接完成所有动画

you use the transitionFromView:toView:duration:options:completion: method to transition between two views. This method actually removes the first view from your hierarchy and inserts the other, so you should make sure you have a reference to the first view if you want to keep it. If you want to hide views instead of remove them from your view hierarchy, pass the UIViewAnimationOptionShowHideTransitionViews key as one of the options.
In addition to swapping out views, your view controller code needs to manage the loading and unloading of both the primary and secondary views
替换视图
替换视图仅仅是替换视图(这好像是一句废话),意思是与VC无关,仅仅是替换视图,
transitionFromView:toView:duration:options:completion:方法用来转换两个视图,先会将旧的视图从视图层级中移除,然后插入新的视图,如果你想要保留它,你需要确保你拥有第一个视图的引用,如果你只想暂时隐藏视图而非移除它,有 UIViewAnimationOptionShowHideTransitionViews 选项可设置
VC需要完成新旧视图的加载和消除

For block-based animations, use the completion handler supported by the animateWithDuration:animations:completion: and animateWithDuration:delay:options:animations:completion: methods to execute any follow-on animations.
For begin/commit animations, associate a delegate object and a did-stop selector with the animation
An alternative to linking animations together is to use nested animations with different delay factors so as to start the animations at different times.
动画的连接
动画支持连接为一个序列,而非同时触发
对于闭包类型的动画使用animateWithDuration:animations:completion: 和 animateWithDuration:delay:options:animations:completion: 方法连接其他动画(completion)
对于begin/commit动画,关联一个代理对象和selector连接其他动画
使用delay结合嵌套动画

由于本人对Core Animation更熟悉一些,所以底下这一部分讲Core Animation的时候会写到

猜你喜欢

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