Skip to content

Instantly share code, notes, and snippets.

@Duraiamuthan
Created December 11, 2017 23:01
Show Gist options
  • Save Duraiamuthan/53922c04877fc6a11f9369920b97e666 to your computer and use it in GitHub Desktop.
Save Duraiamuthan/53922c04877fc6a11f9369920b97e666 to your computer and use it in GitHub Desktop.
Circular progress bar with animation pause and resume
#import "ViewController.h"
@interface ViewController (){
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self registerNotifications];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)deregisterNotifications {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
}
- (void)registerNotifications {
// Add observers to application state changes
[[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(applicationDidEnterBackground)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(applicationDidBecomeActive)
name:UIApplicationDidBecomeActiveNotification
object:nil];
}
-(void)applicationDidEnterBackground{
[self pauseLayer:self.progressBar.layer];
}
-(void)applicationDidBecomeActive{
[self resumeLayer:self.progressBar.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;
}
- (IBAction)StartProgressBar:(id)sender {
[UIView animateWithDuration:60 animations:^{
self.progressBar.value = 90;
}];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment