我正在尝试旋转图像。我正在成功地做到这一点。问题是,当我向下单击并轻轻拖动时,图像会完全旋转以到达我单击的位置,然后在我顺时针拖动图像时缓慢旋转。我很关心为什么它会完全旋转到我拖动的地方。我希望拖动从找到的位置开始,而不是旋转到我手指按下的位置,然后从那里开始。
这是我的代码:
-
(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 增加与新角度的差值,然后从中创建变换。
-(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];
}
关于iphone - 使用物镜 C 旋转图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4010779/