旋转与iPhone中的触摸拖动相对应的图像 [英] Rotate the image corresponding to the touch drag in iPhone

查看:66
本文介绍了旋转与iPhone中的触摸拖动相对应的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想顺时针或逆时针旋转图像,以使其与用户触摸拖动相对应。我认为可以通过一些数学和逻辑来完成。

I want to rotate the image in clockwise or anticlockwise direction corresponding to the user touch drag with its speed. I think this can be done with some math and logic. What would a code sample for this look like?

推荐答案

如果目标是iOS 3.2或更高版本,则可以使用 UIRotationGestureRecognizer 。代码看起来像这样:

If you're targeting iOS 3.2 or greater, you can use UIRotationGestureRecognizer. The code would look something like this:

在您的设置方法中( viewDidLoad init 等):

In your setup method (viewDidLoad or init, etc,):

UIRotationGestureRecognizer *rotationGesture = 
  [[UIRotationGestureRecognizer alloc] initWithTarget:self
                                               action:@selector(handleRotate:)];
rotationGesture.delegate = self;
[myImageView addGestureRecognizer:rotationGesture];
[rotationGesture release];

事件处理程序:

- (void)handleRotate:(UIRotationGestureRecognizer *)recognizer {
  if(recognizer.state == UIGestureRecognizerStateBegan || 
     recognizer.state == UIGestureRecognizerStateChanged)
  {
    recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, 
                                                        recognizer.rotation);
    [recognizer setRotation:0];
  }
}

我还有更多的手势识别器示例(包括上面的一个)在 github 上。

I have some more examples of gesture recognizers (including the one above) on github.

这篇关于旋转与iPhone中的触摸拖动相对应的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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