杨锴
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
/**
 * 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.
 */
//
//  QMUIAnimationHelper.m
//  WeRead
//
//  Created by zhoonchen on 2018/9/3.
//
 
#import "QMUIAnimationHelper.h"
#import "QMUICore.h"
 
#define SpringDefaultMass 1.0
#define SpringDefaultDamping 18.0
#define SpringDefaultStiffness 82.0
#define SpringDefaultInitialVelocity 0.0
 
@implementation QMUIAnimationHelper
 
+ (CGFloat)bounceFromValue:(CGFloat)fromValue toValue:(CGFloat)toValue time:(CGFloat)time coeff:(CGFloat)coeff {
    // 以下算法来源于社区:
    // How UIScrollView works: https://medium.com/@esskeetit/how-uiscrollview-works-e418adc47060
    // Grant Paul's Twitter: https://twitter.com/chpwn/status/285540192096497664
    coeff = coeff == -1 ? 0.55 : coeff;// 0.55 为系统 UIScrollView 的默认系数,这里我们也将其作为我们的默认系数
    CGFloat d = toValue;
    CGFloat x = (d - fromValue) * time;
    CGFloat result = fromValue + d + 1.0 - (1.0 / (coeff * x / d + 1)) * d;
//    NSLog(@"[%.2f-%.2f], coeff = %.2f, x = %.2f, result = %.2f", fromValue, d, coeff, x, result);
    return result;
}
 
+ (id)interpolateFromValue:(id)fromValue
                   toValue:(id)toValue
                      time:(CGFloat)time
                    easing:(QMUIAnimationEasings)easing {
    return [self interpolateSpringFromValue:fromValue toValue:toValue time:time mass:SpringDefaultMass damping:SpringDefaultDamping stiffness:SpringDefaultStiffness initialVelocity:SpringDefaultInitialVelocity easing:easing];
}
 
/*
 * 插值器,遇到新的类型再添加
 */
