通过触摸旋转图像

发布于 2024-10-19 23:06:22 字数 1038 浏览 3 评论 0原文

我制作了一个用时钟设置睡眠计时器的应用程序。基本上,它是一个有单手的时钟,用户可以移动它来设置他的睡眠时间。我尝试使用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");



    }

I making an app which sets the sleep timer with a clock .Basically it is clock which has a single hand which user can move to set his sleep time .I tried to rotate the image with uitouch but it rotates from middle but i want it to rotate from the tip.Secondly i want that the image only rotates when user is touching the tip of the image but in my project the image is also rotating when use touches any part of the screen.Also i want to rotate the image in both directions but in my project it moves only clockwise due to this method

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

Can anybody give me hints or solutions about how can it be done?

- (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");



    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

瑶笙 2024-10-26 23:06:22

具体来说,可以自定义一个rotateView,然后:

1:在“touchesBegan”的委托方法中,获取手指的initialPoint和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。

如果有任何困惑,为了了解更多详细信息,您可以写信回来。

In detail, you can custom a rotateView,then:

1: In the delegate method of "touchesBegan", get initialPoint of finger and initialAngle.

2: During "touchesMoved",get the newPoint of finger:

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: At last, in "touchesEnded" you can calculate final AngularVelocity.

If anything being confused, for more detail, you can write back.

孤凫 2024-10-26 23:06:22

要以其他方式旋转它,您只需使用:

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

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

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

To rotate it the other way you just use:

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

And to rotate form the tip you should use setAnchorPoint (like you used in your code, but i think you should do: [image setAnchorPoint]).

more on the subject: setAnchorPoint for UIImage?

独自唱情﹋歌 2024-10-26 23:06:22

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

我不知道 SDK 中有什么方法可以在其最末端旋转元素。如果您想获得这种效果,请使用时钟手柄图像,您的长度是两倍,其中一半是透明的。这不是一个直接的解决方案,而是一种解决方法。

我也想双向旋转图像,但在我的项目中,由于这种方法,它只能顺时针移动

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

I tried to rotate the image with uitouch but it rotates from middle but i want it to rotate from the tip

I dont know any way in SDK to rotate an element on its extreme end. If you want to have that effect take the clock handle image you have twice in length with half portion transparent. It is not a direct solution, but a workaround.

Also i want to rotate the image in both directions but in my project it moves only clockwise due to this method

As per iOS developer documentation, a positive angle value specifies counterclockwise rotation and a negative value specifies clockwise rotation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文