无法为具有子视图控制器

时间:2017-04-06 23:47:33

标签: ios swift cocoa-touch uikit

我在self.view中有两个视图,它们是容器视图(通过故事板:每个视图在viewDidLoad中都有一个嵌入segue,用于加载各自的子视图控制器)。他们称之为self.leftPane和self.rightPane。我正在使用自动布局。

我想为self.leftPane的位置变化设置动画,因为它从x = 0移动到x =(负面的东西)。我基本上试图以简单的方式在左边做一个汉堡包菜单......

@IBOutlet weak var leftPane: UIView! // has child view controller embedded
@IBOutlet weak var rightPane: UIView! // has child view controller embedded

        // in/after viewDidAppear
        self.leftPaneLeftConstraint.constant = 0 - self.leftPane.frame.size.width + 20.0

        UIView.transition(with: self.leftPane, duration: 1.0, options: [], animations: {
            self.leftPane.layoutIfNeeded()
        }, completion: { (finished) in
            self.leftPaneIsFull = false
        })

问题在于没有动画 - 它只是跳跃。 我尝试过不同视图的UIView.transition和不同的UIView.animate风格,所有这些都具有相同的效果(没有实际动画)。

我错过了什么?谢谢!

1 个答案:

答案 0 :(得分:3)

你应该使用UIView.animate而不是UIView.transition.I写下代码示例。你试试这段代码吗?

 @IBOutlet weak var leftPane: UIView! // has child view controller 
  embedded
 @IBOutlet weak var rightPane: UIView! // has child view controller embedded

   // in/after viewDidAppear
   UIView.animate(withDuration: 1.0, animations: {

        self.leftPaneLeftConstraint.constant = 0 - self.leftPane.frame.size.width + 20.0
        self.view.layoutIfNeeded()
    }, completion: { (success) -> Void in

           //You can write something you want

           })
        }
    })

享受它:)