如何使用TouchesMoved旋转UIImageView

时间:2014-08-09 16:06:30

标签: ios uiimageview swift

我正在尝试使用触摸事件旋转UIImageView,我想用我的手指旋转图像,我想做同样的事情:dropbox link(最好看到它,所以你明白我的意思)

在研究如何旋转UIImageView之后:How can I rotate an UIImageView by 20 degrees。我用我开发iPhone应用程序的简单方法尝试了以下代码:

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

我可以清楚地看到我的代码有很多错误和不良做法,而且它也没有按照我的意愿行事。 (参见上面的dropbox链接)

所以也许有更好的方法可以通过使用CoreAnimation来实现这一点,如果代码示例可以按照我想要的那样做,我将不胜感激。

2 个答案:

答案 0 :(得分:3)

我刚才写得这么快。我认为这正是你要找的。我使用彩色视图而不是使用图像,但您可以轻松地用图像替换它。 您可能想要检测视图是否被拖动,以便用户必须按顺序拖动视图以进行旋转,因为当前用户可以拖动任何位置来旋转视图。 (以下代码现在检查此案例)如果您对此有任何疑问,请与我们联系。

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

enter image description here

答案 1 :(得分:1)

//更新了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)


    }



}