使用物镜C旋转图像 [英] Rotating image using objective C

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

问题描述

我正在尝试旋转图像。这是我成功的。问题是,当我单击并略微拖动时,图像完全旋转到达我点击的点,然后顺时针拖动图像然后慢慢旋转。我担心为什么它完全旋转到我拖动的地方。我希望拖动从它找到的位置开始,而不是旋转到我的手指向下的位置,然后从那里开始。

I am trying to rotate an image. This I am succeeding in doing. The problem is that when I am clicking down and dragging slightly, the image rotates completely to reach the point where I have clicked and then rotates slowly as I drag the image clockwise. I am concerned to why it is rotating completely to the place that I am dragging. I want the dragging to start from the position it is found and NOT to rotate to the place my finger is down on and then starts from there.

这是我的代码:

-

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

    -(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        NSSet *allTouches = [event allTouches];

        int len = [allTouches count]-1;

        UITouch *touch =[[allTouches allObjects] objectAtIndex:len];
        CGPoint location = [touch locationInView:[self superview]];
        float theAngle = atan2( location.y-self.center.y, location.x-self.center.x );

        totalRadians = theAngle;

        [self rotateImage:theAngle];

    }

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
}

-(void) rotateImage:(float)angleRadians{

    self.transform = CGAffineTransformMakeRotation(angleRadians);
    CATransform3D rotatedTransform = self.layer.transform;
    self.layer.transform = rotatedTransform;    
}

我做错了什么?

谢谢!

推荐答案

尝试增加totalRadians,而不是从触摸角度创建转换通过它与新角度的差异,然后从中创建变换。

Instead of creating the transformation from the angle that your getting from the touch, try incrementing totalRadians by its difference from the new angle, and then create the transform from that.

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        NSSet *allTouches = [event allTouches];

        int len = [allTouches count]-1;

        UITouch *touch =[[allTouches allObjects] objectAtIndex:len];
        CGPoint location = [touch locationInView:[self superview]];
        float theAngle = atan2( location.y-self.center.y, location.x-self.center.x );

        totalRadians += fabs(theAngle - totalRadians);
        totalRadians = fmod(totalRadians, 2*M_PI);

        [self rotateImage:totalRadians];

    }

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

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