UIview 起始中心看似不正确。 (虚假视觉)

UIview starting center is deceptively incorrect. (False Visual)

视频解说:

我有一个 UIView,您可以将其拖动到另一个视图的中心以执行操作。这个视图应该有一个来自中心视图的前导约束 + 24。

我在较大的 iphones (iphone 8) 上打印的起始中心 X 值是 ~324。

在 iphone 5s 和其他一些较小的手机上,视图似乎放置在正确的位置,但是一旦我移动视图,它就会弹回到不正确的位置。 .ended PanGesture 将使其捕捉到其起始中心 X 值。

我将其起始值打印为 324,但是一旦我移动它,它就会捕捉到屏幕右侧的 324。

这是什么原因造成的?为什么它一开始就出现在正确的位置?

下方 Checkbutton 的 Pangesture:

@objc func checkButtonWasDragged(_ sender: UIPanGestureRecognizer) {

    if sender.state == UIGestureRecognizerState.began || sender.state == UIGestureRecognizerState.changed {
        self.view.bringSubview(toFront: checkButton)
        let translation = sender.translation(in: self.view)
        checkButton.center = CGPoint(x: checkButton.center.x + translation.x, y: checkButton.center.y)
        print(checkButton.center.x)
        distanceCounter += translation.x
        if distanceCounter > 0 {
            distanceCounter = 0
        }
        if checkButton.center.x > startingPointCheckBtn {
            checkButton.center.x = startingPointCheckBtn
        } else if checkButton.center.x < largeCircle.center.x && distanceCounter < checkBtnDistance{
            checkButton.center.x = largeCircle.center.x
            distanceCounter = checkBtnDistance
        } else {
            sender.setTranslation(CGPoint.zero, in: self.view)
        }
        // hide the buttons after we drag over them
        if distanceCounter < -60.0 && !rightCirclesHidden {
            for circle in rightCircles {
                circle.layer.removeAllAnimations()
                circle.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 0)
            }
            rightCirclesHidden = true
        } else if distanceCounter > -60.0 && rightCirclesHidden {
            for i in 0..<leftCircles.count {
                leftCircles[i].layer.removeAllAnimations()
                //rightCircles[i].backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 0)
                rightCircles[i].backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
                leftCircles[i].backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
            }
            startAnimatingCircles()
            rightCirclesHidden = false
        }
    } else if sender.state == UIGestureRecognizerState.ended {
        if largeCircle.center.x - checkButton.center.x >= -35.0 {
            checkButton.center = largeCircle.center
            self.loadSignUpPage()
        } else {
            checkButton.center.x = startingPointCheckBtn
            self.view.layoutIfNeeded()
            print(startingPointCheckBtn)
            if rightCirclesHidden {
                for i in 0..<leftCircles.count {
                    leftCircles[i].layer.removeAllAnimations()
                    //rightCircles[i].backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 0)
                    rightCircles[i].backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
                    leftCircles[i].backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
                }
                startAnimatingCircles()
                rightCirclesHidden = false
            }
        }
        distanceCounter = 0
    }
}

greenCheck 的限制条件:

viewDidLoad() 正在调用:

setUpCheckButton()

setUpCheckButton代码:

    func setUpCheckButton() {
    startingPointCheckBtn = checkButton.center.x
    print("check circle center: " + "\(startingPointCheckBtn)")
    checkBtnDistance = largeCircle.center.x - startingPointCheckBtn
    let checkGestureDrag = UIPanGestureRecognizer(target: self, action: #selector(AdViewPageVC.checkButtonWasDragged(_:)))
    checkButton.addGestureRecognizer(checkGestureDrag)
}

与其将其设置在 viewDidLoad 中,不如将其设置在 viewDidAppear 中,以便当复选标记出现在屏幕上时您可以获得实际值。同时将您的手势结束块更改为此。

UIView.animate(withDuration: 0.70, delay: 0, options: .curveEaseOut, animations: {
      self.checkButton.center.x = self.startingPointCheckBtn
      self.view.layoutIfNeeded()
}, completion: nil)