我有一个CALayer
其中有一个CAKeyFrameAnimation
.
有没有办法检测内容的变化CALayer
?
就像,每当 的内容(图像)CALayer
由于 而改变时CAKeyFrameAnimation
,我想用 播放短的声音AVAudioPlayer
。
更新
我通过这样做解决了它。
- (void) viewDidLoad
{
// init CADisplayLink to catch the changing moment
CADisplayLink *displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(checkContents:)];
[displayLink setFrameInterval:6] // checking by every 0.1 sec (6 frames)
[displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
}
- (void) checkContents:(CADisplayLink *) sender;
{
// _newImage and _oldImage are class variables. (UIImage)
// animatingView is a `UIImageView` where its layer's contents is being changed by `CAKeyFrameAnimation`
CALayer *presentLayer = [animatingView.layer presentationLayer];
_newImage = presentLayer.contents;
if ( ![_newimage isEqual:_oldImage] )
{
NSLog(@"Image changed! From %@ to %@", _oldImage, _newImage);
}
_oldImage = _newImage;
}
当然,不要忘记在CADisplayLink
不再需要它时使其无效。