【发布时间】:2014-02-13 14:06:51
【问题描述】:
我有一个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 时使其无效。
【问题讨论】:
标签: ios calayer cakeyframeanimation cadisplaylink