前言
在ios开发中,我们model另外一个控制器的时候,一般都使用默认的转场动画。本文将给大家详细介绍关于ios swift控制器转场动画的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。
返回效果也可更改
四种转场动画
1. move:源图片位置移动到目标图片位置;
2. circle:根据源控件大小创建圆形或者椭圆形path路径,放大展示目标;
3. tier:源左右,目标由小到大缩放;
4. middle:源的中心点开始放大,返回是缩回到中心。
代码解析
给uiviewcontroller添加一个属性yy_routeranimation: yytransition
1
2
3
4
5
6
7
8
9
10
11
12
13
|
extension uiviewcontroller {
public var yy_routeranimation : yytransition {
set {
objc_setassociatedobject(self, &yytransitionkey.krouteranimationkey, newvalue, .objc_association_retain_nonatomic)
}
get {
guard let r = objc_getassociatedobject(self, &yytransitionkey.krouteranimationkey) as? yytransition else {
return yytransition()
}
return r
}
}
}
|
yytransition类
1
|
public class yytransition: nsobject
|
遵守代理
1
|
extension yytransition: uiviewcontrolleranimatedtransitioning, uiviewcontrollertransitioningdelegate, uinavigationcontrollerdelegate
|
实现代理方法
1
2
3
4
5
6
|
return self
}
public func transitionduration( using transitioncontext: uiviewcontrollercontexttransitioning?) -> timeinterval {
return 转场动画所需时间
}
|
这个方法内调用相应动画方法
1
2
3
4
5
6
7
8
9
10
11
12
|
public func animatetransition( using transitioncontext: uiviewcontrollercontexttransitioning) {
switch self.yy_ransitionanimationtype {
case .circle:
break
case .move:
break
case .middle:
break
case .tier:
break
}
}
|
相关属性
1
2
3
4
5
6
7
8
9
10
11
12
|
extension yytransition {
// 是push还是pop
public var yy_isback: bool {}
// 动画类型
var yy_ransitionanimationtype: yytransitionanimationtype {}
// 源view名字
var yy_fromviewpath: string? { }
// 目标view名字
var yy_toviewpath: string? { }
// 句柄
var yy_transitioncontext: uiviewcontrollercontexttransitioning {}
}
|
实现基础动画结束时的代理方法
1
2
3
4
5
6
7
|
extension yytransition: caanimationdelegate {
public func animationdidstop(_ anim: caanimation, finished flag: bool ) {
yy_transitioncontext.completetransition(!yy_transitioncontext.transitionwascancelled)
yy_transitioncontext.viewcontroller(forkey: .from)?.view.layer.mask = nil
yy_transitioncontext.viewcontroller(forkey: .to)?.view.layer.mask = nil
}
}
|
基础动画对路径操作的动画
1
2
3
|
extension yytransition {
func maskanimation(targetvc: uiviewcontroller, startpath: uibezierpath, endpath: uibezierpath, context: uiviewcontrollercontexttransitioning) {
}
|
下面四个文件内实现相对应的动画
1
2
3
4
|
yytransition+circle
yytransition+move
yytransition+tier
yytransition+middle
|
动画实现的思想基本就是拿到源view和目标view,控制位置和大小,做相应的动画即可。
用到的方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
uiviewcontrollercontexttransitioning 调用
public func viewcontroller(forkey key: uitransitioncontextviewcontrollerkey) -> uiviewcontroller?
uiviewcontroller调用
open func value(forkeypath keypath: string) -> any?
* when requesting a snapshot, 'afterupdates' defines whether the snapshot is representative of what's currently on screen or if you wish to include any recent changes before taking the snapshot.
open func snapshotview(afterscreenupdates afterupdates: bool ) -> uiview?
open func convert(_ rect: cgrect, from view: uiview?) -> cgrect
open func insertsubview(_ view: uiview, belowsubview siblingsubview: uiview)
// this must be called whenever a transition completes (or is cancelled.)
// typically this is called by the object conforming to the
// uiviewcontrolleranimatedtransitioning protocol that was vended by the transitioning
// delegate. for purely interactive transitions it should be called by the
// interaction controller. this method effectively updates internal view
// controller state at the end of the transition.
public func completetransition(_ didcomplete: bool )
|
具体代码在ye项目地址中yytransition动态库中
eg在yysourcetransitionviewcontroller和yytargettransitionviewcontroller中可以看到。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。
原文链接:https://www.jianshu.com/p/4b01f79686da