Skip to content

Commit

Permalink
Merge pull request #26 from dealforest/topic/CAReplicatorLayer
Browse files Browse the repository at this point in the history
Modify using CAReplicatorLayer
  • Loading branch information
shu223 committed Oct 14, 2015
2 parents d07b140 + 5f58d7b commit 6a1098f
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 255 deletions.
85 changes: 0 additions & 85 deletions PulsingHalo/MultiplePulsingHaloLayer.h

This file was deleted.

88 changes: 0 additions & 88 deletions PulsingHalo/MultiplePulsingHaloLayer.m

This file was deleted.

18 changes: 16 additions & 2 deletions PulsingHalo/PulsingHaloLayer.h
Expand Up @@ -12,7 +12,7 @@
#import <QuartzCore/QuartzCore.h>


@interface PulsingHaloLayer : CALayer
@interface PulsingHaloLayer : CAReplicatorLayer

/**
* The default value of this property is @c 60pt.
Expand Down Expand Up @@ -60,6 +60,20 @@
*/
@property (nonatomic, assign) BOOL useTimingFunction;

- (id)initWithRepeatCount:(float)repeatCount;
/**
* The default value of this property is @c 1.
*/
@property (nonatomic, assign) NSInteger haloLayerNumber;

/**
* The animation delay in seconds.
*
* The default value of this property is @c 1.
*/
@property (nonatomic, assign) NSTimeInterval startInterval;

- (instancetype)initWithRepeatCount:(float)repeatCount;

- (instancetype)initWithLayerNumber:(NSInteger)layerNumber;

@end
126 changes: 85 additions & 41 deletions PulsingHalo/PulsingHaloLayer.m
Expand Up @@ -12,40 +12,36 @@


@interface PulsingHaloLayer ()
@property (nonatomic, strong) CALayer *effect;
@property (nonatomic, strong) CAAnimationGroup *animationGroup;
@end


@implementation PulsingHaloLayer
@dynamic repeatCount;

- (id)initWithRepeatCount:(float) repeatCount
- (instancetype)initWithRepeatCount:(float) repeatCount
{
self = [super init];
if (self) {
self.contentsScale = [UIScreen mainScreen].scale;
self.opacity = 0;

// default
self.radius = 60;
self.fromValueForRadius = 0.0;
self.fromValueForAlpha = 0.45;
self.keyTimeForHalfOpacity = 0.2;
self.animationDuration = 3;
self.pulseInterval = 0;
self.repeatCount = repeatCount;
self.backgroundColor = [[UIColor colorWithRed:0.000 green:0.478 blue:1.000 alpha:1] CGColor];
self.useTimingFunction = YES;

self.effect = [CALayer new];
self.effect.contentsScale = [UIScreen mainScreen].scale;
self.effect.opacity = 0;
[self addSublayer:self.effect];

[self _setupProperties];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {

[self setupAnimationGroup];
[self _setupAnimationGroup];

if(self.pulseInterval != INFINITY) {

dispatch_async(dispatch_get_main_queue(), ^(void) {

[self addAnimation:self.animationGroup forKey:@"pulse"];
[self.effect addAnimation:self.animationGroup forKey:@"pulse"];
});
}
});
Expand All @@ -54,32 +50,91 @@ - (id)initWithRepeatCount:(float) repeatCount

}

- (id)init {
- (instancetype)initWithLayerNumber:(NSInteger)layerNumber {
self = [self initWithRepeatCount:INFINITY];
if (self) {
self.haloLayerNumber = layerNumber;
}
return self;
}

- (instancetype)init {
return [self initWithRepeatCount:INFINITY];
}

// =============================================================================
#pragma mark - Accessor

- (void)setFrame:(CGRect)frame {

[super setFrame:frame];
self.effect.frame = frame;
}

- (void)setBackgroundColor:(CGColorRef)backgroundColor {

[super setBackgroundColor:backgroundColor];
self.effect.backgroundColor = backgroundColor;
}

- (void)setRadius:(CGFloat)radius {

_radius = radius;

CGPoint tempPos = self.position;

CGFloat diameter = self.radius * 2;

self.bounds = CGRectMake(0, 0, diameter, diameter);
self.cornerRadius = self.radius;
self.position = tempPos;
self.effect.bounds = CGRectMake(0, 0, diameter, diameter);
self.effect.cornerRadius = self.radius;
}

- (void)setPulseInterval:(NSTimeInterval)pulseInterval {

_pulseInterval = pulseInterval;

if (_pulseInterval == INFINITY) {
[self.effect removeAnimationForKey:@"pulse"];
}
}

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

// =============================================================================
#pragma mark - private

- (void)_setupProperties {
_fromValueForRadius = 0.0;
_fromValueForAlpha = 0.45;
_keyTimeForHalfOpacity = 0.2;
_animationDuration = 3;
_pulseInterval = 0;
_useTimingFunction = YES;

self.radius = 60;
self.haloLayerNumber = 1;
self.startInterval = 1;
self.backgroundColor = [[UIColor colorWithRed:0.000 green:0.478 blue:1.000 alpha:1] CGColor];
}

- (void)setupAnimationGroup {
- (void)_setupAnimationGroup {

self.animationGroup = [CAAnimationGroup animation];
self.animationGroup.duration = self.animationDuration + self.pulseInterval;
self.animationGroup.repeatCount = self.repeatCount;
self.animationGroup.removedOnCompletion = NO;
CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
animationGroup.duration = self.animationDuration + self.pulseInterval;
animationGroup.repeatCount = self.repeatCount;
animationGroup.removedOnCompletion = NO;
if (self.useTimingFunction) {
CAMediaTimingFunction *defaultCurve = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
self.animationGroup.timingFunction = defaultCurve;
animationGroup.timingFunction = defaultCurve;
}

CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale.xy"];
Expand All @@ -95,20 +150,9 @@ - (void)setupAnimationGroup {

NSArray *animations = @[scaleAnimation, opacityAnimation];

self.animationGroup.animations = animations;
self.animationGroup.delegate = self;
}


// =============================================================================
#pragma mark - CAAnimation Delegate

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

if (flag) {
[self removeAllAnimations];
[self removeFromSuperlayer];
}
animationGroup.animations = animations;

self.animationGroup = animationGroup;
}

@end

0 comments on commit 6a1098f

Please sign in to comment.