我正在将NSView移植到
Swift,我需要使用Quartz CGContextAddpath.
import Cocoa class MYView: NSView { init(frame: NSRect) { super.init(frame: frame) } override func drawRect(dirtyRect: NSRect) { super.drawRect(dirtyRect) NSColor .redColor() .set() NSRectFill(self.bounds) var p :CGMutablePathRef = CGPathCreateMutable() var ctx = NSGraphicsContext.currentContext().graphicsPort() CGContextAddpath(ctx,p) // compiler rejects this line } }
你怎么理解这个错误信息?
Cannot convert the expression's type 'Void' to type 'CGContext!'
CGContextAddpath的Swift签名是:
func CGContextAddpath(context: CGContext!,path: CGPath!)
我的错误是什么?
我用这个时:
let context = UnsafePointer<CGContext>(ctx).memory
Jun 3 15:57:13 xxx.x SwiftTest [57092]<错误>:CGContextAddpath:无效的上下文0x7fff73bd0060.这是一个严重的错误.该应用程序或其使用的库正在使用无效的上下文,从而导致系统稳定性和可靠性的整体降低.此通知是礼貌的:请解决此问题.它将成为即将到来的更新中的致命错误.
这是我目前使用的代码:
import Cocoa class MYView: NSView { init(frame: NSRect) { super.init(frame: frame) } override func drawRect(dirtyRect: NSRect) { super.drawRect(dirtyRect) var p :CGMutablePathRef = CGPathCreateMutablecopy( CGPathCreateWithRoundedRect(self.bounds,10,nil)) var ctx = NSGraphicsContext.currentContext().graphicsPort() let context = UnsafePointer<CGContext>(ctx).memory CGContextAddpath(context,p) // compiler no longer rejects this line var blueColor = NSColor.blueColor() CGContextSetstrokeColorWithColor(context,NSColor.blueColor().CGColor) CGContextSetlinewidth(context,2) CGContextstrokePath(context) } }
截至Swift 1.0
如果您的部署目标是10.10,则可以使用Yosemite引入的便捷方法.
let context = NSGraphicsContext.currentContext().CGContext
如果你必须支持10.9,你必须按照下面的方法手动转换上下文.
let contextPtr = NSGraphicsContext.currentContext().graphicsPort let context = unsafeBitCast(contextPtr,CGContext.self)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。