视图通过平移手势在旋转时奇怪地移动 [英] View moves weirdly on rotation through pan gesture

查看:57
本文介绍了视图通过平移手势在旋转时奇怪地移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在按钮上有一个平移手势,可以将按钮向平移方向移动.当翻译足够远"时,按钮会旋转.在此旋转中,可见按钮将转到其初始位置或关闭,然后返回应有的位置.无法弄清楚发生了什么.这是我的代码:

I have a pan gesture on a button that moves the button in the direction of the pan. When the translation is "far enough" the button gets rotated. On this rotation the button visible goes to its initial position or close and then back where it should be. Cannot figure out what is going on. This is my code:

    @IBAction func didPanButton(_ sender: UIPanGestureRecognizer) {

    print("Did pan button", sender.translation(in: sender.view).x)

    answerButton.center.x = view.center.x + sender.translation(in: sender.view).x

    switch buttonPosition {
    case .left:
        print("Rotate button left")
        UIView.animate(withDuration: 0.5, animations: {
            self.rotateButtonLeft()
        })
    case .right:
        print("Rotate button right")
        UIView.animate(withDuration: 0.5, animations: {
            self.rotateButtonRight()
        })
    case .middle:
        print("Remove button rotation")
        UIView.animate(withDuration: 0.5, animations: {
            self.removeButtonRotation()
        })
    }

} 

var buttonPosition : ButtonPosition {
    let leftWidth = (view.frame.size.width - answerButton.frame.size.width) / 2

    if answerButton.frame.origin.x < leftWidth / 2 {
        return .left
    }

    if answerButton.frame.origin.x > leftWidth * 3 / 2 {
        return .right
    }

    return .middle
}

func rotateButtonLeft() {
    let degrees : CGFloat = -10; //the value in degrees
    answerButton.transform = CGAffineTransform(rotationAngle: degrees * CGFloat.pi/180);
    let image = UIImage(named: "item_no")
    answerButton.setImage(image, for: UIControlState.normal)
}

func rotateButtonRight() {
    let degrees : CGFloat = 10; //the value in degrees
    answerButton.transform = CGAffineTransform(rotationAngle: degrees * CGFloat.pi/180);
    let image = UIImage(named: "item_yes")
    answerButton.setImage(image, for: UIControlState.normal)
}

func removeButtonRotation() {
    let degrees : CGFloat = 0; //the value in degrees
    answerButton.transform = CGAffineTransform(rotationAngle: degrees * CGFloat.pi/180);
    let image = UIImage(named: "item_neutral")
    answerButton.setImage(image, for: UIControlState.normal)
}

推荐答案

您的按钮具有设置其位置的约束.当自动布局运行时,它会根据这些约束重置按钮的 center.设置按钮的图像会触发自动布局.

Your button has constraints to set its position. When auto layout runs, it resets the button's center according to those constraints. Setting the button's image is triggering auto layout.

您可以通过三个选项来解决此问题:

You have three options to fix this:

  1. 通过更新约束来改变按钮的位置,或者
  2. 通过设置按钮的 transform 而不是改变它的 center 来改变按钮的位置,或者
  3. 不要使用约束来定位按钮.
  1. Change the button's position by updating the constraints, OR
  2. Change the button's position by setting its transform instead of by changing its center, OR
  3. Don't use constraints to position the button.

这篇关于视图通过平移手势在旋转时奇怪地移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