How to rotate an UIImageView using TouchesMoved

喜夏-厌秋 提交于 2019-12-08 15:18:52

问题


I am trying to rotate an UIImageView using Touch events, I want the image to rotate with my finger, I want to do the same as this: dropbox link (best to see it, so u understand what I mean)

After researching on how to rotate an UIImageView: How can I rotate an UIImageView by 20 degrees. I have tried the following code with my simple approach to developing iPhone apps:

class ViewController: UIViewController {

    var startpoint:CGPoint!
    var endpoint:CGPoint!

    @IBOutlet var yello:UIImageView

    override func touchesMoved(touches: NSSet!, withEvent event: UIEvent!) {

        let t:UITouch = touches.anyObject() as UITouch
        startpoint = t.locationInView(self.view)
        endpoint = t.previousLocationInView(self.view)

        if t.view == yello {
            var startX = startpoint.x
            var endX = endpoint.x
            yello.center = CGPointMake(yello.center.x, yello.center.y)
            UIView.animateWithDuration(1, animations: { self.yello.transform = CGAffineTransformMakeRotation(startX-endX)})
        }
    }
}

I can see clearly that my code has a lot of mistakes and bad practices, and it also doesn't behave correctly as I want. (see dropbox link above)

So maybe there is a better way to do this perhaps by using CoreAnimation, and I would appreciate if code sample would do the same as I want.


回答1:


I just wrote this real quick. I think it is what you are looking for. Instead of using images I used colored-views but you could easily replace that with an image. You may want to detect if the view was dragged so that the user must drag the view in-order to rotate, because currently the user can drag anywhere to rotate the view. (The code below now checks for this case) Let me know if you have any questions about it.

 class ViewController: UIViewController {
    var myView: UIView!
    override func viewDidLoad() {
        super.viewDidLoad()
        myView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 20))
        myView.center = self.view.center
        myView.backgroundColor = UIColor.redColor()
        self.view.addSubview(myView)
        myView.layer.anchorPoint = CGPoint(x: 1.0, y: 0.5)

        let box = UIView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
        box.backgroundColor = UIColor.blueColor()
        myView.addSubview(box)
    }
    override func touchesMoved(touches: NSSet!, withEvent event: UIEvent!) {
        let touch = touches.anyObject() as UITouch
        if touch.view === myView.subviews[0] {
        let position = touch.locationInView(self.view)
        let target = myView.center
        let angle = atan2(target.y-position.y, target.x-position.x)
        myView.transform = CGAffineTransformMakeRotation(angle)
        }
    }
}




回答2:


//updated code for swift 4

class ViewController: UIViewController {
    var myView: UIView!
    override func viewDidLoad() {
    super.viewDidLoad()
    myView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 20))
    myView.center = self.view.center
    myView.isUserInteractionEnabled = true
    myView.backgroundColor = UIColor.red
    self.view.addSubview(myView)
    myView.layer.anchorPoint = CGPoint(x: 1.0, y: 0.5)
    let box = UIView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
    box.backgroundColor = UIColor.blue
    myView.addSubview(box)

}

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

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

    let touch:UITouch = touches.first!

    if touch.view === myView.subviews[0] {


        let position = touch.location(in: self.view)
        let target = myView.center
        let angle = atan2(target.y-position.y, target.x-position.x)
        myView.transform = CGAffineTransform(rotationAngle: angle)


    }



}


来源:https://stackoverflow.com/questions/25220457/how-to-rotate-an-uiimageview-using-touchesmoved

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!