简体   繁体   中英

Set center point of UIView after CGAffineTransformMakeRotation

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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