使用物镜C旋转图像

时间:2010-10-24 22:19:28

标签: iphone objective-c

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

这是我的代码:

-

    (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;    
}

我做错了吗?

谢谢!

2 个答案:

答案 0 :(得分:3)

不是从触摸角度创建变换,而是尝试将totalRadians与新角度的差值递增,然后从中创建变换。

-(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];

    }

答案 1 :(得分:1)

如果您的应用适用于4.0及更高版本,我的第一个建议是切换到使用UIRotateGestureRecognizer。它做得对,并为您提供rotation属性。