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

#import "YHStopProxy.h"
#import "POP.h"
#import <objc/runtime.h>

static char kStopAnimationProxyKey;

@implementation YHStopProxy

+ (NSString *)propertyNameForSelector:(SEL)selector {
    return [self propertyNameFromGetterSelector:selector];
}

/**
 *  停止对应的动画
    对该对象发送消息,只要对应到动画属性,就会取出对应的动画并停止掉
 *
 *  @param propertyName 动画属性
 *  @param value        值
 */
- (void)completeInvocationWithPropertyName:(NSString *)propertyName andValue:(id)value {
    if ([propertyName hasPrefix:@"pop_"]) {
        propertyName = [propertyName substringFromIndex:4];
    }
    
    // remove animation
    POPPropertyAnimation *animation = [self.object pop_animationForKey:propertyName];
    if (animation) {
        [self.object pop_removeAnimationForKey:propertyName];
    }
}

@end

@implementation NSObject (YHStopProxy)

- (YHStopProxy *)yh_stopAnimationProxy {
    YHStopProxy *proxy = objc_getAssociatedObject(self, &kStopAnimationProxyKey);
    if (!proxy) {
        proxy = [[YHStopProxy alloc] initWithObject:self];
        objc_setAssociatedObject(self, &kStopAnimationProxyKey, proxy, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return proxy;
}

- (instancetype)pop_stop {
    return (id)[self yh_stopAnimationProxy];
}

@end