-
Notifications
You must be signed in to change notification settings - Fork 120
/
OLImageView.m
280 lines (238 loc) · 7.08 KB
/
OLImageView.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
//
// OLImageView.m
// OLImageViewDemo
//
// Created by Diego Torres on 9/5/12.
// Copyright (c) 2012 Onda Labs. All rights reserved.
//
#import "OLImageView.h"
#import "OLImage.h"
#import <QuartzCore/QuartzCore.h>
@interface OLImageView () {
struct {
unsigned int didLoop : 1;
unsigned int shouldStartAnimating : 1;
} _delegateFlags;
}
@property (nonatomic, strong) OLImage *animatedImage;
@property (nonatomic, strong) CADisplayLink *displayLink;
@property (nonatomic) NSTimeInterval accumulator;
@property (nonatomic) NSUInteger currentFrameIndex;
@property (nonatomic) NSUInteger loopCountdown;
@end
@implementation OLImageView
const NSTimeInterval kMaxTimeStep = 1; // note: To avoid spiral-o-death
@synthesize runLoopMode = _runLoopMode;
@synthesize displayLink = _displayLink;
- (id)init
{
self = [super init];
if (self) {
self.currentFrameIndex = 0;
}
return self;
}
- (CADisplayLink *)displayLink
{
if (self.superview) {
if (!_displayLink && self.animatedImage && [self delegateShouldStartAnimating]) {
_displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(changeKeyframe:)];
[_displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:self.runLoopMode];
}
} else {
[_displayLink invalidate];
_displayLink = nil;
}
return _displayLink;
}
- (NSString *)runLoopMode
{
return _runLoopMode ?: NSDefaultRunLoopMode;
}
- (void)setRunLoopMode:(NSString *)runLoopMode
{
if (runLoopMode != _runLoopMode) {
[self stopAnimating];
NSRunLoop *runloop = [NSRunLoop mainRunLoop];
[self.displayLink removeFromRunLoop:runloop forMode:_runLoopMode];
[self.displayLink addToRunLoop:runloop forMode:runLoopMode];
_runLoopMode = runLoopMode;
[self startAnimating];
}
}
- (void)setDelegate:(id<OLImageViewDelegate>)delegate
{
if (delegate == _delegate) {
return;
}
_delegate = delegate;
_delegateFlags.didLoop = [delegate respondsToSelector:@selector(imageViewDidLoop:)];
_delegateFlags.shouldStartAnimating = [delegate respondsToSelector:@selector(imageViewShouldStartAnimating:)];
}
- (void)setImage:(UIImage *)image
{
if (image == self.image) {
return;
}
[self stopAnimating];
self.currentFrameIndex = 0;
self.loopCountdown = 0;
self.accumulator = 0;
if ([image isKindOfClass:[OLImage class]] && image.images) {
[super setImage:nil];
self.animatedImage = (OLImage *)image;
self.loopCountdown = self.animatedImage.loopCount ?: NSUIntegerMax;
[self startAnimating];
} else {
self.animatedImage = nil;
[super setImage:image];
}
[self.layer setNeedsDisplay];
}
- (void)setAnimatedImage:(OLImage *)animatedImage
{
_animatedImage = animatedImage;
if (animatedImage == nil) {
self.layer.contents = nil;
self.layer.contentsScale = 1;
} else {
self.layer.contentsScale = animatedImage.scale;
}
}
#define OLCaseContentMode(aCase) \
case UIViewContentMode##aCase: \
contentsGravity = kCAGravity##aCase; \
break
- (void)setContentMode:(UIViewContentMode)contentMode
{
NSString *contentsGravity;
switch (contentMode) {
OLCaseContentMode(Bottom);
OLCaseContentMode(BottomLeft);
OLCaseContentMode(BottomRight);
OLCaseContentMode(Top);
OLCaseContentMode(TopLeft);
OLCaseContentMode(TopRight);
OLCaseContentMode(Center);
OLCaseContentMode(Right);
OLCaseContentMode(Left);
case UIViewContentModeScaleAspectFill:
contentsGravity = kCAGravityResizeAspectFill;
break;
case UIViewContentModeScaleAspectFit:
contentsGravity = kCAGravityResizeAspect;
break;
case UIViewContentModeScaleToFill:
case UIViewContentModeRedraw:
contentsGravity = kCAGravityResize;
break;
}
self.layer.contentsGravity = contentsGravity;
[super setContentMode:contentMode];
}
- (BOOL)isAnimating
{
return [super isAnimating] || (self.displayLink && !self.displayLink.isPaused);
}
- (void)stopAnimating
{
if (!self.animatedImage) {
[super stopAnimating];
return;
}
self.loopCountdown = 0;
self.displayLink.paused = YES;
}
- (void)startAnimating
{
if (!self.animatedImage) {
[super startAnimating];
return;
}
if (self.isAnimating) {
return;
}
self.loopCountdown = self.animatedImage.loopCount ?: NSUIntegerMax;
self.displayLink.paused = ! [self delegateShouldStartAnimating];
}
- (void)changeKeyframe:(CADisplayLink *)displayLink
{
if (self.currentFrameIndex >= [self.animatedImage.images count] && [self.animatedImage isPartial]) {
return;
}
self.accumulator += fmin(displayLink.duration, kMaxTimeStep);
while (self.accumulator >= self.animatedImage.frameDurations[self.currentFrameIndex]) {
self.accumulator -= self.animatedImage.frameDurations[self.currentFrameIndex];
if (++self.currentFrameIndex >= [self.animatedImage.images count] && ![self.animatedImage isPartial]) {
if (--self.loopCountdown == 0) {
[self stopAnimating];
return;
}
self.currentFrameIndex = 0;
[self delegateDidLoop];
}
self.currentFrameIndex = MIN(self.currentFrameIndex, [self.animatedImage.images count] - 1);
[self.layer setNeedsDisplay];
}
}
- (void)displayLayer:(CALayer *)layer
{
if (!self.animatedImage || [self.animatedImage.images count] == 0) {
return;
}
layer.contents = (__bridge id)([[self.animatedImage.images objectAtIndex:self.currentFrameIndex] CGImage]);
}
- (void)didMoveToWindow
{
[super didMoveToWindow];
if (self.window) {
[self startAnimating];
} else {
dispatch_async(dispatch_get_main_queue(), ^{
if (!self.window) {
[self stopAnimating];
}
});
}
}
- (void)didMoveToSuperview
{
[super didMoveToSuperview];
if (self.superview) {
//Has a superview, make sure it has a displayLink
[self displayLink];
} else {
//Doesn't have superview, let's check later if we need to remove the displayLink
dispatch_async(dispatch_get_main_queue(), ^{
[self displayLink];
});
}
}
- (void)setHighlighted:(BOOL)highlighted
{
if (!self.animatedImage) {
[super setHighlighted:highlighted];
}
}
- (UIImage *)image
{
return self.animatedImage ?: [super image];
}
- (CGSize)sizeThatFits:(CGSize)size
{
return self.image.size;
}
#pragma mark - delegation
- (BOOL)delegateShouldStartAnimating {
if (self.delegate && _delegateFlags.shouldStartAnimating) {
return [self.delegate imageViewShouldStartAnimating:self];
} else {
return YES;
}
}
- (void)delegateDidLoop {
if (self.delegate && _delegateFlags.didLoop) {
[self.delegate imageViewDidLoop:self];
}
}
@end