在多平台项目上找不到 UIBezierPath

发布于 2025-01-11 06:00:57 字数 95 浏览 0 评论 0原文

我在 Xcode 中创建了一个多平台游戏,UIBezierPath 给出了“在范围内找不到‘UIBezierPath’”错误。我确信我只是缺少导入一个套件,但我不知道是哪一个。

I created a multi-platform game in Xcode and UIBezierPath gives a "Cannot find 'UIBezierPath' in scope" error. I'm sure I'm just missing importing a kit but I don't know which one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可可 2025-01-18 06:00:57

简短的回答:如果您尝试使用 UIBezierPath 您需要导入 UIKit

更长的注释“多平台”:您的问题没有提供太多上下文..但是如果您的意思是您开始为 iOS 开发 UIBezierPath,然后切换到不同的平台目标(例如:macOS),那么是的,您的许多类将无法工作。通常(但并非总是),macOS 上有一个等效类,它使用“NS”前缀而不是“UI”前缀。 (NS 是对“下一步”的传统引用)。因此,当您面向 macOS 时,请尝试使用 AppKit 类(例如 NSBezierPath),而不是 UIKit 类。

#if os(macOS)
import AppKit
#elseif os(iOS)
import UIKit
#endif 

警告:UI/NS 类经常有不同的实现。因此,有不同的构造函数、函数调用等是正常的。这意味着您通常必须使用上面的 if/elseif/endif 结构在代码中拥有两个实现。是的,这可能很乏味。

更新

这是在 macOS 和 iOS 中绘制椭圆的示例。注意:macOS 版本使用 CGMutablePath (因为在 macOS 上没有像 iOS 那样从 NSBezierPath 构建 SKShapeNode 的简单方法)。如果您确实想使用 NSBezierPath,您也可以尝试这些技术用于将 NSBezierPath 转换为 CGPath

override func didMove(to view: SKView) {
    let rect = CGRect(x: 0, y: 0, width: 200, height: 200)
    
    #if os(macOS)
    let path = CGMutablePath(ellipseIn:rect, transform:nil)
    let shape = SKShapeNode(path:path)
    
    #elseif os(iOS)
    let path = UIBezierPath(ovalIn: rect)
    let shape = SKShapeNode(path: path.cgPath)
    #endif
    
    addChild(shape)
}

short answer: if you're trying to use UIBezierPath you need to import UIKit

longer note re "multi-platform": your question doesn't provide much context.. but if you mean you started developing a UIBezierPath for iOS, and then switched to a different platform target (ex: macOS), then yes many of your classes won't work. often, but not always, there is simply an equivalent class for macOS that uses the "NS" prefix instead of the "UI" prefix. (NS is a legacy reference to "Next Step"). so try using AppKit classes like NSBezierPath when you target macOS, instead of UIKit classes.

#if os(macOS)
import AppKit
#elseif os(iOS)
import UIKit
#endif 

warning: UI/NS classes frequently have different implementation. so it's normal to have different constructors, function calls, etc. meaning you often have to have two implementations in code using the above if/elseif/endif structure. yes this can be tedious.

UPDATE

Here is an example for drawing an ellipse in both macOS and iOS. note: the macOS version uses a CGMutablePath (since there's not a simple way to construct a SKShapeNode from an NSBezierPath on macOS like there is on iOS). if you do want to use NSBezierPath, you might also try these techniques for converting NSBezierPath to CGPath

override func didMove(to view: SKView) {
    let rect = CGRect(x: 0, y: 0, width: 200, height: 200)
    
    #if os(macOS)
    let path = CGMutablePath(ellipseIn:rect, transform:nil)
    let shape = SKShapeNode(path:path)
    
    #elseif os(iOS)
    let path = UIBezierPath(ovalIn: rect)
    let shape = SKShapeNode(path: path.cgPath)
    #endif
    
    addChild(shape)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文