如何使用 TouchesMoved 旋转 UIImageView [英] How to rotate an UIImageView using TouchesMoved

查看:24
本文介绍了如何使用 TouchesMoved 旋转 UIImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

在研究如何旋转 UIImageView 之后:如何才能我将 UIImageView 旋转了 20 度.我用我开发 iPhone 应用程序的简单方法尝试了以下代码:

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)

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

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

这篇关于如何使用 TouchesMoved 旋转 UIImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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