start_time: 2024-05-02 00:18:24 +0800

TransitionAnimation 学习笔记

96
Auther丶
IP属地: 河南
2016.06.20 09:24 字数 809

酷炫的转场动画难的是想象,不是实现。

UIViewControllerTransitioning.h里面包含了所有你想要的

image

其中 ** UIViewControllerContextTransitioning ** 协议是转场动画上下文,看其属性就能看到,它提供了大多部分转场相关的细节内容

你可以拿到容器视图 :** containerView() -> UIView? **

还可以拿到动画的主要两个页面:** viewForKey(key: String) -> UIView? **

 // viewForKey: may return nil which would indicate that the animator should    not
// manipulate the associated view controller's view.
@available(iOS 8.0, *)
public func viewForKey(key: String) -> UIView? 
 
key:UITransitionContextFromViewKey 或者 UITransitionContextToViewKey 

在 Modal 转场里要注意,从上面可以知道,Custom 模式下,fromView 并不受 containerView 管理,这时通过viewForKey:方法来获取 fromView 得到的是 nil.但是你可以用下面的方法来搞

获取视图 ** public func viewControllerForKey(key: String) -> UIViewController? **
key:UITransitionContextToViewControllerKey, and UITransitionContextFromViewControllerKey. 再通过.view 来拿到对应的 to/from View

好了 还有常用切很重要的 ** public func completeTransition(didComplete: Bool)
**
动画执行后 必须写上这句话。 还有一个 ** transitionWasCancelled() -> Bool **

下面这些看注释就清楚了,跟交互相关的,在实现交互的时候也就这几个函数。下面细讲

public func isInteractive() -> Bool // This indicates whether the transition is currently interactive.//是否是与用户交互控制的动画

    // It only makes sense to call these from an interaction controller that
// conforms to the UIViewControllerInteractiveTransitioning protocol and was
// vended to the system by a container view controller's delegate or, in the case
// of a present or dismiss, the transitioningDelegate.
public func updateInteractiveTransition(percentComplete: CGFloat)
public func finishInteractiveTransition()
public func cancelInteractiveTransition()

实现一个简单的转场动画

1.实现对应的控制器代理方法,简单告诉他返回你已经实现了转场动画协议的类对象,或者实现了交互协议的类对象就好了。 注意如果当前不是交互控制转场的,在转场协议里面要返回nil。

