我正在努力寻找一种快速绘制(和填充)贝塞尔曲线路径的方法。这是我认为行得通但行不通的方法。
var myBezier = UIBezierPath()
myBezier.moveToPoint(CGPoint(x: 0, y: 0))
myBezier.addLineToPoint(CGPoint(x: 100, y: 0))
myBezier.addLineToPoint(CGPoint(x: 50, y: 100))
myBezier.closePath()
UIColor.setStroke(UIColor.blackColor())
myBezier.stroke()
行 UIColor.setStroke(UIColor.blackColor())
在控制台中给我这个错误:
Playground execution failed: error: <REPL>:51:1: error: expression
resolves to an unused function UIColor.blackColor())
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
行 myBezier.stroke()
给我这组错误:
Jul 19 12:07:46 Computer-MacBook-Pro.local [20579] <Error>: CGContextSaveGState: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
Jul 19 12:07:46 Computer-MacBook-Pro.local [20579] <Error>: CGContextSetLineWidth: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
Jul 19 12:07:46 Computer-MacBook-Pro.local [20579] <Error>: CGContextSetLineJoin: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
Jul 19 12:07:46 Computer-MacBook-Pro.local [20579] <Error>: CGContextSetLineCap: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
Jul 19 12:07:46 Computer-MacBook-Pro.local [20579] <Error>: CGContextSetMiterLimit: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
Jul 19 12:07:46 Computer-MacBook-Pro.local [20579] <Error>: CGContextSetFlatness: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
Jul 19 12:07:46 Computer-MacBook-Pro.local [20579] <Error>: CGContextAddPath: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
Jul 19 12:07:46 Computer-MacBook-Pro.local [20579] <Error>: CGContextDrawPath: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
Jul 19 12:07:46 Computer-MacBook-Pro.local [20579] <Error>: CGContextRestoreGState: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
在 Objective-C 中,我会使用这样的东西,但是,这在 Swift 中显然行不通。
[[UIColor blackColor] setStroke]
[myBezier stroke]
有什么建议吗?
最佳答案
setStroke
是 UIColor
的实例方法,不带参数。使用
UIColor.blackColor().setStroke()
这意味着:
var color = UIColor.blackColor() //returns color
color.setStroke() // setStroke on color
但你做的恰恰相反。
UIColor.setStroke(UIColor.blackColor())
这意味着您正在调用UIColor
的类方法setStroke
,并传递blackColor
。 setStroke
是一个实例方法,不是类方法,所以你需要一个UIColor
对象,它由UIColor.blackColor()
返回。
编辑
//MyPlayground.playground
import UIKit
class MyCustomView :UIView{
//Write your code in drawRect
override func drawRect(rect: CGRect) {
var myBezier = UIBezierPath()
myBezier.moveToPoint(CGPoint(x: 0, y: 0))
myBezier.addLineToPoint(CGPoint(x: 100, y: 0))
myBezier.addLineToPoint(CGPoint(x: 50, y: 100))
myBezier.closePath()
UIColor.blackColor().setStroke()
myBezier.stroke()
}
}
var view = MyCustomView(frame: CGRectMake(0, 0, 100, 100))
view.backgroundColor = UIColor.whiteColor()
这不会给出上下文错误
关于swift - Swift 中的 UIBezierPath 描边(和填充),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24843516/