我是 Xcode 新手,正在开发一个小工具。基本上它有一个在 View 中旋转的圆形图像。该图像有两个点放置在圆径向上。当图像处于初始位置时,我们可以通过触摸拖动点,它们会随着指尖移动......但是当我们旋转图像并在它之后拖动点时,它们会在屏幕上不规则地移动!......如果旋转为 180 度时,点会沿与拖动运动相反的方向移动!...
我很感激任何帮助。 提前谢谢了。
Rotation.m
@implementation Rotation
@synthesize rotation = rotation_;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([[event touchesForGestureRecognizer:self] count] > 1) {
[self setState:UIGestureRecognizerStateFailed];
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([self state] == UIGestureRecognizerStatePossible) {
[self setState:UIGestureRecognizerStateBegan];
} else {
[self setState:UIGestureRecognizerStateChanged];
}
UITouch *touch = [touches anyObject];
UIView *view = [self view];
CGPoint center = CGPointMake(CGRectGetMidX([view bounds]), CGRectGetMidY([view bounds]));
CGPoint currentTouchPoint = [touch locationInView:view];
CGPoint previousTouchPoint = [touch previousLocationInView:view];
CGFloat angleInRadians = atan2f(currentTouchPoint.y - center.y, currentTouchPoint.x - center.x) - atan2f(previousTouchPoint.y - center.y, previousTouchPoint.x - center.x);
[self setRotation:angleInRadians];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([self state] == UIGestureRecognizerStateChanged) {
[self setState:UIGestureRecognizerStateEnded];
} else {
[self setState:UIGestureRecognizerStateFailed];
}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[self setState:UIGestureRecognizerStateFailed];
}
@end
最佳答案
确保将图层的 anchor 设置为
circleView.layer.anchorPoint = CGPointMake(.5, .5);
关于ios - Xcode UIView rotation : How to set the new position coordinates to 0, 0 旋转后不移动图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31655612/