push,pop转场 navi-UINavigationControllerDelegate

    @available(iOS 7.0, *)
    optional public func navigationController(navigationController: UINavigationController, interactionControllerForAnimationController animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning?
    
    @available(iOS 7.0, *)
    optional public func navigationController(navigationController: UINavigationController, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning?

selelcted-UITabBarControllerDelegate

@available(iOS 7.0, *)
optional public func tabBarController(tabBarController: UITabBarController, interactionControllerForAnimationController animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning?

@available(iOS 7.0, *)
optional public func tabBarController(tabBarController: UITabBarController, animationControllerForTransitionFromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning?

present,dismiss - transitioningDelegate

@available(iOS 2.0, *)
optional public func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning?

@available(iOS 2.0, *)
optional public func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?

optional public func interactionControllerForPresentation(animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning?

optional public func interactionControllerForDismissal(animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning?

@available(iOS 8.0, *)
optional public func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController?

含有interactionControllerForAnimationController是交互协议,animationControllerForDismissedController是告诉代理,转场的时候要返回我自己的转场动画控制器。比如这里我返回了一个已经实现UIViewControllerAnimatedTransitioning的动画控制器

extension PushOneVC:UINavigationControllerDelegate {
    func navigationController(navigationController: UINavigationController, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        if operation == .Pop && fromVC == self {
            return nil
        }
        
        let type:CircleTransitionType = operation == .Push ? CircleTransitionType.NavTransitionPush :CircleTransitionType.NavTransitionPop
        
        return CirclePushAnimationController(type: type)
    }
2.实现转场动画协议里面的两个方法
class CirclePushAnimationController: NSObject,UIViewControllerAnimatedTransitioning {

//告诉他动画的时常    
func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
    return 0.5
}

//这个是最主要的,告诉他执行什么动画
func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
...
1.拿到容器视图containerView,fromVC,toVC
2.将要执行动画的视图 toView 加到containerView中,其中fromView 系统已经自动加入.
** 注意,如果当前是present切style是Custom,那么在dismiss的时候就不要加toView了,你可以这样简单理解,一般在转场结束completeTransition后,会自动将fromView从容器中移除。但是Custom类型的时候却没有移除,你可以明显的看到。因此在dismiss的时候,之前的fromView 也就变成了toView。**
3.书写酷炫的动画代码,一般使用UIView 的类方法 animaitonWithDur....,如果对toView/fromView的layer做动画就用CABaseAnimation去做。
4.在completion回调或者animationdidStop中记得写上 completeTransition(ture)很重要
}

}
3.实现手势交互

在每一个需要交互控制转场动画的根图层上加一个pan手势,根据手势的状态或者拖动比例以及系统提供的updateInteractiveTransition(0.7)等方法来控制转场进度。

注意一点要在对应的交互代理方法interactionControllerForPresentation中判断当前是不是正在进行手势交互,如果是的话才返回对应的交互控制器,如果不是的话就返回nil

系统提供了一个手势交互控制器,UIPercentDrivenInteractiveTransition。当然你也可以继承他,写一个自己的手势交互控制器。记得一点,每一个需要交互控制转场动画的视图都需要一个自己的交互手势。如fromVC 和 toVC都应该有一个自己的交互手势。这也不难理解。

下面是手势交互控制转场进度的代码

    switch gesture.state {
    case .Began :
        interation = true
        self.startTransition()
    case .Changed :
        updateInteractiveTransition(percent)
    
    case .Ended :
        completionSpeed = 0.99
        interation = false
        if percent > 0.7 {
            finishInteractiveTransition()
        }else {
            cancelInteractiveTransition()
        }
    case .Cancelled :
        interation = false
        completionSpeed = 0.99
        cancelInteractiveTransition()
    default :
        break
    }
}

func startTransition() {
    switch type {
    case .Presente, .Push :
        
        if  let closure = config {
            closure()//执行nav.pushViewController()或者self.presentViewController
        }
    case .Pop:
        vc?.navigationController?.popViewControllerAnimated(true)
    case .Dismiss:
        vc?.dismissViewControllerAnimated(true, completion: nil)
    }
}

最后说一下,你可以通过使用截图snapshotViewAfterScreenUpdates:NO(YES代表立即刷新视图然后截图),对截图和toView进行动画操作,来做一些cell的转场动画。 你也可以通过layer.mask属性做最上面gif图的转场效果,对这个属性有疑问的,可以百度搜alpha通道,会帮助理解。

Demo下载地址

转场详解

另一篇参考博客

最后编辑于 :2017-12-03 06:55:41
©著作权归作者所有,转载或内容合作请联系作者
序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
783c64db 45e5 48d7 82e4 95736f50533e 沈念sama 阅读 160165 评论 4 赞 364
序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
783c64db 45e5 48d7 82e4 95736f50533e 沈念sama 阅读 67720 评论 1 赞 298
文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
8 开封第一讲书人 阅读 109849 评论 0 赞 244
文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
8 开封第一讲书人 阅读 44245 评论 0 赞 213
正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
388e473c fe2f 40e0 9301 e357ae8f1b41 茶点故事 阅读 52596 评论 3 赞 288
文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
8 开封第一讲书人 阅读 40747 评论 1 赞 222
那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
783c64db 45e5 48d7 82e4 95736f50533e 沈念sama 阅读 31977 评论 2 赞 315
文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
8 开封第一讲书人 阅读 30708 评论 0 赞 204
序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
783c64db 45e5 48d7 82e4 95736f50533e 沈念sama 阅读 34448 评论 1 赞 246
正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
388e473c fe2f 40e0 9301 e357ae8f1b41 茶点故事 阅读 30657 评论 2 赞 249
正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
388e473c fe2f 40e0 9301 e357ae8f1b41 茶点故事 阅读 32141 评论 1 赞 261
序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
783c64db 45e5 48d7 82e4 95736f50533e 沈念sama 阅读 28493 评论 3 赞 258
正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
388e473c fe2f 40e0 9301 e357ae8f1b41 茶点故事 阅读 33153 评论 3 赞 238
文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
8 开封第一讲书人 阅读 26108 评论 0 赞 8
文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
8 开封第一讲书人 阅读 26890 评论 0 赞 198
我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
783c64db 45e5 48d7 82e4 95736f50533e 沈念sama 阅读 35799 评论 2 赞 277
正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
388e473c fe2f 40e0 9301 e357ae8f1b41 茶点故事 阅读 35685 评论 2 赞 272
end_time: 2024-05-02 00:18:24 +0800 Completed in 137.743142ms