Swift中的UIBezierPath Stroke(和Fill)

时间:2014-07-19 18:15:22

标签: swift uibezierpath

我正在努力找到一种方法来快速抚摸(并填充)Bezier路径。这是我认为可行的但却没有。

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]

任何Sugestions?

3 个答案:

答案 0 :(得分:12)

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

  UIColor.blackColor().setStroke()

这意味着:

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

但你正在做相反的事情。

 UIColor.setStroke(UIColor.blackColor())

这意味着您正在调用setStroke的类方法UIColor,并传递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()

这不会给上下文错误

答案 1 :(得分:2)

  

在Objective-C中,我会使用类似的东西但是,这显然不适用于Swift。

[[UIColor blackColor] setStroke]
[myBezier stroke]

根本不明显。事实上,这是错误的。如果该代码在Objective-C中有效,那么它将在Swift中工作。直接翻译:

UIColor.blackColor().setStroke()
myBezier.stroke()

但是,除非你处于图形上下文中(例如drawRect:或UIGraphicsBeginImageContext),否则你无法在Objective-C或Swift中做任何这些事情。

答案 2 :(得分:0)

Swift 3.2:

this.reveal.updateVisuals(i, 'hide');