自定义视图控制器转换 xcode8
Custom View Controller Transitions xcode8
我正在尝试在此 tutorial 之后进行自定义视图转换。这是我的代码
class ItemsTableViewController: UITableViewController, UIViewControllerTransitioningDelegate {
let customPresentAnimationController = CustomPresentAnimationController()
// viewDidLoad and TableView methods
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showAction" {
let toViewController = segue.destination as UIViewController
toViewController.transitioningDelegate = self
toViewController.modalPresentationStyle = .custom
}
}
func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return customPresentAnimationController
}
}
以及 CustomPresentAnimationController
class CustomPresentAnimationController: NSObject, UIViewControllerAnimatedTransitioning {
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 5
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let fromViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)!
let toViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)!
let finalFrameForVC = transitionContext.finalFrame(for: toViewController)
let containerView = transitionContext.containerView
let bounds = UIScreen.main.bounds
toViewController.view.frame = finalFrameForVC.offsetBy(dx: 0, dy: bounds.size.height)
containerView.addSubview(toViewController.view)
UIView.animate(withDuration: transitionDuration(using: transitionContext), delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.0, options: .curveLinear, animations: {
fromViewController.view.alpha = 0.5
toViewController.view.frame = finalFrameForVC
}, completion: {
finished in
transitionContext.completeTransition(true)
fromViewController.view.alpha = 1.0
})
}
}
但是自定义转换不起作用。
问题是没有调用 animationControllerForPresentedController 方法。
我该如何解决这个问题?
中swift3方法
func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return customPresentAnimationController
}
已替换为
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return customPresentAnimationController
}
我正在尝试在此 tutorial 之后进行自定义视图转换。这是我的代码
class ItemsTableViewController: UITableViewController, UIViewControllerTransitioningDelegate {
let customPresentAnimationController = CustomPresentAnimationController()
// viewDidLoad and TableView methods
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showAction" {
let toViewController = segue.destination as UIViewController
toViewController.transitioningDelegate = self
toViewController.modalPresentationStyle = .custom
}
}
func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return customPresentAnimationController
}
}
以及 CustomPresentAnimationController
class CustomPresentAnimationController: NSObject, UIViewControllerAnimatedTransitioning {
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 5
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let fromViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)!
let toViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)!
let finalFrameForVC = transitionContext.finalFrame(for: toViewController)
let containerView = transitionContext.containerView
let bounds = UIScreen.main.bounds
toViewController.view.frame = finalFrameForVC.offsetBy(dx: 0, dy: bounds.size.height)
containerView.addSubview(toViewController.view)
UIView.animate(withDuration: transitionDuration(using: transitionContext), delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.0, options: .curveLinear, animations: {
fromViewController.view.alpha = 0.5
toViewController.view.frame = finalFrameForVC
}, completion: {
finished in
transitionContext.completeTransition(true)
fromViewController.view.alpha = 1.0
})
}
}
但是自定义转换不起作用。 问题是没有调用 animationControllerForPresentedController 方法。 我该如何解决这个问题?
中swift3方法
func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return customPresentAnimationController
}
已替换为
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return customPresentAnimationController
}