ios - Xcode UIView rotation : How to set the new position coordinates to 0, 0 旋转后不移动图像?

标签 ios objective-c xcode rotation

我是 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/

相关文章:

当 UIApplicationExitsOnSuspend = Yes 时,iOS 8 在重新启动时崩溃

javascript - 如何检查 WKWebView 是否加载了 HTML 字符串

ios - 如何使导航 Controller 导航栏不可见或至少改变颜色

objective-c - 使用 Pushviewcontroller 自下而上的动画?

ios - 在键盘上显示带有按钮的 UIView,例如 Skype、Viber 信使(Swift、iOS)

objective-c - 使用 scanf 在循环中使用多个变量

xcode - MAC应用程序: Unable to import Mac Distribution Profile in Organization

ios - 在 Swift 中获取图像名称 UIImagePickerController

ios - 如何在 iOS 中将 uitextview 的文本转换为背景图像上的 mask

ipad - iOS:不兼容的 block 指针类型