自定義轉場動畫使我們所需要的,是為了更加體現APP的絢麗和增加用戶對產品的好奇,甚至是那么點點的使用率。
不管是為了什么,今天寫一下在present/dismiss/push/pop的時候轉場動畫。
首先建立一個NavigationController,兩個按鈕present和push
present按鈕present出PresentViewController
Present
在PresentViewController中建立一個dismiss按鈕dismiss
讓NavigationController的rootViewController遵循UIViewControllerTransitioningDelegate
并且實現
哎呀,出錯了,必須返回一個東西。
接下來我們新建一個類CustomPresentAnimation繼承NSObject并且遵循UIViewControllerAnimatedTransitioning
UIViewControllerAnimatedTransitioning有兩個代理必須實現
然后在這個方法里面寫上你要的動畫就可以了
我們找到NavigationController的rootViewController引入CustomPresentAnimation并初始化返回
運行代碼是不是你想要的呢。。。你可以寫更好看的動畫哦,比如圓形擴散,scale等。。。
當人dismissed也是可以這樣做哦
Push / Pop
我們NavigationController的rootViewController里面的Push按鈕push出PushViewController
并且遵循UINavigationControllerDelegate別忘記設置代理哦
return寫好的UIViewControllerAnimatedTransitioning運行程序,是不是很酷。。。
好了大功告成了。當然你要是用的是SB的話在記得prepareForSegue這個方法哦。
是不是很簡單。