YHDecayAnimation.m
1.83 KB
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
//
// YHDecayAnimation.m
// YHAnimationDemo
//
// Created by gaoqiang xu on 2/11/15.
// Copyright (c) 2015 gaoqiang. All rights reserved.
//
#import "YHDecayAnimation.h"
#import "YHVelocityProxyInternal.h"
#import <objc/runtime.h>
#import "YHBeginTime.h"
#import "POPDecayAnimation.h"
static char kDecayAnimationProxyKey;
@implementation YHDecayAnimation
- (instancetype)initWithObject:(id)object {
self = [super initWithObject:object];
if (self) {
_deceleration = 0.998;
}
return self;
}
+ (NSString *)propertyNameForSelector:(SEL)selector {
return [self propertyNameFromGetterSelector:selector];
}
- (POPPropertyAnimation *)propertyAnimation {
POPDecayAnimation *animation = [POPDecayAnimation animation];
animation.deceleration = self.deceleration;
id velocity = [self.object yh_velocityProxy].velocity;
if (velocity) {
animation.velocity = velocity;
[self.object yh_velocityProxy].velocity = nil;
}
animation.beginTime = [self.object pop_beginTime];
animation.delegate = [self.object pop_delegate];
[self.object setPop_delegate:nil];
return animation;
}
@end
@implementation NSObject (YHDecayAnimation)
- (YHDecayAnimation *)yh_decayAnimationProxy {
YHDecayAnimation *proxy = objc_getAssociatedObject(self, &kDecayAnimationProxyKey);
if (!proxy) {
proxy = [[YHDecayAnimation alloc] initWithObject:self];
objc_setAssociatedObject(self, &kDecayAnimationProxyKey, proxy, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
return proxy;
}
- (instancetype)pop_decay {
return (id)[self yh_decayAnimationProxy];
}
- (CGFloat)pop_decayDeceleration {
return [self yh_decayAnimationProxy].deceleration;
}
- (void)setPop_decayDeceleration:(CGFloat)decayDeceleration {
[self yh_decayAnimationProxy].deceleration = decayDeceleration;
}
@end