forked from overtake/telegram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
POPBasicAnimationInternal.h
executable file
·96 lines (82 loc) · 2.76 KB
/
POPBasicAnimationInternal.h
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
/**
Copyright (c) 2014-present, Facebook, Inc.
All rights reserved.
This source code is licensed under the BSD-style license found in the
LICENSE file in the root directory of this source tree. An additional grant
of patent rights can be found in the PATENTS file in the same directory.
*/
#import "POPBasicAnimation.h"
#import "POPPropertyAnimationInternal.h"
// default animation duration
static CGFloat const kPOPAnimationDurationDefault = 0.4;
// progress threshold for computing done
static CGFloat const kPOPProgressThreshold = 1e-6;
static void interpolate(POPValueType valueType, NSUInteger count, const CGFloat *fromVec, const CGFloat *toVec, CGFloat *outVec, CGFloat p)
{
switch (valueType) {
case kPOPValueInteger:
case kPOPValueFloat:
case kPOPValuePoint:
case kPOPValueSize:
case kPOPValueRect:
case kPOPValueEdgeInsets:
case kPOPValueColor:
POPInterpolateVector(count, outVec, fromVec, toVec, p);
break;
default:
NSCAssert(false, @"unhandled type %d", valueType);
break;
}
}
struct _POPBasicAnimationState : _POPPropertyAnimationState
{
CAMediaTimingFunction *timingFunction;
double timingControlPoints[4];
CFTimeInterval duration;
CFTimeInterval timeProgress;
_POPBasicAnimationState(id __unsafe_unretained anim) : _POPPropertyAnimationState(anim),
timingFunction(nil),
timingControlPoints{0.},
duration(kPOPAnimationDurationDefault),
timeProgress(0.)
{
type = kPOPAnimationBasic;
}
bool isDone() {
if (_POPPropertyAnimationState::isDone()) {
return true;
}
return timeProgress + kPOPProgressThreshold >= 1.;
}
void updatedTimingFunction()
{
float vec[4] = {0.};
[timingFunction getControlPointAtIndex:1 values:&vec[0]];
[timingFunction getControlPointAtIndex:2 values:&vec[2]];
for (NSUInteger idx = 0; idx < POP_ARRAY_COUNT(vec); idx++) {
timingControlPoints[idx] = vec[idx];
}
}
bool advance(CFTimeInterval time, CFTimeInterval dt, id obj) {
// default timing function
if (!timingFunction) {
((POPBasicAnimation *)self).timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
}
// solve for normalized time, aka progresss [0, 1]
CGFloat p = 1.0f;
if (duration > 0.0f) {
// cap local time to duration
CFTimeInterval t = MIN(time - startTime, duration) / duration;
p = POPTimingFunctionSolve(timingControlPoints, t, SOLVE_EPS(duration));
timeProgress = t;
} else {
timeProgress = 1.;
}
// interpolate and advance
interpolate(valueType, valueCount, fromVec->data(), toVec->data(), currentVec->data(), p);
progress = p;
clampCurrentValue();
return true;
}
};
typedef struct _POPBasicAnimationState POPBasicAnimationState;