-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpotifyProgressHUD.m
100 lines (83 loc) · 3.53 KB
/
SpotifyProgressHUD.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
90
91
92
93
94
95
96
97
98
99
100
//
// ProgressView.m
// testdel
//
// Created by Paolo Boschini on 8/8/13.
// Copyright (c) 2013 Paolo Boschini. All rights reserved.
//
#import "SpotifyProgressHUD.h"
#import <QuartzCore/QuartzCore.h>
@interface CircleView : UIView
@end
@implementation CircleView
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.opaque = NO;
}
return self;
}
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextFillEllipseInRect(context, rect);
}
@end
@interface SpotifyProgressHUD () {
CircleView *leftCircle;
CircleView *middleCircle;
CircleView *rightCircle;
}
@property float interval;
@property int pointDiameter;
@end
@implementation SpotifyProgressHUD
- (id)initWithFrame:(CGRect)frame withPointDiameter:(int)diameter withInterval:(float)interval {
if ((self = [super initWithFrame:frame])) {
self.interval = interval;
self.pointDiameter = diameter;
self.backgroundColor = [UIColor colorWithRed:200 green:200 blue:200 alpha:0.5];
self.layer.cornerRadius = 15;
leftCircle = [self addCircleViewWithXOffsetFromCenter:-35];
middleCircle = [self addCircleViewWithXOffsetFromCenter:0];
rightCircle = [self addCircleViewWithXOffsetFromCenter:35];
self.animating = YES;
NSArray *circles = @[leftCircle, middleCircle, rightCircle];
[self animateWithViews:circles index:0 delay:self.interval*2 offset:self.pointDiameter];
[self animateWithViews:circles index:1 delay:self.interval*3 offset:self.pointDiameter];
[self animateWithViews:circles index:2 delay:self.interval*4 offset:self.pointDiameter];
}
return self;
}
- (CircleView*)addCircleViewWithXOffsetFromCenter:(float)offset {
CGRect rect = CGRectMake(0, 0, self.pointDiameter, self.pointDiameter);
CircleView *circle = [[CircleView alloc] initWithFrame:rect];
circle.center = self.center;
circle.frame = CGRectOffset(circle.frame, offset, 0);
[self addSubview:circle];
return circle;
}
- (void)animateWithViews:(NSArray*)circles index:(int)index delay:(float)delay offset:(float)offset {
UIView *view = ((CircleView*)[circles objectAtIndex:index]);
[UIView animateWithDuration:0.2 delay:delay options:UIViewAnimationOptionCurveEaseIn animations:^{
view.frame = CGRectMake(view.frame.origin.x - offset/2,
view.frame.origin.y - offset/2,
view.frame.size.width + offset,
view.frame.size.height + offset);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
view.frame = CGRectMake(view.frame.origin.x + offset/2,
view.frame.origin.y + offset/2,
view.frame.size.width - offset,
view.frame.size.height - offset);
} completion:^(BOOL finished) {
if (self.animating) {
if (index == 2) {
[self animateWithViews:circles index:0 delay:self.interval*2 offset:self.pointDiameter];
[self animateWithViews:circles index:1 delay:self.interval*3 offset:self.pointDiameter];
[self animateWithViews:circles index:2 delay:self.interval*4 offset:self.pointDiameter];
}
}
}];
}];
}
@end