Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to move a UIImageView after applying CGAffineTransformRotate to it?
    primarykey
    data
    text
    <p>I'm building an iPhone app. I have a UIView that contains a set of UIImageView subclass objects. The user can drag and rotate the image views via touch. I'm having trouble moving the image view after it has been rotated.</p> <p>To rotate an image view, I apply a transform rotation, which works fine. It looks like this:</p> <pre><code>CGAffineTransform trans = self.transform; self.transform = CGAffineTransformRotate(trans, delta); </code></pre> <p>The problem comes later when the user tries to move the element via touch. In touchesBegan:WithEvent:, I save the start point in a class variable, startLocation:</p> <pre><code>- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { // Retrieve the touch point CGPoint pt = [[touches anyObject] locationInView:self]; startLocation = pt; } </code></pre> <p>In touchesMoved:withEvent:, I had the following code, which works well enough if there is no rotation transform on the image view:</p> <pre><code>- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event { CGPoint pt = [[touches anyObject] locationInView:self]; CGFloat dx = pt.x - startLocation.x; CGFloat dy = pt.y - startLocation.y; CGPoint newCenter = CGPointMake(self.center.x + dx, self.center.y + dy); self.center = newCenter; } </code></pre> <p>But if there is a rotation transform on the image view, then the image view thrashes about the screen on each touchesMoved event and soon disappears. In the debugger, I observed that the value of pt became monstrous. It occurred to me that I needed to transform that point, which I did, like so:</p> <pre><code>- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event { CGPoint pt = [[touches anyObject] locationInView:self]; if (!CGAffineTransformIsIdentity(self.transform)) { pt = CGPointApplyAffineTransform(pt, self.transform); } CGFloat dx = pt.x - startLocation.x; CGFloat dy = pt.y - startLocation.y; CGPoint newCenter = CGPointMake(self.center.x + dx, self.center.y + dy); } </code></pre> <p>This worked much better. I can drag the image about the screen now. But the very first movement causes the image to jolt once in one direction or another, depending on the angle of rotation in the transform and the dimensions of the image view.</p> <p>How can I move the image view without having the initial jolt?</p> <p>Why is it that I do not need to transform startLocation (the point I capture when touches began)?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload