swift - Swift 中的 UIBezierPath 描边(和填充)

标签 swift uibezierpath

我正在努力寻找一种快速绘制(和填充)贝塞尔曲线路径的方法。这是我认为行得通但行不通的方法。

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]

有什么建议吗?

最佳答案

setStrokeUIColor 的实例方法,不带参数。使用

  UIColor.blackColor().setStroke()

这意味着:

    var color = UIColor.blackColor()   //returns color
    color.setStroke()                  // setStroke on color

但你做的恰恰相反。

 UIColor.setStroke(UIColor.blackColor())

这意味着您正在调用UIColor 的类方法setStroke,并传递blackColorsetStroke是一个实例方法,不是类方法,所以你需要一个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/

相关文章:

ios - 如何使用 TableView 项目访问 Swift 2 中的 View Controller ?

ios - 难以绘制阴影

uibezierpath - SceneKit 绘制曲线

ios - Here Map iOS Swift Crashed After call NMAApplicationContext.set(appId :. . appCode :. ..)

ios - 如何在 iOS 中自动执行加法?

ios - 快速绘制自定义形状

ios - 使用 CoreAnimation 时出现 EXC_BAD_ACCESS

objective-c - 一种跟踪 CAShapeLayer 的 strokeEnd 点的方法?

swift - 在 Swift 中有选择地从 NSMutableArray 中移除和删除对象

ios - 我的角半径仅适用于一个角