Skip to content

Instantly share code, notes, and snippets.

@boherna
Last active June 17, 2018 13:08
Show Gist options
  • Save boherna/76edbed674e0a20a57dd to your computer and use it in GitHub Desktop.
Save boherna/76edbed674e0a20a57dd to your computer and use it in GitHub Desktop.
Spin animation of indefinite duration (not INFINITE). It can be stopped at an even quarter turn if needed. Also, the acceleration ramps up during the first 90 degrees, and decelerates during the last 90 degrees (after a stop has been requested).
// an ivar for your class:
BOOL animating;
- (void)spinWithOptions:(UIViewAnimationOptions)options
{
// this spin completes 360 degrees every 2 seconds
[UIView animateWithDuration: 0.5f
delay: 0.0f
options: options
animations: ^{
self.imageToMove.transform = CGAffineTransformRotate(imageToMove.transform, M_PI / 2);
}
completion: ^(BOOL finished)
{
if (finished)
{
if (animating)
{
// if flag still set, keep spinning with constant speed
[self spinWithOptions: UIViewAnimationOptionCurveLinear];
}
else
if (options != UIViewAnimationOptionCurveEaseOut)
{
// one last spin, with deceleration
[self spinWithOptions: UIViewAnimationOptionCurveEaseOut];
}
}
}];
}
- (void) startSpin
{
if (!animating)
{
animating = YES;
[self spinWithOptions: UIViewAnimationOptionCurveEaseIn];
}
}
- (void) stopSpin
{
// set the flag to stop spinning after one last 90 degree increment
animating = NO;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment