【问题标题】:UIview starting center is deceptively incorrect. (False Visual)UIview 起始中心看似不正确。 (虚假视觉)
【发布时间】:2018-07-27 04:57:29
【问题描述】:

视频说明:

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

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

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

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

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

下面的Checkbutton的Pagesture:

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

【问题讨论】:

  • 请显示 panGesture 的代码和视图的 te 约束。
  • 哇,这是很多检查。但看到你需要什么。如果您将checkButton 的起始位置存储在startingPointCheckButton 中,它应该可以工作。检查起始位置是否确实存储在该变量中。另外我建议你对动画使用自动布局约束。
  • 也可以使用UIView.animate 让你的复选标记回到原来的位置。
  • 请在viewDidAppear 中显示该代码。我想你可能使用了 frame 而不是 bounds 什么的。
  • 你能重新加入聊天吗?

标签: ios swift uiview autolayout uipangesturerecognizer


【解决方案1】:

您应该将其设置为viewDidAppear,而不是在viewDidLoad 中设置,这样当复选标记出现在屏幕上时,您就可以获得实际值。还将您的手势结束块更改为此。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-07
    • 2011-07-24
    • 2015-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    相关资源
    最近更新 更多