4

I have a UIView that I want the user to be able to drag around the screen.

-(void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event 
{
    CGPoint pt = [[touches anyObject] locationInView:self];

    CGPoint center = self.center;

    CGFloat adjustmentX = pt.x - startLocation.x;
    CGFloat adjustmentY = pt.y - startLocation.y;

   center.x += adjustmentX;
   center.y += adjustmentY;

   [self setCenter:center];
}

works perfectly until I apply a transform to the view as follows:

self.transform = CGAffineTransformMakeRotation(....);

However, once the transform has been applied, dragging the UIView results in the view being moved in a exponential spiral effect.

I'm assuming I have to make an correction for the rotation in the touchesMoved method, but so far all attempts have eluded me.

Dave
  • 107
  • 2
  • 4

1 Answers1

1

You need to translate your transform to your new coordinates because a transform other than the identity is assigned to your view and moving its center alters the transform's results.

Try this instead of altering your view's center

self.transform = CGAffineTransformTranslate(self.transform, adjustmentX, adjustmentY);
tsakoyan
  • 1,891
  • 14
  • 12
  • This works perfectly until another rotation is performed via CGAffineTransformMakeRotation, because self.center has not been updated - meaning the rotation point is incorrect. The solution would appear to be to set self.center before the rotation, but this results in the original incorrect behaviour. – Dave Dec 26 '10 at 12:57
  • Applying a new transform again will nullify the previous ones (the previous rotation + the movement), you can continue applying rotations like this self.transform = CGAffineTransformRotate(self.transform, additiveAngle); // where additive angle could be newAngle - previousAngle – tsakoyan Dec 27 '10 at 08:09