斯威夫特 - 像#34;推动"手动添加视图时

时间:2016-08-14 05:28:10

标签: swift swift2

我有一个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)

这个工作正常,但我想添加一个动画,所以它看起来像" Push"在NavigationController中。

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

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

有任何线索吗?

1 个答案:

答案 0 :(得分:0)

查看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()