自定义转换

时间:2017-07-26 21:53:27

标签: ios swift

我有一个导航控制器,在导航控制器里面我有一个主屏幕,从主屏幕我点击一个按钮进入另一个屏幕。

但是使用导航控制器的标准节目动画是它从侧面滑动,但我想要做的是视图控制器从屏幕底部向上滑动并在到达屏幕时创建一种弹跳动画顶部。

1 个答案:

答案 0 :(得分:0)

任何想要使用自定义转换两件事来记住UIViewControllerAnimatedTransitioningUIViewControllerTransitioningDelegate协议的人。现在,在UIViewControllerAnimatedTransitioning继承自NSObject

的自定义类中符合import UIKit class CustomPushAnimation: NSObject, UIViewControllerAnimatedTransitioning { func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval { return 0.2 } func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { let containerVw = transitionContext.containerView let fromViewController = transitionContext.viewController(forKey: .from) let toViewController = transitionContext.viewController(forKey: .to) guard let fromVc = fromViewController, let toVc = toViewController else { return } let finalFrame = transitionContext.finalFrame(for: toVc) //For different animation you can play around this line by changing frame toVc.view.frame = finalFrame.offsetBy(dx: 0, dy: finalFrame.size.height/2) containerVw.addSubview(toVc.view) UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: { toVc.view.frame = finalFrame fromVc.view.alpha = 0.5 }, completion: {(finished) in transitionContext.completeTransition(finished) fromVc.view.alpha = 1.0 }) } }
import UIKit
class YourViewController: UIViewController {
lazy var customPushAnimation: CustomPushAnimation = {
        return CustomPushAnimation()
    }()
func openViewControler() {
}let vc =//Assuming your view controller which you want to open
let navigationController = UINavigationController(rootViewController: vc)
//Set transitioningDelegate to invoke protocol method
 navigationController.transitioningDelegate = self
present(navigationController, animated: true, completion: nil)
}
  

上述方法将处理动画。之后创建   上面的对象和在yourViewController类中的使用

UIViewControllerTransitioningDelegate
  

注意:为了看动画。永远不要将动画标志设置为false   同时展示ViewController。否则你的动画会   永远不会工作。

最后在YourViewcontroller class

中实现extension YourViewController: UIViewControllerTransitioningDelegate { func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? { return customPushAnimation } 协议方法
mymodel <- multinom(job~height, data = dataset)
pred <- predict(mymodel,dataset,type = 'prob')
roc_pred <- prediction(pred,dataset$job)
roc <- performance(roc_pred,"tpr","fpr")
plot(roc,colorize=T) 
  

每当你提出上面的viewcontroller协议方法时都会   调用,你的动画魔术就会出现。