【问题标题】:NSLayoutConstraint not animating with UIView.animateNSLayoutConstraint 不使用 UIView.animate 进行动画处理
【发布时间】:2018-06-08 15:39:51
【问题描述】:

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

【问题讨论】:

    标签: ios swift animation nslayoutconstraint uiviewanimation


    【解决方案1】:

    你需要重新布局superview

    func animate(){
        centered = !centered
        self.top?.constant = -500 
    
        UIView.animate(withDuration: 3) {
            self.view.layoutIfNeeded()
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-23
      • 2013-02-16
      • 1970-01-01
      相关资源
      最近更新 更多