CAAnimation循环引用

image

解决方案0


@protocol HYAnimationWeakDelegate 
@optional
- (void)animationDidStart:(CAAnimation *)anim;
- (void)animationDidStop:(CAAnimation *)anim;
@end

@interface HYAnimationDelegateManager : NSObject 
@property (weak, nonatomic) id delegate;
@end

@implementation HYAnimationDelegateManager
- (void)animationDidStart:(CAAnimation *)anim {
    if (_delegate && [_delegate respondsToSelector:@selector(animationDidStart:)]) {
        [_delegate animationDidStart:anim];
    }
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    if (_delegate && [_delegate respondsToSelector:@selector(animationDidStop123:)]) {
        [_delegate animationDidStop:anim];
    }
}
@end

//使用:
HYAnimationDelegateManager * manager = [HYAnimationDelegateManager new];
manager.delegate = self;
anim.delegate = manager;

解决方案1

声明一个单独的类实现delegate的回调

//.h
#import 

@interface AnimationDelegate : NSObject

@end

//.m
#import "AnimationDelegate.h"

@implementation AnimationDelegate

- (void)animationDidStart:(CAAnimation *)anim
{
    NSLog(@"Animation Start");
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    NSLog(@"Animation Stop");
}

- (void)dealloc
{
    NSLog(@"Delegate Dealloc");
}

@end

使用方式

self.animation.delegate = [[AnimationDelegate alloc]init];

eg: http://blog.csdn.net/a787188834/article/details/78303594

解决方式2:
使用NSProxy来解决
该类可直接引用YYKit中的YYWeakProxy类
使用方法

self.animation.delegate = [YYWeakProxy proxyWithTarget:self];

//NSTimer
https://www.jianshu.com/p/469bf43e522b

你可能感兴趣的:(CAAnimation循环引用)