简体   繁体   中英

UIview starting center is deceptively incorrect. (False Visual)

Video Explanation: 例

I have a UIView that you can drag to the center of another view to perform an action. This view is supposed to have a leading constraint from the center view + 24.

Its starting center X value I print on larger iphones (iphone 8) is ~324.

On iphone 5s and some other smaller phones, the view appears to be placed at the correct location but once I move the view it is snaps back to an incorrect position. The .ended PanGesture will have it snap to its starting center X value.

I print its starting value at 324 but once I move it it snaps to 324 which is to the right off screen.

What is causing this? Why does it appear to be in a correct place at start?

Pangesture for Checkbutton below:

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

Constraints on the greenCheck: 检查约束

viewDidLoad() is calling:

setUpCheckButton()

setUpCheckButton code:

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

Instead of setting it in viewDidLoad you should set it in viewDidAppear so that you get the actual value when the checkMark is on screen. Also change your gesture ended block to this.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM