YHVelocityProxy.m 1.55 KB
//
//  YHVelocityProxy.m
//  YHAnimationDemo
//
//  Created by gaoqiang xu on 2/11/15.
//  Copyright (c) 2015 gaoqiang. All rights reserved.
//

#import "YHVelocityProxy.h"
#import "YHVelocityProxyInternal.h"
#import <objc/runtime.h>

static char kVelocityProxyKey;

@implementation YHVelocityProxy

+ (NSString *)propertyNameForSelector:(SEL)selector {
    NSString *selectorName = NSStringFromSelector(selector);
    if (![selectorName hasPrefix:@"set"]) {
        [NSException raise:NSInternalInconsistencyException format:@"Spring animation only takes setters."];
    }
    
    NSString *propertyName = [selectorName substringWithRange:NSMakeRange(3, [selectorName length]-4)];
    propertyName = [[[propertyName substringWithRange:NSMakeRange(0, 1)] lowercaseString] stringByAppendingString:[propertyName substringFromIndex:1]];
    
    return propertyName;
}

- (void)completeInvocationWithPropertyName:(NSString *)propertyName andValue:(id)value {
    self.velocity = value;
}

@end

@implementation NSObject (YHVelocityProxyInternal)

- (YHVelocityProxy *)yh_velocityProxy {
    YHVelocityProxy *proxy = objc_getAssociatedObject(self, &kVelocityProxyKey);
    if (!proxy) {
        proxy = [[YHVelocityProxy alloc] initWithObject:self];
        objc_setAssociatedObject(self, &kVelocityProxyKey, proxy, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return proxy;
}

@end

@implementation NSObject (YHVelocityProxy)

- (id)pop_velocity {
    return (id)[self yh_velocityProxy];
}

- (void)setPop_velocity:(id)velocity {
    [self yh_velocityProxy].velocity = velocity;
}

@end