0

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

4

1 回答 1

1

您需要重新布局超级视图

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

    UIView.animate(withDuration: 3) {
        self.view.layoutIfNeeded()
    }
}
于 2018-06-08T15:42:34.647 回答