繁体   English   中英

Xcode UIView旋转:如何在旋转后不移动图像的情况下将新位置坐标设置为0,0?

[英]Xcode UIView rotation: How to set the new position coordinates to 0,0 without moving the image after it rotation?

我是Xcode的新手,并且正在开发一个小工具。 基本上,它具有在视图中旋转的圆形图像。 该图像在圆的径向上有两个点。 当图像处于初始位置时,我们可以通过触摸拖动点,它们随指尖移动...但是,当我们旋转图像并在其拖动之后,它们在屏幕上不规则地移动!...如果旋转角度为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

确保将图层的锚点设置为

circleView.layer.anchorPoint = CGPointMake(.5, .5);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM