简体   繁体   中英

How to rotate an UIImageView using TouchesMoved

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.

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

在此处输入图片说明

//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)


    }



}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM