Skip to content

Commit

Permalink
added a property to disable resume
Browse files Browse the repository at this point in the history
  • Loading branch information
shu223 committed Jan 16, 2017
1 parent ddd9381 commit 045ee21
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
6 changes: 6 additions & 0 deletions PulsingHalo/PulsingHaloLayer.h
Expand Up @@ -67,6 +67,12 @@
*/
@property (nonatomic, assign) NSTimeInterval startInterval;

/**
* When this value is true, the halo will be automatically resumed after entering foreground.
*
* The default value of this property is YES.
*/
@property (nonatomic, assign) BOOL shouldResume;

- (void)start;

Expand Down
23 changes: 11 additions & 12 deletions PulsingHalo/PulsingHaloLayer.m
Expand Up @@ -70,11 +70,8 @@ - (void)onDidEnterBackground:(NSNotification *)notification {
}

- (void)onWillEnterForeground:(NSNotification *)notification {
NSLog(@"effect:%@, superlayer:%@, effect.superlayer:%@", self.effect, self.superlayer, self.effect.superlayer);
[self addSublayer:self.effect];
[self.prevSuperlayer insertSublayer:self atIndex:self.prevLayerIndex];
if (self.prevAnimation) {
[self.effect addAnimation:self.prevAnimation forKey:@"pulse"];
if (self.shouldResume) {
[self _resume];
}
}

Expand All @@ -97,7 +94,6 @@ - (void)setBackgroundColor:(CGColorRef)backgroundColor {
}

- (void)setRadius:(CGFloat)radius {

_radius = radius;

CGFloat diameter = self.radius * 2;
Expand All @@ -107,7 +103,6 @@ - (void)setRadius:(CGFloat)radius {
}

- (void)setPulseInterval:(NSTimeInterval)pulseInterval {

_pulseInterval = pulseInterval;

if (_pulseInterval == INFINITY) {
Expand All @@ -116,20 +111,17 @@ - (void)setPulseInterval:(NSTimeInterval)pulseInterval {
}

- (void)setHaloLayerNumber:(NSInteger)haloLayerNumber {

_haloLayerNumber = haloLayerNumber;
self.instanceCount = haloLayerNumber;
self.instanceDelay = (self.animationDuration + self.pulseInterval) / haloLayerNumber;
}

- (void)setStartInterval:(NSTimeInterval)startInterval {

_startInterval = startInterval;
self.instanceDelay = startInterval;
}

- (void)setAnimationDuration:(NSTimeInterval)animationDuration {

_animationDuration = animationDuration;

self.instanceDelay = (self.animationDuration + self.pulseInterval) / self.haloLayerNumber;
Expand All @@ -145,6 +137,7 @@ - (void)setRepeatCount:(float)repeatCount {
#pragma mark - Private

- (void)_setupDefaults {
_shouldResume = YES;
_fromValueForRadius = 0.0;
_keyTimeForHalfOpacity = 0.2;
_animationDuration = 3;
Expand All @@ -159,7 +152,6 @@ - (void)_setupDefaults {
}

- (void)_setupAnimationGroup {

CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
animationGroup.duration = self.animationDuration + self.pulseInterval;
animationGroup.repeatCount = self.repeatCount;
Expand Down Expand Up @@ -187,12 +179,19 @@ - (void)_setupAnimationGroup {
self.animationGroup.delegate = self;
}

- (void)_resume {
[self addSublayer:self.effect];
[self.prevSuperlayer insertSublayer:self atIndex:self.prevLayerIndex];
if (self.prevAnimation) {
[self.effect addAnimation:self.prevAnimation forKey:@"pulse"];
}
}


// =============================================================================
#pragma mark - CAAnimationDelegate

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {

if ([self.effect.animationKeys count]) {
[self.effect removeAllAnimations];
}
Expand Down

0 comments on commit 045ee21

Please sign in to comment.