Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If all you want to do is rotate the image endlessly, this works quite well, and is very simple:</p> <pre><code>NSTimeInterval duration = 10.0f; CGFloat angle = M_PI / 2.0f; CGAffineTransform rotateTransform = CGAffineTransformRotate(imageView.transform, angle); [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionRepeat| UIViewAnimationOptionCurveLinear animations:^{ imageView.transform = rotateTransform; } completion:nil]; </code></pre> <p>In my experience, this works flawlessly, but be sure your image is capable of being rotated around its center without any offsets, or the image animation will "jump" once it makes it around to PI.</p> <p>To change the direction of the spin, change the sign of <code>angle</code> (<code>angle *= -1</code>).</p> <p><strong>Update</strong> Comments by @AlexPretzlav made me revisit this, and I realized that when I wrote this the image I was rotating was mirrored along both the vertical and horizontal axis, meaning the image was indeed only rotating 90 degrees and then resetting, though it <em>looked</em> like it was continuing to rotate all the way around.</p> <p>So, if your image is like mine was, this will work great, however, if the image is not symmetrical, you'll notice the "snap" back to the original orientation after 90 degrees.</p> <p>To rotate a non-symmetrical image, you're better off with the accepted answer.</p> <p>One of these less elegant solutions, seen below, will truly rotate the image, but there may be a noticeable stutter when the animation is restarted:</p> <pre><code>- (void)spin { NSTimeInterval duration = 0.5f; CGFloat angle = M_PI_2; CGAffineTransform rotateTransform = CGAffineTransformRotate(self.imageView.transform, angle); [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionCurveLinear animations:^{ self.imageView.transform = rotateTransform; } completion:^(BOOL finished) { [self spin]; }]; } </code></pre> <p>You could also do this just with blocks, as @richard-j-ross-iii suggests, but you will get a retain loop warning since the block is capturing itself:</p> <pre><code>__block void(^spin)() = ^{ NSTimeInterval duration = 0.5f; CGFloat angle = M_PI_2; CGAffineTransform rotateTransform = CGAffineTransformRotate(self.imageView.transform, angle); [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionCurveLinear animations:^{ self.imageView.transform = rotateTransform; } completion:^(BOOL finished) { spin(); }]; }; spin(); </code></pre>
    singulars
    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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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