Swift 4中旋转变换后的错误行为

时间:2018-01-05 16:12:42

标签: swift rotation transform

这是我的代码:

@IBOutlet weak var btnOutlet: UIButton!
var degree = 0
var location = CGPoint(x: 112, y: 308)

override func viewDidLoad() {
    super.viewDidLoad()
}

@IBAction func moveAndSnap(_ sender: UIPanGestureRecognizer) {
    if sender.state == .began || sender.state == .changed {
        let translation = sender.translation(in: sender.view)

        switch degree {
        case 0:
            let changeX = (sender.view?.center.x)! + translation.x
            let changeY = (sender.view?.center.y)! + translation.y
            sender.view?.center = CGPoint(x: changeX, y: changeY)
            sender.setTranslation(CGPoint.zero, in: sender.view)

        case 180:
            let changeX = (sender.view?.center.x)! - translation.x
            let changeY = (sender.view?.center.y)! - translation.y
            sender.view?.center = CGPoint(x: changeX, y: changeY)
            sender.setTranslation(CGPoint.zero, in: sender.view)

        case 360:
            let changeX = (sender.view?.center.x)! + translation.x
            let changeY = (sender.view?.center.y)! + translation.y
            sender.view?.center = CGPoint(x: changeX, y: changeY)
            sender.setTranslation(CGPoint.zero, in: sender.view)

        default:
            break
        }
    }

    if sender.state == .ended {
        if 90 ... 140 ~= Int((sender.view?.frame.origin.x)!) && 285 ... 330 ~= Int((sender.view?.frame.origin.y)!) {
            sender.view?.frame.origin = location
        }

        // PROBLEM starts here. Explanation is under this code
        if btnOutlet.frame.origin == location {
            if degree == 0 || degree == 360 {
                print("Hurra!")
            }
        } else {
            print(degree)
            print(btnOutlet.frame.origin)
        }
    }
}

@IBAction func rotate(_ sender: UIButton) {
    if degree == 360 {
        degree = 180
    } else {
        degree += 180
    }
    UIView.animate(withDuration: 0.2) {
        self.btnOutlet.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi/(180/Double(self.degree))))
    }
}

问题: 每次我将我的btnOutlet旋转到360度并将其框架移动到位置时,下面的代码不起作用。为什么? 有趣的是,当我在下面的'else'中询问时,外包就是位置。我错了什么?

        if btnOutlet.frame.origin == location {
            if degree == 0 || degree == 360 {
                print("Hurra!")
            }
        } else {
            print(btnOutlet.frame.origin)
            print(location)
        }

我该怎么办?

1 个答案:

答案 0 :(得分:0)

如果您开始使用转换,那么保持一致并继续使用转换始终是一个好习惯。请记住转换的顺序。也许你正在寻找类似于:

的平滑
@IBAction func panRecognised(_ gesture: UIPanGestureRecognizer) {
    if gesture.state == .began || gesture.state == .changed{
        guard let targetView = gesture.view else { return }
        let transform =  targetView.transform
        let translation = gesture.translation(in: view)
        targetView.transform = transform.concatenating(CGAffineTransform(translationX: translation.x, y: translation.y))
        gesture.setTranslation(CGPoint(x: 0, y: 0), in: view)
    }
}