[英]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) 我正在尝试使用Touch事件旋转UIImageView
,我希望图像可以用手指旋转,我希望这样做是一样的: dropbox链接 (最好看一下,所以你明白我的意思了)
After researching on how to rotate an UIImageView: How can I rotate an UIImageView by 20 degrees . 研究了如何旋转UIImageView之后: 如何将UIImageView旋转20度 。 I have tried the following code with my simple approach to developing iPhone apps: 我已经用开发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)})
}
}
}
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. 因此,也许有更好的方法也许可以通过使用CoreAnimation来做到这一点,如果代码示例能够达到我想要的效果,我将不胜感激。
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 //更新了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)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.