-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRippleEffect.m
92 lines (76 loc) · 3.76 KB
/
RippleEffect.m
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//
// RippleEffect.m
// Tap
//
// Created by Souvick Ghosh on 17/12/17.
// Copyright © 2017 User. All rights reserved.
//
#import "RippleEffect.h"
@implementation RippleEffect
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if(self != nil) {
}
return self;
}
- (void)awakeFromNib {
[super awakeFromNib];
self.backgroundColor = [UIColor clearColor];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self drawCircle];
});
}
- (void)drawCircle {
UIColor *topColor = [self getUIColorObjectFromHexString:self.StartColorHexCode alpha:1.0];
UIColor *bottomColor = [self getUIColorObjectFromHexString:self.EndColorHexCode alpha:1.0];
float animationStartDelay = 0.5;
for(int i=0;i<self.NoOfCircle;i++) {
float initialRadius = self.frame.size.width>self.frame.size.height?((self.frame.size.height/2.0)-self.InitialRadiusPadding):((self.frame.size.width/2.0)-self.InitialRadiusPadding);
UIBezierPath *bezierPath = [UIBezierPath bezierPath];
[bezierPath addArcWithCenter:CGPointMake(self.frame.size.width/2.0, self.frame.size.height/2.0) radius:initialRadius+(i*self.CircleRadiusDifference) startAngle:0 endAngle:2 * M_PI clockwise:YES];
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = self.bounds;
gradientLayer.colors = @[(id)topColor.CGColor, (id)bottomColor.CGColor];
CAShapeLayer *progressLayer = [[CAShapeLayer alloc] init];
progressLayer.frame = self.bounds;
[progressLayer setPath:bezierPath.CGPath];
progressLayer.fillColor = [UIColor clearColor].CGColor;
progressLayer.strokeColor = [UIColor blackColor].CGColor;
[progressLayer setLineWidth:self.LineWidth];
//progressLayer.opacity = 0.0;
gradientLayer.mask = progressLayer;
[self.layer addSublayer:gradientLayer];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(animationStartDelay+(i*0.1) * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self animateCircleLayer:progressLayer];
});
}
}
- (void)animateCircleLayer:(CAShapeLayer *)layer {
CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
scaleAnimation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
scaleAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.3, 1.3, 1)];
scaleAnimation.removedOnCompletion = NO;
CABasicAnimation *alphaAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
alphaAnimation.fromValue = @1;
alphaAnimation.removedOnCompletion = NO;
alphaAnimation.toValue = @0;
CAAnimationGroup *animation = [CAAnimationGroup animation];
animation.animations = @[scaleAnimation,alphaAnimation];
animation.duration = 2.0f;
animation.removedOnCompletion = NO;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
animation.repeatCount = HUGE_VALF;
[layer addAnimation:animation forKey:nil];
}
- (UIColor *)getUIColorObjectFromHexString:(NSString *)hexStr alpha:(float)alpha {
unsigned int hexInt = 0;
NSScanner *scanner = [NSScanner scannerWithString:hexStr];
[scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@"#"]];
[scanner scanHexInt:&hexInt];
UIColor *color = [UIColor colorWithRed:((CGFloat) ((hexInt & 0xFF0000) >> 16))/255
green:((CGFloat) ((hexInt & 0xFF00) >> 8))/255
blue:((CGFloat) (hexInt & 0xFF))/255
alpha:alpha];
return color;
}
@end