用触摸旋转图像

时间:2011-03-03 07:54:05

标签: ios cocoa-touch uitouch

我制作了一个应用程序,用时钟设置睡眠定时器。基本上它是一只手,用户可以移动来设置他的睡眠时间。我试图用<旋转图像< strong> uitouch 但它从中间旋转但我希望它从尖端旋转。其次我希望图像只在用户触摸图像的尖端时旋转但在我的项目中图像也在旋转时使用触摸屏幕的任何部分。我也想在两个方向上旋转图像,但在我的项目中,由于这种方法,它只能顺时针移动

image.transform = CGAffineTransformRotate(image.transform, degreesToRadians(1));

有人可以给我提供关于如何做到的提示或解决方案吗?

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    //

        touch = [[event allTouches] anyObject];
        touchLocation = [touch locationInView:touch.view];

        NSLog(@"began");

    }


    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        // get touch event

        [image.layer setAnchorPoint:CGPointMake(0.0,0.0)];



        if ([touch view] == image) {
            image.transform = CGAffineTransformRotate(image.transform, degreesToRadians(1));

        //image.center = touchLocation;
    }

      }

    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
        NSLog(@"end");



    }

3 个答案:

答案 0 :(得分:2)

详细说明,您可以自定义rotateView,然后:

1:在“touchesBegan”的委托方法中,获取finger的初始点和initialAngle。

2:在“touchesMoved”中,获取手指的新点:

CGPoint newPoint = [[touches anyObject] locationInView:self];
[self pushTouchPoint:thePoint date:[NSDate date]];
double angleDif = [self angleForPoint:newPoint] - [self angleForPoint:initialPoint];
self.angle = initialAngle + angleDif;
[[imageView layer] setTransform:CATransform3DMakeRotation(angle, 0, 0, 1)];

3:最后,在“touchesEnded”中你可以计算最终的AngularVelocity。

如果有任何疑问,可以回复一下。

答案 1 :(得分:0)

  

我尝试用uitouch旋转图像,但是它从中间旋转但我希望它从尖端旋转

我不知道SDK中的任何方式在其极端旋转元素。如果你想获得那个效果,请使用时钟控制器图像,你有两倍的长度,半部分是透明的。它不是一个直接的解决方案,而是一种解决方法。

  

此外,我想在两个方向上旋转图像,但在我的项目中,由于此方法,它只能顺时针移动

根据iOS开发人员文档,正角度值指定逆时针旋转,负值指定顺时针旋转。

答案 2 :(得分:0)

以您刚才使用的另一种方式旋转它:

image.transform = CGAffineTransformRotate(image.transform, degreesToRadians(1));

要从提示旋转你应该使用setAnchorPoint(就像你在代码中使用的那样,但我认为你应该这样做:[image setAnchorPoint])。

有关此主题的更多信息:setAnchorPoint for UIImage?

相关问题