簡(jiǎn)介
本人最近一直在學(xué)習(xí)Swift編程,然后遇到自定義過(guò)場(chǎng)動(dòng)畫(huà)的問(wèn)題,再次做一個(gè)小小的總結(jié),也希望能給同時(shí)也在學(xué)習(xí)Swift的朋友一些幫助。如果想了解OC的自定義過(guò)場(chǎng)動(dòng)畫(huà),推薦閱讀 iOS自定義轉(zhuǎn)場(chǎng)動(dòng)畫(huà)/UIPresentationController,講解的非常細(xì)致,而且有demo代碼。
Demo 下載
如果不想下載請(qǐng)看文章最后的全部代碼
第一部分
首先我們需要?jiǎng)?chuàng)建一個(gè)CustomPresentationManager類(lèi)
成員變量
//用于指定presentationController的frame
var presentedFrame = CGRect.zero
//用于標(biāo)記presentation視圖是否已經(jīng)顯示
var isPresented = false
協(xié)議
CustomPresentationManager類(lèi)需要繼承兩個(gè)協(xié)議分別是:UIViewControllerTransitioningDelegate
UIViewControllerAnimatedTransitioning
在UIViewControllerTransitionDelegate中需要實(shí)現(xiàn)三個(gè)方法,分別是
1.presentationController()
func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController?{
//創(chuàng)建presentationController
let dialog = DialogPresentationController.init(presentedViewController: presented, presenting: presenting)
//設(shè)置Controller中成員變量的值
dialog.presentedFrame = presentedFrame
//返回指定的presentationController
return dialog
}
這個(gè)函數(shù)主要用于指定PresentationController
2.animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning?
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning?{
isPresented = false
NSLog("Show")
return self
}
這個(gè)函數(shù)會(huì)在presented視圖出現(xiàn)的時(shí)候被調(diào)用, 在此函數(shù)中可以進(jìn)行一些需要在presented視圖出現(xiàn)之后進(jìn)行的操作。 需要注意的是在這個(gè)函數(shù)中需要把isPresented置為false,原因稍后說(shuō)明。
- animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?{
isPresented = true
NSLog("Disappear")
return self
}
這個(gè)函數(shù)的作用正好和上一個(gè)相反,在presentation視圖消失的時(shí)候會(huì)被調(diào)用,并在此函數(shù)中把isPresented置為false。
UIViewControllerAnimatedTransitioning協(xié)議主要用于指定transitioning的時(shí)間和動(dòng)畫(huà)的細(xì)節(jié),其中需要實(shí)現(xiàn)兩個(gè)方法。
- transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval{
return 0.8
}
這個(gè)函數(shù)并沒(méi)有什么好說(shuō)的,需要返回transitioning 的時(shí)間。
- animateTransition(using transitionContext: UIViewControllerContextTransitioning)
func animateTransition(using transitionContext: UIViewControllerContextTransitioning){
if isPresented{
disappear(transitionContext: transitionContext)
}else{
showUp(transitionContext: transitionContext)
}
}
這個(gè)函數(shù)需要注意的地方是,在transition開(kāi)始和結(jié)束的時(shí)候,該函數(shù)都會(huì)被調(diào)用,所以這也是上文提到的isPresented變量需要被用到的地方。需要根據(jù)isPresented來(lái)判斷Presentation視圖是否展示,從而指定不同的transition方式。值得一提的是transitionContext這個(gè)變量,transition所需要的東西全部都包含在這個(gè)變量里面。showUp和disappear函數(shù)指定了具體的transition方式,具體細(xì)節(jié)請(qǐng)參考上文中的Demo代碼。
第二部分
這個(gè)部分主要介紹自定義的UIPresentationController里面的一些細(xì)節(jié)和注意點(diǎn)。
這部分的代碼比較少而且很簡(jiǎn)單,就全部粘出來(lái)吧。
import UIKit
class DialogPresentationController: UIPresentationController {
var presentedFrame = CGRect.zero
var isLayout = false
override func containerViewDidLayoutSubviews(){
if !isLayout {
presentedView?.frame = presentedFrame
containerView?.insertSubview(coverbutton, at: 0)
coverbutton.addTarget(self, action: #selector(closeDialog), for: .touchUpInside)
isLayout = true
}
}
func closeDialog(){
presentedViewController.dismiss(animated: true, completion: nil)
}
private lazy var coverbutton: UIButton = {
let btn = UIButton.init(frame: UIScreen.main.bounds)
btn.backgroundColor = UIColor.lightGray
btn.alpha = 0.8
return btn
}()
}
最需要注意的是containerViewDidLayoutSubviews方法在presented視圖展示和消失的時(shí)候都會(huì)被調(diào)用。如果不用isLayout判斷是否layout過(guò)就會(huì)重復(fù)layout,導(dǎo)致一些奇奇怪怪的錯(cuò)誤。
全部代碼:
CustomTransitioningManager
//
// CustomTransition.swift
// AnimationTest
//
// Created by Albert-z on 2017-05-14.
// Copyright ? 2017 Albert-z. All rights reserved.
//
import UIKit
class CustomTransitioningManager: NSObject, UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning {
//用于指定presentationController的frame
var presentedFrame = CGRect.zero
//用于標(biāo)記presentation視圖是否已經(jīng)顯示
var isPresented = false
//MARK:UIViewControllerAnimatedTransitioning
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval{
return 0.8
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning){
if isPresented{
disappear(transitionContext: transitionContext)
}else{
showUp(transitionContext: transitionContext)
}
}
//MARK:UIViewControllerTransitioningDelegate
func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController?{
//創(chuàng)建presentationController
let dialog = DialogPresentationController.init(presentedViewController: presented, presenting: presenting)
//設(shè)置Controller中成員變量的值
dialog.presentedFrame = presentedFrame
//返回指定的presentationController
return dialog
}
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning?{
isPresented = false
NSLog("Show")
return self
}
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?{
isPresented = true
NSLog("Disappear")
return self
}
//MARK:Custom func
func showUp(transitionContext: UIViewControllerContextTransitioning){
let view = transitionContext.view(forKey: .to)
transitionContext.containerView.addSubview(view!)
view?.transform = .init(scaleX: 0.0, y: 0.0)
view?.layer.anchorPoint = .init(x: 0.5, y: 0.5)
UIView.animate(withDuration: 0.5, animations: {
view?.transform = .identity
}) { (_) in
transitionContext.completeTransition(true)
}
}
func disappear(transitionContext: UIViewControllerContextTransitioning){
let view = transitionContext.view(forKey: .from)
view?.transform = .init(scaleX: 1.0, y: 1.0)
UIView.animate(withDuration: 0.5, animations: {
view?.transform = .init(scaleX: 1.0, y: 0.001)
}) { (_) in
transitionContext.completeTransition(true)
}
}
}
DialogPresentationController
//
// DialogPresentationController.swift
// AnimationTest
//
// Created by Albert-z on 2017-05-14.
// Copyright ? 2017 Albert-z. All rights reserved.
//
import UIKit
class DialogPresentationController: UIPresentationController {
var presentedFrame = CGRect.zero
var isLayout = false
override func containerViewDidLayoutSubviews(){
if !isLayout {
presentedView?.frame = presentedFrame
containerView?.insertSubview(coverbutton, at: 0)
coverbutton.addTarget(self, action: #selector(closeDialog), for: .touchUpInside)
isLayout = true
}
}
func closeDialog(){
presentedViewController.dismiss(animated: true, completion: nil)
}
private lazy var coverbutton: UIButton = {
let btn = UIButton.init(frame: UIScreen.main.bounds)
btn.backgroundColor = UIColor.lightGray
btn.alpha = 0.8
return btn
}()
}
ViewController
//
// ViewController.swift
// AnimationTest
//
// Created by Albert-z on 2017-05-13.
// Copyright ? 2017 Albert-z. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
var window: UIWindow?
var manager:CustomTransitioningManager?
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func btnClick(_ sender: Any) {
manager = CustomTransitioningManager.init()
manager?.presentedFrame = CGRect.init(x: UIScreen.main.bounds.width/2 - 150.0, y: UIScreen.main.bounds.height/2 - 100.0, width: 300, height: 150)
let sb = UIStoryboard.init(name: "DialogVC", bundle: nil)
let dialogVC = sb.instantiateInitialViewController()
dialogVC?.modalPresentationStyle = .custom
dialogVC?.transitioningDelegate = manager
present(dialogVC!, animated: true, completion: nil)
}
}