杨锴
2024-08-14 909e20941e45f8712c012db602034b47da0bfdb0
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
281
282
283
284
285
286
287
288
289
290
/**
 * Tencent is pleased to support the open source community by making QMUI_iOS available.
 * Copyright (C) 2016-2021 THL A29 Limited, a Tencent company. All rights reserved.
 * Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * http://opensource.org/licenses/MIT
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 */
//
//  QMUIDisplayLinkAnimation.m
//  WeRead
//
//  Created by zhoonchen on 2018/9/3.
//
 
#import "QMUIDisplayLinkAnimation.h"
#import "QMUICore.h"
 
@interface QMUIDisplayLinkAnimation ()
 
@property(nonatomic, strong, readwrite) CADisplayLink *displayLink;
 
@property(nonatomic, assign) NSTimeInterval timeOffset;
@property(nonatomic, assign) NSInteger curRepeatCount;
@property(nonatomic, assign) BOOL isReversing;
 
@end
 
@implementation QMUIDisplayLinkAnimation
 
- (instancetype)init {
    self = [super init];
    if (self) {
        self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(handleDisplayLink:)];
        self.fromValue = nil;
        self.toValue = nil;
        self.duration = 0;
        self.repeatCount = 0;
        self.easing = QMUIAnimationEasingsLinear;
        self.timeOffset = 0;
        self.animation = nil;
    }
    return self;
}
 
- (instancetype)initWithDuration:(CFTimeInterval)duration
                          easing:(QMUIAnimationEasings)easing
                       fromValue:(id)fromValue
                         toValue:(id)toValue
                       animation:(void (^)(id curValue))animation {
    if (self = [self init]) {
        self.duration = duration;
        self.easing = easing;
        self.fromValue = fromValue;
        self.toValue = toValue;
        self.animation = animation;
    }
    return self;
}
 
- (instancetype)initWithDuration:(CFTimeInterval)duration
                          easing:(QMUIAnimationEasings)easing
                      animations:(void (^)(QMUIDisplayLinkAnimation *animation, CGFloat curTime))animations {
    if (self = [self init]) {
        self.duration = duration;
        self.easing = easing;
        self.animations = animations;
    }
    return self;
}
 
- (void)dealloc {
    [_displayLink invalidate];
    _displayLink = nil;
}
 
- (void)startAnimation {
    if (!self.displayLink) {
        return;
    }
    if (self.displayLink.paused) {
        self.displayLink.paused = NO;
        return;
    }
    if (self.beginTime > 0) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.beginTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            if (self.displayLink) {
                if (self.willStartAnimation) {
                    self.willStartAnimation();
                }
                [self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
            }
        });
    } else {
        if (self.willStartAnimation) {
            self.willStartAnimation();
        }
        [self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
    }
}
 
- (void)stopAnimation {
    [self.displayLink invalidate];
    self.displayLink = nil;
    if (self.didStopAnimation) {
        self.didStopAnimation();
    }
}
 
- (void)handleDisplayLink:(CADisplayLink *)displayLink {
    if (!self.animation && !self.animations) {
        return;
    }
    NSTimeInterval oneFrame = 1.0 / [self preferredFramesPerSecond];
    if (self.autoreverses && self.isReversing) {
        self.timeOffset = MAX(self.timeOffset - oneFrame, 0);
    } else {
        self.timeOffset = MIN(self.timeOffset + oneFrame, self.duration);
    }
    CGFloat time = self.timeOffset / self.duration;
    if (self.animations) {
        self.animations(self, time);
    } else if (self.animation) {
        id curValue = [QMUIAnimationHelper interpolateFromValue:self.fromValue toValue:self.toValue time:time easing:self.easing];
        self.animation(curValue);
    }
    if (self.timeOffset >= self.duration) {
        [self beginToDecrease];
    } else if (self.timeOffset <= 0) {
        [self beginToIncrease];
    }
}
 
- (void)beginToIncrease {
    if (self.repeat && self.repeatCount > 0) {
        self.curRepeatCount++;
    }
    if (self.autoreverses) {
        self.isReversing = NO;
    }
    if (self.curRepeatCount >= self.repeatCount) {
        [self stopAnimation];
    }
}
 
- (void)beginToDecrease {
    if (self.repeat && self.repeatCount > 0) {
        self.curRepeatCount++;
    }
    if (self.repeat) {
        if (self.autoreverses) {
            self.isReversing = YES;
        } else {
            self.timeOffset = 0;
        }
        if (self.curRepeatCount >= self.repeatCount) {
            [self stopAnimation];
        }
    } else {
        [self stopAnimation];
    }
}
 
- (NSInteger)preferredFramesPerSecond {
    if (self.displayLink.preferredFramesPerSecond == 0) {
        // 不能写死60,而要拿当前设备支持的最大帧率来计算。根据 CADisplayLink 的官方文档,如果返回一个超过当前设备实际帧率的数字,实际依然会用设备实际帧率来计算,所以不用担心设备降频导致帧率降低后动画时长是否有问题。
        return UIScreen.mainScreen.maximumFramesPerSecond;
    }
    return self.displayLink.preferredFramesPerSecond;
}
 
