macos - 为什么在 NSView 中使用大多数内容重力常量时会发生崩溃?

标签 macos cocoa core-animation

我有一个 NSView 子类,并在 initWithFrame 方法中设置以下内容:

self.wantsLayer = YES;
self.layer.contents = [NSImage imageNamed:@"sprite-sheet"];
self.layer.contentsGravity = kCAGravityLeft;

当我运行它时,我得到了预期的视觉结果......并且崩溃并显示以下日志:

** Misuse of NSImage and CALayer. contentsGravity is left. It should be one of resize, resizeAspect, or resizeAspectFill.

为什么?文档中没有任何迹象表明不应使用其他值。

最佳答案

实际上,您所看到的行为在文档中。它只是在 NSImage 文档中,而不是 CALayer 文档中(我强调):

Using Images with CALayer Objects

Although you can assign an NSImage object directly to the contents property of a CALayer object, doing so may not always yield the best results. Instead of using your image object, you can use the layerContentsForContentsScale: method to obtain an object that you can use for your layer’s contents. That method creates an image that is suited for use as the contents of a layer and that is supports all of the layer’s gravity modes. By contrast, the NSImage class supports only the kCAGravityResize, kCAGravityResizeAspect, and kCAGravityResizeAspectFill modes.

关于macos - 为什么在 NSView 中使用大多数内容重力常量时会发生崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22311705/

相关文章:

macos - 检测显示器是否支持 30 位颜色

objective-c - 检测正在运行的应用程序是否被沙盒化

wpf - 将 WPF 移植到 Cocoa(和/或反之亦然)

cocoa - 在 Mac 应用程序中加载 NPAPI 插件时遇到问题

objective-c - 获取路径的位置

c++ - 在 Mac OS X 上编辑 C++ 的好方法是什么?

objective-c - 如何从其他类(NSViewController 的子类)获取主窗口(App Delegate)?

Cocoa:NSView 上的按键事件未触发

cocoa - 调用-[CALayer setBorderColor :] with an RGB color

ios - 使用 CAAnimation.CurrentMediaTime() 更改显式动画的速度