UIview启动中心看似不正确。 (虚假视觉)

时间:2018-07-27 04:57:30

标签: ios swift uiview autolayout uipangesturerecognizer

视频说明: Example

我有一个UIView,您可以将其拖动到另一个视图的中心以执行操作。从中心视图+ 24开始,该视图应该具有领先约束。

我在较大的iPhone(iPhone 8)上打印的起始中心X值为〜324。

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

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

是什么原因造成的?为什么在开始时它似乎在正确的位置?

下面的Checkbutton手势

@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
    }
}

绿色检查的约束: Check Constraints

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)
}

1 个答案:

答案 0 :(得分:1)

应该在viewDidLoad中进行设置,而不是在viewDidAppear中进行设置,以便在屏幕上显示选中标记时获得实际值。还将您的手势结束块更改为此。

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