public
Last active

UPDATE: Added stop, pause and resume animation methods. UIImageView category - 360° rotating with duration and repeat count. USAGE: [_myImageView rotate360WithDuration:1.0 repeatCount:0]; //0 for infinite loop

  • Download Gist
UIImageView+Rotate.h
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//
// Created by Alex Hajdu on 5/27/13.
// Copyright (c) 2013 Mr.Fox and friends. All rights reserved.
//
// To change the template use AppCode | Preferences | File Templates.
//
 
 
#import <Foundation/Foundation.h>
 
@interface UIImageView (Rotate)
- (void)rotate360WithDuration:(CGFloat)duration repeatCount:(float)repeatCount;
- (void)pauseAnimations;
- (void)resumeAnimations;
- (void)stopAllAnimations;
@end
UIImageView+Rotate.m
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
//
// Created by Alex Hajdu on 5/27/13.
// Copyright (c) 2013 Mr.Fox and friends. All rights reserved.
//
// To change the template use AppCode | Preferences | File Templates.
//
 
 
#import <QuartzCore/QuartzCore.h>
#import "UIImageView+Rotate.h"
 
@implementation UIImageView (Rotate)
 
- (void)rotate360WithDuration:(CGFloat)duration repeatCount:(float)repeatCount
{
 
CABasicAnimation *fullRotation;
fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
fullRotation.fromValue = [NSNumber numberWithFloat:0];
fullRotation.toValue = [NSNumber numberWithFloat:((360 * M_PI) / 180)];
fullRotation.duration = duration;
if (repeatCount == 0)
fullRotation.repeatCount = MAXFLOAT;
else
fullRotation.repeatCount = repeatCount;
 
[self.layer addAnimation:fullRotation forKey:@"360"];
}
 
- (void)stopAllAnimations
{
 
[self.layer removeAllAnimations];
};
 
- (void)pauseAnimations
{
 
[self pauseLayer:self.layer];
}
 
- (void)resumeAnimations
{
 
[self resumeLayer:self.layer];
}
 
- (void)pauseLayer:(CALayer *)layer
{
 
CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
layer.speed = 0.0;
layer.timeOffset = pausedTime;
}
 
- (void)resumeLayer:(CALayer *)layer
{
 
CFTimeInterval pausedTime = [layer timeOffset];
layer.speed = 1.0;
layer.timeOffset = 0.0;
layer.beginTime = 0.0;
CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
layer.beginTime = timeSincePause;
}
 
@end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.