+ (id)interpolateSpringFromValue:(id)fromValue
                         toValue:(id)toValue
                            time:(CGFloat)time
                            mass:(CGFloat)mass
                         damping:(CGFloat)damping
                       stiffness:(CGFloat)stiffness
                 initialVelocity:(CGFloat)initialVelocity
                          easing:(QMUIAnimationEasings)easing {
    
    if ([fromValue isKindOfClass:[NSNumber class]]) { // NSNumber
        CGFloat from = [fromValue floatValue];
        CGFloat to = [toValue floatValue];
        CGFloat result = interpolateSpring(from, to, time, easing, mass, damping, stiffness, initialVelocity);
        return [NSNumber numberWithFloat:result];
    }
    
    else if ([fromValue isKindOfClass:[UIColor class]]) { // UIColor
        UIColor *from = (UIColor *)fromValue;
        UIColor *to = (UIColor *)toValue;
        CGFloat fromRed, toRed, curRed = 0;
        CGFloat fromGreen, toGreen, curGreen = 0;
        CGFloat fromBlue, toBlue, curBlue = 0;
        CGFloat fromAlpha, toAlpha, curAlpha = 0;
        [from getRed:&fromRed green:&fromGreen blue:&fromBlue alpha:&fromAlpha];
        [to getRed:&toRed green:&toGreen blue:&toBlue alpha:&toAlpha];
        curRed = interpolateSpring(fromRed, toRed, time, easing, mass, damping, stiffness, initialVelocity);
        curGreen = interpolateSpring(fromGreen, toGreen, time, easing, mass, damping, stiffness, initialVelocity);
        curBlue = interpolateSpring(fromBlue, toBlue, time, easing, mass, damping, stiffness, initialVelocity);
        curAlpha = interpolateSpring(fromAlpha, toAlpha, time, easing, mass, damping, stiffness, initialVelocity);
        UIColor *result = [UIColor colorWithRed:curRed green:curGreen blue:curBlue alpha:curAlpha];
        return result;
    }
    
    else if ([fromValue isKindOfClass:[NSValue class]]) { // NSValue
        const char *type = [(NSValue *)fromValue objCType];
        if (strcmp(type, @encode(CGPoint)) == 0) {
            CGPoint from = [fromValue CGPointValue];
            CGPoint to = [toValue CGPointValue];
            CGPoint result = CGPointMake(interpolateSpring(from.x, to.x, time, easing, mass, damping, stiffness, initialVelocity), interpolateSpring(from.y, to.y, time, easing, mass, damping, stiffness, initialVelocity));
            return [NSValue valueWithCGPoint:result];
        }
        else if (strcmp(type, @encode(CGSize)) == 0) {
            CGSize from = [fromValue CGSizeValue];
            CGSize to = [toValue CGSizeValue];
            CGSize result = CGSizeMake(interpolateSpring(from.width, to.width, time, easing, mass, damping, stiffness, initialVelocity), interpolateSpring(from.height, to.height, time, easing, mass, damping, stiffness, initialVelocity));
            return [NSValue valueWithCGSize:result];
        }
        else if (strcmp(type, @encode(CGRect)) == 0) {
            CGRect from = [fromValue CGRectValue];
            CGRect to = [toValue CGRectValue];
            CGRect result = CGRectMake(interpolateSpring(from.origin.x, to.origin.x, time, easing, mass, damping, stiffness, initialVelocity), interpolateSpring(from.origin.y, to.origin.y, time, easing, mass, damping, stiffness, initialVelocity), interpolateSpring(from.size.width, to.size.width, time, easing, mass, damping, stiffness, initialVelocity), interpolateSpring(from.size.height, to.size.height, time, easing, mass, damping, stiffness, initialVelocity));
            return [NSValue valueWithCGRect:result];
        }
        else if (strcmp(type, @encode(CGAffineTransform)) == 0) {
            CGAffineTransform from = [fromValue CGAffineTransformValue];
            CGAffineTransform to = [toValue CGAffineTransformValue];
            CGAffineTransform result = CGAffineTransformIdentity;
            result.a = interpolateSpring(from.a, to.a, time, easing, mass, damping, stiffness, initialVelocity);
            result.b = interpolateSpring(from.b, to.b, time, easing, mass, damping, stiffness, initialVelocity);
            result.c = interpolateSpring(from.c, to.c, time, easing, mass, damping, stiffness, initialVelocity);
            result.d = interpolateSpring(from.d, to.d, time, easing, mass, damping, stiffness, initialVelocity);
            result.tx = interpolateSpring(from.tx, to.tx, time, easing, mass, damping, stiffness, initialVelocity);
            result.ty = interpolateSpring(from.ty, to.ty, time, easing, mass, damping, stiffness, initialVelocity);
            return [NSValue valueWithCGAffineTransform:result];
        }
        else if (strcmp(type, @encode(UIEdgeInsets)) == 0) {
            UIEdgeInsets from = [fromValue UIEdgeInsetsValue];
            UIEdgeInsets to = [toValue UIEdgeInsetsValue];
            UIEdgeInsets result = UIEdgeInsetsZero;
            result.top = interpolateSpring(from.top, to.top, time, easing, mass, damping, stiffness, initialVelocity);
            result.left = interpolateSpring(from.left, to.left, time, easing, mass, damping, stiffness, initialVelocity);
            result.bottom = interpolateSpring(from.bottom, to.bottom, time, easing, mass, damping, stiffness, initialVelocity);
            result.right = interpolateSpring(from.right, to.right, time, easing, mass, damping, stiffness, initialVelocity);
            return [NSValue valueWithUIEdgeInsets:result];
        }
    }
    
    return (time < 0.5) ? fromValue: toValue;
}
 
CGFloat interpolate(CGFloat from, CGFloat to, CGFloat time, QMUIAnimationEasings easing) {
    return interpolateSpring(from, to, time, easing, SpringDefaultMass, SpringDefaultDamping, SpringDefaultStiffness, SpringDefaultInitialVelocity);
}
 
