ios - 无法为具有 subview Controller 的两个容器 UIView 的位置更改设置动画

标签 ios swift cocoa-touch uikit

我在 self.view 中有两个 View ,它们是容器 View (通过 Storyboard:每个 View 在 viewDidLoad 中都有一个嵌入的 segue,它加载了各自的 subview Controller )。它们称为 self.leftPane 和 self.rightPane。我正在使用自动布局。

我想为 self.leftPane 的位置变化设置动画,因为它从 x=0 移动到 x=(negative something)。我基本上是想以一种简单的方式在左侧制作一个汉堡包菜单...

@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
        })

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

我错过了什么?谢谢!

最佳答案

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

 @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

           })
        }
    })

好好享受吧:)

关于ios - 无法为具有 subview Controller 的两个容器 UIView 的位置更改设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43267508/

相关文章:

swift - Swiftui 中 init() 中的变量前的下划线是什么意思?

ios - Swift 中 TableViewCell 中的 indexPathForRowAtPoint 错误

ios - Xamarin.iOS - 发布应用程序 - 无效的 Swift 支持

ios - 关闭 UIActivityViewController 的衍生 subview

ios - 具有资源的通用静态库

ios - NotificationCenter 在 Swift 中传递数据

ios - 如何使用UISwipeGestureRecognizer检测轻拂手势?

ios - `numberOfItemsInSection` 仅在添加数组中的第一项后调用,将项目限制为只有一个

iphone - 如何使用 UIGraphicsBeginImageContextWithOptions?

objective-c - 后台实时摄像头(ios)