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

标签 ios swift uiview autolayout uipangesturerecognizer

视频说明: Example

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

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

在 iphone 5s 和其他一些较小的手机上, View 似乎放置在正确的位置,但一旦我移动 View ,它就会弹回到不正确的位置。 .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 的限制: 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)
}

最佳答案

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

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

关于ios - UIview 起始中心看似不正确。 (虚假视觉),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51551002/

相关文章:

iphone - 仅以编程方式为不在 AppDelegate 中的一个 ViewController 创建 TabBar

xcode - Swift 中的 If else 语句

ios - 有没有办法让标签中的文本自动更新显示

ios - 在包含 View block 上点击手势 tableView 选择

ios - UIImage.pngData(_ :) losing Camera photo orientation

ios - 动画后移除 UIView 不丢失其他元素

ios - 如何从 UIWebView 获取 URL 的哈希片段

在结构中存储对象时出现 Swift 问题

iphone - iOS 5 UIView drawRect 覆盖在设备上不起作用

iphone - 如何修改现有的 UIViewController 以允许滚动?