forked from overtake/telegram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPOPDecayAnimation.mm
executable file
·181 lines (146 loc) · 5.35 KB
/
POPDecayAnimation.mm
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
/**
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 "POPDecayAnimationInternal.h"
#if TARGET_OS_IPHONE
#import <UIKit/UIKit.h>
#endif
const POPValueType supportedVelocityTypes[6] = { kPOPValuePoint, kPOPValueInteger, kPOPValueFloat, kPOPValueRect, kPOPValueSize, kPOPValueEdgeInsets };
@implementation POPDecayAnimation
#pragma mark - Lifecycle
#undef __state
#define __state ((POPDecayAnimationState *)_state)
+ (instancetype)animation
{
return [[self alloc] init];
}
+ (instancetype)animationWithPropertyNamed:(NSString *)aName
{
POPDecayAnimation *anim = [self animation];
anim.property = [POPAnimatableProperty propertyWithName:aName];
return anim;
}
- (id)init
{
return [self _init];
}
- (void)_initState
{
_state = new POPDecayAnimationState(self);
}
#pragma mark - Properties
DEFINE_RW_PROPERTY(POPDecayAnimationState, deceleration, setDeceleration:, CGFloat, __state->toVec = NULL;);
@dynamic velocity;
- (id)toValue
{
[self _ensureComputedProperties];
return POPBox(__state->toVec, __state->valueType);
}
- (CFTimeInterval)duration
{
[self _ensureComputedProperties];
return __state->duration;
}
- (void)setFromValue:(id)fromValue
{
super.fromValue = fromValue;
[self _invalidateComputedProperties];
}
- (void)setToValue:(id)aValue
{
// no-op
MTLog(@"ignoring to value on decay animation %@", self);
}
- (id)reversedVelocity
{
id reversedVelocity = nil;
POPValueType velocityType = POPSelectValueType(self.originalVelocity, supportedVelocityTypes, POP_ARRAY_COUNT(supportedVelocityTypes));
if (velocityType == kPOPValueFloat) {
CGFloat originalVelocityFloat = [(NSNumber *)self.originalVelocity floatValue];
NSNumber *negativeOriginalVelocityNumber = @(-originalVelocityFloat);
reversedVelocity = negativeOriginalVelocityNumber;
} else if (velocityType == kPOPValueInteger) {
NSInteger originalVelocityInteger = [(NSNumber *)self.originalVelocity integerValue];
NSNumber *negativeOriginalVelocityNumber = @(-originalVelocityInteger);
reversedVelocity = negativeOriginalVelocityNumber;
} else if (velocityType == kPOPValuePoint) {
CGPoint originalVelocityPoint = [self.originalVelocity CGPointValue];
CGPoint negativeOriginalVelocityPoint = CGPointMake(-originalVelocityPoint.x, -originalVelocityPoint.y);
reversedVelocity = [NSValue valueWithCGPoint:negativeOriginalVelocityPoint];
} else if (velocityType == kPOPValueRect) {
CGRect originalVelocityRect = [self.originalVelocity CGRectValue];
CGRect negativeOriginalVelocityRect = CGRectMake(-originalVelocityRect.origin.x, -originalVelocityRect.origin.y, -originalVelocityRect.size.width, -originalVelocityRect.size.height);
reversedVelocity = [NSValue valueWithCGRect:negativeOriginalVelocityRect];
} else if (velocityType == kPOPValueSize) {
CGSize originalVelocitySize = [self.originalVelocity CGSizeValue];
CGSize negativeOriginalVelocitySize = CGSizeMake(-originalVelocitySize.width, -originalVelocitySize.height);
reversedVelocity = [NSValue valueWithCGSize:negativeOriginalVelocitySize];
} else if (velocityType == kPOPValueEdgeInsets) {
#if TARGET_OS_IPHONE
UIEdgeInsets originalVelocityInsets = [self.originalVelocity UIEdgeInsetsValue];
UIEdgeInsets negativeOriginalVelocityInsets = UIEdgeInsetsMake(-originalVelocityInsets.top, -originalVelocityInsets.left, -originalVelocityInsets.bottom, -originalVelocityInsets.right);
reversedVelocity = [NSValue valueWithUIEdgeInsets:negativeOriginalVelocityInsets];
#endif
}
return reversedVelocity;
}
- (id)originalVelocity
{
return POPBox(__state->originalVelocityVec, __state->valueType);
}
- (id)velocity
{
return POPBox(__state->velocityVec, __state->valueType);
}
- (void)setVelocity:(id)aValue
{
POPValueType valueType = POPSelectValueType(aValue, supportedVelocityTypes, POP_ARRAY_COUNT(supportedVelocityTypes));
if (valueType != kPOPValueUnknown) {
VectorRef vec = POPUnbox(aValue, __state->valueType, __state->valueCount, YES);
VectorRef origVec = POPUnbox(aValue, __state->valueType, __state->valueCount, YES);
if (!vec_equal(vec, __state->velocityVec)) {
__state->velocityVec = vec;
__state->originalVelocityVec = origVec;
if (__state->tracing) {
[__state->tracer updateVelocity:aValue];
}
[self _invalidateComputedProperties];
// automatically unpause active animations
if (__state->active && __state->paused) {
__state->fromVec = NULL;
__state->setPaused(false);
}
}
} else {
__state->velocityVec = NULL;
MTLog(@"Invalid velocity value for the decayAnimation: %@", aValue);
}
}
#pragma mark - Utility
- (void)_ensureComputedProperties
{
if (NULL == __state->toVec) {
__state->computeDuration();
__state->computeToValue();
}
}
- (void)_invalidateComputedProperties
{
__state->toVec = NULL;
__state->duration = 0;
}
- (void)_appendDescription:(NSMutableString *)s debug:(BOOL)debug
{
[super _appendDescription:s debug:debug];
if (0 != self.duration) {
[s appendFormat:@"; duration = %f", self.duration];
}
if (__state->deceleration) {
[s appendFormat:@"; deceleration = %f", __state->deceleration];
}
}
@end