This repository was archived by the owner on Nov 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInboxRefreshControl.m
More file actions
131 lines (105 loc) · 3.83 KB
/
InboxRefreshControl.m
File metadata and controls
131 lines (105 loc) · 3.83 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//
// InboxRefreshControl.m
//
//
// Created by Bobo Shone on 15/6/5.
// Copyright (c) 2015年 . All rights reserved.
//
#import "InboxRefreshControl.h"
typedef enum {
InboxRefreshControlArcStateIncrease = 0,
InboxRefreshControlArcStateDecrease
} InboxRefreshControlArcState;
@interface InboxRefreshControl ()
@property (nonatomic, assign) BOOL refreshing;
@property (nonatomic, assign) CGFloat circleRadius;
@property (nonatomic, assign) CGFloat maxArcAngle;
@property (nonatomic, assign) CGFloat minArcAngle;
@property (nonatomic, strong) NSTimer *updateTimer;
@property (nonatomic, assign) InboxRefreshControlArcState arcState;
@property (nonatomic, assign) CGFloat currentStartAngle;
@property (nonatomic, assign) CGFloat currentEndAngle;
@property (nonatomic, assign) CGFloat rotationAngle;
@property (nonatomic, assign) CGFloat lineWidth;
@property (nonatomic, strong) UIColor *circleColor;
@end
@implementation InboxRefreshControl
- (instancetype)initWithSize:(CGFloat)size {
self = [super initWithFrame:CGRectMake(0, 0, size, size)];
if (self) {
self.updateTimer = [NSTimer timerWithTimeInterval:1.f/60 target:self selector:@selector(update) userInfo:nil repeats:YES];
self.backgroundColor = [UIColor clearColor];
// customize
self.lineWidth = 2;
self.circleRadius = (size - self.lineWidth)/2;
self.maxArcAngle = M_PI * 2 * 0.85;
self.minArcAngle = M_PI * 2 * 0.15;
self.circleColor = [UIColor blueColor];
}
return self;
}
- (void)beginRefreshing {
self.refreshing = YES;
[[NSRunLoop mainRunLoop] addTimer:self.updateTimer forMode:NSRunLoopCommonModes];
}
- (void)endRefreshing {
self.refreshing = NO;
[self.updateTimer invalidate];
}
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat startAngle = self.currentStartAngle;
CGFloat endAngle = self.currentEndAngle;
CGContextAddArc(context, rect.size.width/2, rect.size.height/2, self.circleRadius, startAngle, endAngle, 0);
CGContextSetStrokeColorWithColor(context, self.circleColor.CGColor);
CGContextSetLineWidth(context, self.lineWidth);
CGContextStrokePath(context);
}
- (void)update {
CGFloat degreeDelta = 6;
if (self.currentEndAngle - self.currentStartAngle <= self.minArcAngle) {
self.arcState = InboxRefreshControlArcStateIncrease;
}
if (self.currentEndAngle - self.currentStartAngle >= self.maxArcAngle) {
self.arcState = InboxRefreshControlArcStateDecrease;
}
if (self.arcState == InboxRefreshControlArcStateIncrease) {
self.currentEndAngle += degreeDelta / 180.f * M_PI;
}
else if (self.arcState == InboxRefreshControlArcStateDecrease) {
self.currentStartAngle += degreeDelta / 180.f * M_PI;
}
self.rotationAngle += 3 / 180.f * M_PI;
dispatch_async(dispatch_get_main_queue(), ^{
self.transform = CGAffineTransformMakeRotation(self.rotationAngle);
[self setNeedsDisplay];
});
}
- (void)dealloc {
if (self.updateTimer.valid) {
[self.updateTimer invalidate];
self.updateTimer = nil;
}
}
- (void)pullWithDistance:(CGFloat)pullingDistance {
if (self.refreshing) {
return;
}
CGFloat distance = pullingDistance;
CGFloat ignoredDistance = 50;
if (distance < ignoredDistance) {
return;
}
else {
distance = distance - ignoredDistance;
}
self.currentStartAngle = -M_PI/2;
CGFloat maxChangeSizePullingDistance = 50;
CGFloat scale = MIN(1, distance/maxChangeSizePullingDistance);
self.currentEndAngle = self.currentStartAngle + scale * self.maxArcAngle;
self.rotationAngle = scale * M_PI/2*3;
self.transform = CGAffineTransformMakeRotation(self.rotationAngle);
self.alpha = scale;
[self setNeedsDisplay];
}
@end