NSLayoutConstraint 没有使用 UIView.animate 设置动画
NSLayoutConstraint not animating with UIView.animate
我试图通过更改 NSLayoutConstriant 的常量使 UIView 在屏幕上向上移动。 UIView 移动但移动时没有动画。
初始约束
func setUpView(){
cont = OverlayController()
let overlay = (cont?.view!)!
gameView.addSubview(overlay)
top = overlay.topAnchor.constraint(equalTo: gameView.bottomAnchor, constant: -175)
centerX = overlay.centerXAnchor.constraint(equalTo: gameView.centerXAnchor)
width = overlay.widthAnchor.constraint(equalToConstant: gameView.frame.width * 0.7)
height = overlay.heightAnchor.constraint(equalToConstant: gameView.frame.width * 0.7)
centerY = overlay.centerYAnchor.constraint(equalTo: gameView.centerYAnchor)
top?.isActive = true
centerX?.isActive = true
width?.isActive = true
height?.isActive = true
let swipeListener = UISwipeGestureRecognizer(target: self, action: #selector(swiped))
swipeListener.direction = .up
overlay.addGestureRecognizer(swipeListener)
}
动画方法(运行 向上滑动)
func animate(){
centered = !centered
cont?.view.layoutIfNeeded()
self.top?.constant = -500
UIView.animate(withDuration: 3) {
self.cont?.view.layoutIfNeeded()
}
}
如何才能让正在动画的 UIView 在移动时动画?
您需要重新布局超级视图
func animate(){
centered = !centered
self.top?.constant = -500
UIView.animate(withDuration: 3) {
self.view.layoutIfNeeded()
}
}
我试图通过更改 NSLayoutConstriant 的常量使 UIView 在屏幕上向上移动。 UIView 移动但移动时没有动画。
初始约束
func setUpView(){
cont = OverlayController()
let overlay = (cont?.view!)!
gameView.addSubview(overlay)
top = overlay.topAnchor.constraint(equalTo: gameView.bottomAnchor, constant: -175)
centerX = overlay.centerXAnchor.constraint(equalTo: gameView.centerXAnchor)
width = overlay.widthAnchor.constraint(equalToConstant: gameView.frame.width * 0.7)
height = overlay.heightAnchor.constraint(equalToConstant: gameView.frame.width * 0.7)
centerY = overlay.centerYAnchor.constraint(equalTo: gameView.centerYAnchor)
top?.isActive = true
centerX?.isActive = true
width?.isActive = true
height?.isActive = true
let swipeListener = UISwipeGestureRecognizer(target: self, action: #selector(swiped))
swipeListener.direction = .up
overlay.addGestureRecognizer(swipeListener)
}
动画方法(运行 向上滑动)
func animate(){
centered = !centered
cont?.view.layoutIfNeeded()
self.top?.constant = -500
UIView.animate(withDuration: 3) {
self.cont?.view.layoutIfNeeded()
}
}
如何才能让正在动画的 UIView 在移动时动画?
您需要重新布局超级视图
func animate(){
centered = !centered
self.top?.constant = -500
UIView.animate(withDuration: 3) {
self.view.layoutIfNeeded()
}
}