CGFloat interpolateSpring(CGFloat from, CGFloat to, CGFloat time, QMUIAnimationEasings easing, CGFloat springMass, CGFloat springDamping, CGFloat springStiffness, CGFloat springInitialVelocity) {
    switch (easing) {
        case QMUIAnimationEasingsLinear:
            time = QMUI_Linear(time);
            break;
        case QMUIAnimationEasingsEaseInSine:
            time = QMUI_EaseInSine(time);
            break;
        case QMUIAnimationEasingsEaseOutSine:
            time = QMUI_EaseOutSine(time);
            break;
        case QMUIAnimationEasingsEaseInOutSine:
            time = QMUI_EaseInOutSine(time);
            break;
        case QMUIAnimationEasingsEaseInQuad:
            time = QMUI_EaseInQuad(time);
            break;
        case QMUIAnimationEasingsEaseOutQuad:
            time = QMUI_EaseOutQuad(time);
            break;
        case QMUIAnimationEasingsEaseInOutQuad:
            time = QMUI_EaseInOutQuad(time);
            break;
        case QMUIAnimationEasingsEaseInCubic:
            time = QMUI_EaseInCubic(time);
            break;
        case QMUIAnimationEasingsEaseOutCubic:
            time = QMUI_EaseOutCubic(time);
            break;
        case QMUIAnimationEasingsEaseInOutCubic:
            time = QMUI_EaseInOutCubic(time);
            break;
        case QMUIAnimationEasingsEaseInQuart:
            time = QMUI_EaseInQuart(time);
            break;
        case QMUIAnimationEasingsEaseOutQuart:
            time = QMUI_EaseOutQuart(time);
            break;
        case QMUIAnimationEasingsEaseInOutQuart:
            time = QMUI_EaseInOutQuart(time);
            break;
        case QMUIAnimationEasingsEaseInQuint:
            time = QMUI_EaseInQuint(time);
            break;
        case QMUIAnimationEasingsEaseOutQuint:
            time = QMUI_EaseOutQuint(time);
            break;
        case QMUIAnimationEasingsEaseInOutQuint:
            time = QMUI_EaseInOutQuint(time);
            break;
        case QMUIAnimationEasingsEaseInExpo:
            time = QMUI_EaseInExpo(time);
            break;
        case QMUIAnimationEasingsEaseOutExpo:
            time = QMUI_EaseOutExpo(time);
            break;
        case QMUIAnimationEasingsEaseInOutExpo:
            time = QMUI_EaseInOutExpo(time);
            break;
        case QMUIAnimationEasingsEaseInCirc:
            time = QMUI_EaseInCirc(time);
            break;
        case QMUIAnimationEasingsEaseOutCirc:
            time = QMUI_EaseOutCirc(time);
            break;
        case QMUIAnimationEasingsEaseInOutCirc:
            time = QMUI_EaseInOutCirc(time);
            break;
        case QMUIAnimationEasingsEaseInBack:
            time = QMUI_EaseInBack(time);
            break;
        case QMUIAnimationEasingsEaseOutBack:
            time = QMUI_EaseOutBack(time);
            break;
        case QMUIAnimationEasingsEaseInOutBack:
            time = QMUI_EaseInOutBack(time);
            break;
        case QMUIAnimationEasingsEaseInElastic:
            time = QMUI_EaseInElastic(time);
            break;
        case QMUIAnimationEasingsEaseOutElastic:
            time = QMUI_EaseOutElastic(time);
            break;
        case QMUIAnimationEasingsEaseInOutElastic:
            time = QMUI_EaseInOutElastic(time);
            break;
        case QMUIAnimationEasingsEaseInBounce:
            time = QMUI_EaseInBounce(time);
            break;
        case QMUIAnimationEasingsEaseOutBounce:
            time = QMUI_EaseOutBounce(time);
            break;
        case QMUIAnimationEasingsEaseInOutBounce:
            time = QMUI_EaseInOutBounce(time);
            break;
        case QMUIAnimationEasingsSpring:
            time = QMUI_EaseSpring(time, springMass, springDamping, springStiffness, springInitialVelocity);
            break;
        case QMUIAnimationEasingsSpringKeyboard:
            time = QMUI_EaseSpring(time, SpringDefaultMass, SpringDefaultDamping, SpringDefaultStiffness, SpringDefaultInitialVelocity);
            break;
        default:
            time = QMUI_Linear(time);
            break;
    }
    return (to - from) * time + from;
}
 
@end