【问题标题】:iOS - Detecting change of contents of CALayeriOS - 检测 CALayer 内容的变化
【发布时间】: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


    【解决方案1】:

    您似乎需要CADisplayLink 来完成这项工作,如下所述:Detecting collision, during a CAKeyFrameAnimation

    基本上:

    CADisplayLink 对象是一个计时器对象,它允许您的应用程序将其绘图与显示器的刷新率同步。

    您的应用程序创建一个新的显示链接,提供一个目标对象和一个选择器,以便在屏幕更新时调用。接下来,您的应用程序将显示链接添加到运行循环。

    来源:Apple's documentation on CADisplayLink

    【讨论】:

    • 感谢您的快速回答!我试试看!
    猜你喜欢
    • 2012-12-23
    • 1970-01-01
    • 1970-01-01
    • 2010-11-08
    • 2020-01-28
    • 1970-01-01
    • 1970-01-01
    • 2021-11-30
    • 1970-01-01
    相关资源
    最近更新 更多