#import <QuartzCore/CoreAnimation.h>
@protocol YLBAnimationWeakDelegate <NSObject>
@optional
- (void)animationDidStart:(CAAnimation *)anim;
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag;
@end
@interface YLBAnimationDelegateManager : NSObject <CAAnimationDelegate>
@property (weak, nonatomic) id<YLBAnimationWeakDelegate> delegate;
@end
@implementation YLBAnimationDelegateManager
- (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(animationDidStop:finished:)]) {
[_delegate animationDidStop:anim finished:flag];
}
}
@end
使用方法:
CAKeyframeAnimation *moveAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
YLBAnimationDelegateManager * manager = [YLBAnimationDelegateManager new]; //创建实例
manager.delegate = self; //进行弱引用
moveAnimation.delegate = manager; //进行强引用