YHDecayAnimation.m 1.83 KB
//
//  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