gpt4 book ai didi

ios - NSLayoutConstraint 不使用 UIView.animate 设置动画

转载 作者:行者123 更新时间:2023-11-28 14:37:31 27 4
gpt4 key购买 nike

我试图通过更改 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 在移动时设置动画?

最佳答案

你需要重新布局父 View

func animate(){
centered = !centered
self.top?.constant = -500

UIView.animate(withDuration: 3) {
self.view.layoutIfNeeded()
}
}

关于ios - NSLayoutConstraint 不使用 UIView.animate 设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50764102/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com