Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Demo/SVPulsingAnnotationView/SVViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,18 @@ - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnot
return nil;
}


- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {

[((SVPulsingAnnotationView *)view) pausePulsing];

}

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view {

[((SVPulsingAnnotationView *)view) resumePulsing];

}


@end
4 changes: 4 additions & 0 deletions SVPulsingAnnotationView/SVPulsingAnnotationView.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@

@property (nonatomic, copy) void (^willMoveToSuperviewAnimationBlock)(SVPulsingAnnotationView *view, UIView *superview); // default is pop animation

- (void)stopPulsing;
- (void)pausePulsing;
- (void)resumePulsing;

@end
48 changes: 44 additions & 4 deletions SVPulsingAnnotationView/SVPulsingAnnotationView.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,46 @@ - (id)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString
return self;
}

- (void)stopPulsing {

[_whiteDotLayer removeAllAnimations];
[_colorDotLayer removeAllAnimations];
[_colorHaloLayer removeAllAnimations];
}

- (void)pausePulsing {

[self pauseLayer:_whiteDotLayer];
[self pauseLayer:_colorDotLayer];
[self pauseLayer:_colorHaloLayer];

}

- (void)resumePulsing {

[self resumeLayer:_whiteDotLayer];
[self resumeLayer:_colorDotLayer];
[self resumeLayer:_colorHaloLayer];

}

- (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;
}

- (void)rebuildLayers {
[_whiteDotLayer removeFromSuperlayer];
_whiteDotLayer = nil;
Expand Down Expand Up @@ -224,7 +264,7 @@ - (CALayer*)colorDotLayer {

if(self.delayBetweenPulseCycles != INFINITY) {
CAMediaTimingFunction *defaultCurve = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];

CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
animationGroup.duration = self.pulseAnimationDuration;
animationGroup.repeatCount = INFINITY;
Expand All @@ -233,7 +273,7 @@ - (CALayer*)colorDotLayer {
animationGroup.timingFunction = defaultCurve;
animationGroup.speed = 1;
animationGroup.fillMode = kCAFillModeBoth;

CABasicAnimation *pulseAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale.xy"];
pulseAnimation.fromValue = @0.8;
pulseAnimation.toValue = @1;
Expand All @@ -245,13 +285,13 @@ - (CALayer*)colorDotLayer {
opacityAnimation.duration = self.pulseAnimationDuration;

animationGroup.animations = @[pulseAnimation, opacityAnimation];

dispatch_async(dispatch_get_main_queue(), ^(void) {
[_colorDotLayer addAnimation:animationGroup forKey:@"pulse"];
});
}
});

}
return _colorDotLayer;
}
Expand Down