【问题标题】:Swift - Animate like "Push" when adding manually a viewSwift - 手动添加视图时像“推送”一样动画
【发布时间】:2023-03-22 09:47:01
【问题描述】:

我有一个ViewController用一个“mainView”,在选择段元素时,我正在加载一些ViewControllers

   let storyboard = UIStoryboard(name: "Main", bundle: nil)
    self.vc = storyboard.instantiateViewControllerWithIdentifier("DashboardDetail")

    self.vc.view.frame = self.mainView.bounds
    self.mainView.addSubview(self.vc.view)

    self.addChildViewController(self.vc)
    vc.didMoveToParentViewController(self)

这很好用,但我想添加一个动画,使它看起来像 NavigationController 中的“推送”。

我还应该添加相同的动画,以便在删除视图时使其消失。这是我用于删除的代码。

self.vc.willMoveToParentViewController(nil)
        self.vc.view.removeFromSuperview()
        self.vc.removeFromParentViewController()

有什么线索吗?

【问题讨论】:

    标签: swift swift2


    【解决方案1】:

    查看UIView.transitionWithView 进行转换。

    func animate() {
    let opts : UIViewAnimationOptions = .TransitionFlipFromLeft
    UIView.transitionWithView(self.imView, duration: 1, options: opts,
                              animations: {
                                self.imView.image = UIImage(named:"play-button.png")
        }, completion: nil)
    

    }

    你也可以试试这样的。

    extension UIView {
    
    func slidePush(duration: NSTimeInterval = 1.0, completionDelegate: AnyObject? = nil) {
        // Create a CATransition
        let slidePushTransition = CATransition()
    
        // Set its callback delegate to the completionDelegate
        if let delegate: AnyObject = completionDelegate {
            slidePushTransition.delegate = delegate
        }
    
        // animation's properties
        slidePushTransition.type = kCATransitionPush
        slidePushTransition.subtype = kCATransitionFromTop
        slidePushTransition.duration = duration
        slidePushTransition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
        slidePushTransition.fillMode = kCAFillModeRemoved
    
        // Add it to the View's layer
        self.layer.addAnimation(slidePushTransition, forKey: "slidePushTransition")} }
    

    并使用扩展名self.view.slidePush()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-16
      • 2018-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-07
      • 2011-01-21
      相关资源
      最近更新 更多