更改CALayer属性:EXC_BAD_INSTRUCTION [英] Change CALayer properties: EXC_BAD_INSTRUCTION

查看:121
本文介绍了更改CALayer属性:EXC_BAD_INSTRUCTION的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过单击子图层来实现对子图层的选择,并通过例如更改子图层的背景颜色来可视化已选择该子图层。但是,无论如何,我最终都会遇到以下错误,但是我尝试更改任何属性:

I'm trying to implement selection of a subLayer by clicking on it and visualize that it is had been selected by for example changing the background color of the subLayer. However, I end up with the following error whatever and however I attempt to change any property:

Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

她是从我的调用的函数NSView 子类' mouseUp 函数。

Her is my function that is called from my NSViewsub class' mouseUpfunction.

func SelectObject( atPoint: NSPoint)
{
    let thePoint = atPoint as CGPoint
    if let hitLayer = itsCourseLayer.hitTest( thePoint) {
        if (hitLayer != itsCourseLayer) {
            // Gives following error:
            // Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
            hitLayer.backgroundColor = NSColor.red.cgColor 
        }
    }
}

我也尝试过 hitLayer.setValue(NSColor。 red.cgColor,forKeyPath: backgroundColor),而我尝试用 CATransaction.begin() CATransaction.commit包围它(),或更改subLayer的其他任何属性。

I've also tried hitLayer.setValue( NSColor.red.cgColor, forKeyPath: "backgroundColor") and I've tried to surround it with CATransaction.begin() and CATransaction.commit(), or to change any other property of the subLayer. But with no success.

任何想法我的代码有什么问题吗?

Any Idea what is wrong with my code?

推荐答案

该错误消息告诉您一个解决方案:在 CPCoursePointLayer 中实现 init(layer:)

The error message is kind of telling you one solution: implement init(layer:) in CPCoursePointLayer.

在图层上设置属性,并且该图层位于非隐藏窗口的图层树中时,Core Animation将寻找 CAAction ,通过发送 actionForKey:消息到图层。如果该搜索返回 nil ,Core Animation将使用一些默认参数为该属性创建一个隐式动画。

When you set a property on a layer, and that layer is in a non-hidden window's layer tree, Core Animation looks for a CAAction for that property, by sending the actionForKey: message to the layer. If that search returns nil, Core Animation creates an implicit animation for the property using some default parameters.

任何层附带动画的视频具有相应的表示层。 (阅读层树反映了动画状态的不同方面以获取更多信息。)因此,Core Animation需要为您的 hitLayer 创建相应的表示层。 。它使用 CPCoursePointLayer 类上的 init(layer:)初始化程序完成此操作。

Any layer with an attached animation has a corresponding presentation layer. (Read Layer Trees Reflect Different Aspects of the Animation State for more info.) So Core Animation needs to create the corresponding presentation layer for your hitLayer. It does this using the init(layer:) initializer on your CPCoursePointLayer class.

在Swift中,一个类不会自动继承其超类的所有构造函数,因此您的 CPCoursePointLayer 类没有该初始化程序。因此,您的应用程序崩溃了。

In Swift, a class doesn't automatically inherit all of its superclass's constructors, so your CPCoursePointLayer class doesn't have that initializer. So your app crashes.

一种解决方案是将 init(layer:)初始值设定项添加到您的 CPCoursePointLayer 类:

One solution is to add the init(layer:) initializer to your CPCoursePointLayer class:

init(layer: CALayer) {
    let layer = layer as! CPCoursePointLayer
    // copy any custom properties from layer to self here
    super.init(layer: layer)
}

如果定义 init(layer:),则可以对图层的属性进行动画处理,并允许它们隐式地进行动画处理

If you define init(layer:), then you can animate properties of your layer, and allow them to be implicitly animated.

如果您从未计划过对图层的属性进行显式或隐式动画处理,则可以通过返回 NSNull 当Core Animation搜索动作时,例如通过将此方法添加到您的 CPCoursePointLayer 类中:

If you never plan to explicitly or implicitly animate properties of your layer, then you can instead disable implicit animations by returning NSNull when Core Animation searches for an action, for example by adding this method to your CPCoursePointLayer class:

override class func defaultAction(forKey key: String) -> CAAction? {
    return unsafeBitCast(NSNull(), to: CAAction?.self)
}

这篇关于更改CALayer属性:EXC_BAD_INSTRUCTION的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