@end
 
 
@implementation QMUIDisplayLinkAnimation (ConvenienceClassMethod)
 
+ (instancetype)springAnimateWithFromValue:(id)fromValue
                                   toValue:(id)toValue
                                 animation:(void (^)(id curValue))animation
                              createdBlock:(void (^)(QMUIDisplayLinkAnimation *animation))createdBlock {
    return [self animateWithDuration:SpringAnimationDefaultDuration
                              easing:QMUIAnimationEasingsSpringKeyboard
                           fromValue:fromValue
                             toValue:toValue
                           animation:animation
                        createdBlock:createdBlock];
}
 
+ (instancetype)animateWithDuration:(NSTimeInterval)duration
                             easing:(QMUIAnimationEasings)easing
                          fromValue:(id)fromValue
                            toValue:(id)toValue
                          animation:(void (^)(id curValue))animation {
    return [self animateWithDuration:duration
                              easing:easing
                           fromValue:fromValue
                             toValue:toValue
                           animation:animation
                        createdBlock:nil];
}
 
+ (instancetype)animateWithDuration:(NSTimeInterval)duration
                             easing:(QMUIAnimationEasings)easing
                          fromValue:(id)fromValue
                            toValue:(id)toValue
                          animation:(void (^)(id curValue))animation
                       createdBlock:(void (^)(QMUIDisplayLinkAnimation *animation))createdBlock {
    return [self animateWithDuration:duration
                              easing:easing
                           fromValue:fromValue
                             toValue:toValue
                           animation:animation
                        createdBlock:createdBlock
                        didStopBlock:nil];
}
 
+ (instancetype)animateWithDuration:(NSTimeInterval)duration
                             easing:(QMUIAnimationEasings)easing
                          fromValue:(id)fromValue
                            toValue:(id)toValue
                          animation:(void (^)(id curValue))animation
                       createdBlock:(void (^)(QMUIDisplayLinkAnimation *animation))createdBlock
                       didStopBlock:(void (^)(QMUIDisplayLinkAnimation *animation))didStopBlock {
    QMUIDisplayLinkAnimation *displayLinkAnimation = [[self alloc] initWithDuration:duration
                                                                                                 easing:easing
                                                                                              fromValue:fromValue
                                                                                                toValue:toValue
                                                                                              animation:animation];
    if (createdBlock) {
        createdBlock(displayLinkAnimation);
    }
    __weak QMUIDisplayLinkAnimation *weakDisplayLinkAnimation = displayLinkAnimation;
    displayLinkAnimation.didStopAnimation = ^{
        if (didStopBlock) {
            didStopBlock(weakDisplayLinkAnimation);
        }
    };
    [displayLinkAnimation startAnimation];
    return displayLinkAnimation;
}
 
+ (instancetype)springAnimateWithAnimations:(void (^)(QMUIDisplayLinkAnimation *animation, CGFloat curTime))animations
                               createdBlock:(void (^)(QMUIDisplayLinkAnimation *animation))createdBlock {
    return [self animateWithDuration:SpringAnimationDefaultDuration
                              easing:QMUIAnimationEasingsSpringKeyboard
                          animations:animations
                        createdBlock:createdBlock];
}
 
+ (instancetype)animateWithDuration:(NSTimeInterval)duration
                             easing:(QMUIAnimationEasings)easing
                         animations:(void (^)(QMUIDisplayLinkAnimation *animation, CGFloat curTime))animations {
    return [self animateWithDuration:duration
                              easing:easing
                          animations:animations
                        createdBlock:nil];
}
 
+ (instancetype)animateWithDuration:(NSTimeInterval)duration
                             easing:(QMUIAnimationEasings)easing
                         animations:(void (^)(QMUIDisplayLinkAnimation *animation, CGFloat curTime))animations
                       createdBlock:(void (^)(QMUIDisplayLinkAnimation *animation))createdBlock {
    return [self animateWithDuration:duration
                              easing:easing
                          animations:animations
                        createdBlock:createdBlock
                        didStopBlock:nil];
}
 
+ (instancetype)animateWithDuration:(NSTimeInterval)duration
                             easing:(QMUIAnimationEasings)easing
                         animations:(void (^)(QMUIDisplayLinkAnimation *animation, CGFloat curTime))animations
                       createdBlock:(void (^)(QMUIDisplayLinkAnimation *animation))createdBlock
                       didStopBlock:(void (^)(QMUIDisplayLinkAnimation *animation))didStopBlock {
    QMUIDisplayLinkAnimation *displayLinkAnimation = [[self alloc] initWithDuration:duration
                                                                                                 easing:easing
                                                                                             animations:animations];
    if (createdBlock) {
        createdBlock(displayLinkAnimation);
    }
    __weak QMUIDisplayLinkAnimation *weakDisplayLinkAnimation = displayLinkAnimation;
    displayLinkAnimation.didStopAnimation = ^{
        if (didStopBlock) {
            didStopBlock(weakDisplayLinkAnimation);
        }
    };
    [displayLinkAnimation startAnimation];
    return displayLinkAnimation;
}
 
@end