我有一个UIView,我想在屏幕上制作动画,然后将另一个视图带入屏幕。我正在使用自动布局,我为左右NSLayoutConstraint设置了IBOutlet。它似乎没有为UIView设置宽度,视图实际上没有在屏幕上显示动画。
我试图使用此代码关闭UIView:
self.gameViewConstraintLeft.constant = -400
UIView.animateWithDuration(1, animations: { () in
self.view.layoutIfNeeded()
})
self.gameViewConstriantRight.constant = -400
UIView.animateWithDuration(1, animations: { () in
self.view.layoutIfNeeded()
})
我在顶部(0),左(0)和右(0)设置了约束。也许这是我的问题,我将视图固定在屏幕的边缘,但我希望这可以在多种屏幕尺寸上工作,所以我不确定实现目标的最佳方法。 / p>
答案 0 :(得分:3)
如果将视图水平或垂直固定在多个侧面,您将无法移动视图。
我建议如下:
为两个视图设置width,height,centerX和centerY约束。可以使用superview将宽度和高度设置为“Equal Widths”和“Equal Heights”,以允许它使用多种屏幕尺寸。
将IBOutlet
添加到centerX约束中。通过修改此约束的constant
属性进行动画处理:
self.gameView1ConstraintCenterX.constant = -400
UIView.animateWithDuration(1) {
self.view.layoutIfNeeded()
}
self.gameView2ConstriantCenterX.constant = 0
UIView.animateWithDuration(1) {
self.view.layoutIfNeeded()
}