Sérgio Barbosa
Sérgio Barbosa

Reputation: 1

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

I'm new to Xcode and I'm developing a small tool. Basically it has a circle image that rotates in a view. That image has two dots to be placed in the circle radials. When the image is in the initial position we can drag the dots with touch and them moves following the finger tip... But when we rotate the image and after it we drag the dots them moves erratically on screen!... If the rotation is 180deg the dots move on opposite direction of the drag movement!...

Any help I appreciate. Many thanks in advance.

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

Upvotes: 0

Views: 312

Answers (1)

AlexKoren
AlexKoren

Reputation: 1605

Make sure that you set the anchor point of the layer to

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

Upvotes: 1

Related Questions