import UIKit
class Popoveranimator: NSObject {
//MARK:- 對外提供的屬性
var presentedFrame : CGRect = CGRect.zero
var isPresented : Bool = false
var callBack : ((Bool) -> ())?
//MARK:- 自定義構(gòu)造函數(shù)
init(callBack : @escaping (Bool) -> ()) {
self.callBack = callBack
}
}
//MARK:- 自定義轉(zhuǎn)場代理的方法
extension Popoveranimator : UIViewControllerTransitioningDelegate {
//目的:改變彈出view的尺寸
func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
let presentation = PoppresentationController(presentedViewController: presented, presenting: presenting)
presentation.presentedFrame = presentedFrame
return presentation
}
//目的:自定義彈出的動畫
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
isPresented = true
callBack!(isPresented)
return self
}
//目的:自定義消失的動畫
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
isPresented = false
callBack!(isPresented)
return self
}
}
//MARK:- 彈出和消失動畫代理的方法
extension Popoveranimator : UIViewControllerAnimatedTransitioning {
//動畫執(zhí)行的時間
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.5
}
//獲取轉(zhuǎn)場的上下文:可以通過轉(zhuǎn)場上下文獲取彈出的view和消失的view
//UItransitionContextFromViewKey : 獲取消失的view
//UITransitionContextToViewKey : 獲取彈出的View
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
isPresented ? animationForPresentedView(using: transitionContext) : animationForDismissedView(using: transitionContext)
}
//自定義彈出動畫
private func animationForPresentedView(using transitionContext: UIViewControllerContextTransitioning) {
//獲取彈出的view
let presentedView = transitionContext.view(forKey: .to)!
//將彈出的view添加到containerView
transitionContext.containerView.addSubview(presentedView)
//執(zhí)行動畫
presentedView.transform = CGAffineTransform(scaleX: 1.0, y: 0.0)
presentedView.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
presentedView.transform = CGAffineTransform.identity
}) { (_) in
//必須告訴轉(zhuǎn)場上下文你已經(jīng)完成動畫
transitionContext.completeTransition(true)
}
}
//自定義消失動畫
private func animationForDismissedView(using transitionContext: UIViewControllerContextTransitioning) {
//獲取彈出的view
let dismissedView = transitionContext.view(forKey: .from)!
//執(zhí)行動畫
UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
dismissedView.transform = CGAffineTransform(scaleX: 1.0, y: 0.001)
}) { (_) in
//必須告訴轉(zhuǎn)場上下文你已經(jīng)完成動畫
transitionContext.completeTransition(true)
}
}
}
import UIKit
class PhotoBrowserAnimator: NSObject {
var isPresented : Bool = false
}
extension PhotoBrowserAnimator : UIViewControllerTransitioningDelegate {
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
isPresented = true
return self
}
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
isPresented = false
return self
}
}
extension PhotoBrowserAnimator : UIViewControllerAnimatedTransitioning {
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.5
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
isPresented ? animationForPresentedView(using: transitionContext) : animationForDismissView(using: transitionContext)
}
//自定義彈出動畫
func animationForPresentedView(using transitionContext: UIViewControllerContextTransitioning) {
//取出彈出的view
let presentedView = transitionContext.view(forKey: UITransitionContextViewKey.to)!
//將presentedView添加到containerView中
transitionContext.containerView.addSubview(presentedView)
//執(zhí)行動畫
presentedView.alpha = 0
UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
presentedView.alpha = 1.0
}) { (_) in
transitionContext.completeTransition(true)
}
}
//自定義消失動畫
func animationForDismissView(using transitionContext: UIViewControllerContextTransitioning) {
//取出消失的view
let dismissView = transitionContext.view(forKey: UITransitionContextViewKey.from)!
//執(zhí)行動畫
UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
dismissView.alpha = 0
}) { (_) in
dismissView.removeFromSuperview()
transitionContext.completeTransition(true)
}
}
}