UIViewControllerAnimatedTransitioning 未触发
UIViewControllerAnimatedTransitioning not firing
正在进行 simpel 转换,但没有被调用我没有得到我错过的东西here.Can任何人帮助我。没有收到任何错误或警告。
动画Class-:
import UIKit
class CustomPresentAnimationController: NSObject,UIViewControllerAnimatedTransitioning {
public func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval
{
return 2.5
}
// This method can only be a nop if the transition is interactive and not a percentDriven interactive transition.
public func animateTransition(using transitionContext: UIViewControllerContextTransitioning)
{
let fromViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)
let toViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)
let finalViewcontroller = transitionContext.finalFrame(for: toViewController!)
let bounds = UIScreen.main.bounds
toViewController?.view.frame = finalViewcontroller.offsetBy(dx: 0, dy: bounds.size.height)
print(transitionDuration(using: transitionContext))
let containerView = transitionContext.containerView
containerView.addSubview((toViewController?.view)!)
UIView.animate(withDuration: transitionDuration(using: transitionContext),delay: 0.1,options: UIViewAnimationOptions.curveEaseIn,animations: { () -> Void in
fromViewController?.view.alpha = 0.5
toViewController?.view.frame = finalViewcontroller
},completion: { (finished) -> Void in
transitionContext.completeTransition(true)
fromViewController?.view.alpha = 1.0
})
}
}
从视图控制器-:
import UIKit
class ItemsTableViewController: UITableViewController,UIViewControllerTransitioningDelegate {
override func viewDidLoad() {
super.viewDidLoad()
}
let customPresentAnimationController = CustomPresentAnimationController()
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showAction" {
let toViewController = segue.destination as! ActionViewController
toViewController.transitioningDelegate = self
}
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell", for: indexPath) as UITableViewCell
cell.textLabel?.text = "Item 0\(indexPath.row + 1)"
return cell
}
func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return customPresentAnimationController
}
}
ToViewcontroller-:
import UIKit
class ActionViewController: UIViewController {
@IBAction func dismiss(_ sender: UIButton) {
self.dismiss(animated: true, completion: nil)
}
}
看起来你有 copy/pasted 方法的 swift 2 版本,现在签名不同了:
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return Transition()
}
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return Transition()
}
编辑:也可能最好 return 一个新的过渡实例并去掉顶部的 let
。
正在进行 simpel 转换,但没有被调用我没有得到我错过的东西here.Can任何人帮助我。没有收到任何错误或警告。
动画Class-:
import UIKit
class CustomPresentAnimationController: NSObject,UIViewControllerAnimatedTransitioning {
public func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval
{
return 2.5
}
// This method can only be a nop if the transition is interactive and not a percentDriven interactive transition.
public func animateTransition(using transitionContext: UIViewControllerContextTransitioning)
{
let fromViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)
let toViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)
let finalViewcontroller = transitionContext.finalFrame(for: toViewController!)
let bounds = UIScreen.main.bounds
toViewController?.view.frame = finalViewcontroller.offsetBy(dx: 0, dy: bounds.size.height)
print(transitionDuration(using: transitionContext))
let containerView = transitionContext.containerView
containerView.addSubview((toViewController?.view)!)
UIView.animate(withDuration: transitionDuration(using: transitionContext),delay: 0.1,options: UIViewAnimationOptions.curveEaseIn,animations: { () -> Void in
fromViewController?.view.alpha = 0.5
toViewController?.view.frame = finalViewcontroller
},completion: { (finished) -> Void in
transitionContext.completeTransition(true)
fromViewController?.view.alpha = 1.0
})
}
}
从视图控制器-:
import UIKit
class ItemsTableViewController: UITableViewController,UIViewControllerTransitioningDelegate {
override func viewDidLoad() {
super.viewDidLoad()
}
let customPresentAnimationController = CustomPresentAnimationController()
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showAction" {
let toViewController = segue.destination as! ActionViewController
toViewController.transitioningDelegate = self
}
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell", for: indexPath) as UITableViewCell
cell.textLabel?.text = "Item 0\(indexPath.row + 1)"
return cell
}
func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return customPresentAnimationController
}
}
ToViewcontroller-:
import UIKit
class ActionViewController: UIViewController {
@IBAction func dismiss(_ sender: UIButton) {
self.dismiss(animated: true, completion: nil)
}
}
看起来你有 copy/pasted 方法的 swift 2 版本,现在签名不同了:
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return Transition()
}
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return Transition()
}
编辑:也可能最好 return 一个新的过渡实例并去掉顶部的 let
。