web-dev-qa-db-ja.com

UIBezierPath Stroke(and Fill)in Swift

Swiftでベジェパスをストローク(および塗りつぶす)する方法を見つけるのに苦労しています。これが私がうまくいくと思ったがうまくいかないことです。

_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]
_

何か推測はありますか?

6
Old Name

setStrokeUIColorのインスタンスメソッドであり、パラメータを取りません。使用する

_  UIColor.blackColor().setStroke()
_

これの意味は:

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

しかし、あなたは反対のことをしています。

_ UIColor.setStroke(UIColor.blackColor())
_

これは、setStrokeのクラスメソッドUIColorを呼び出し、blackColorを渡すことを意味します。 setStrokeはインスタンスメソッドであり、クラスメソッドではないため、UIColor.blackColor()によって返されるUIColorオブジェクトが必要です。

[〜#〜]編集[〜#〜]

_//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()
_

これはコンテキストにエラーを与えません

12
codester

Objective-Cでは、このようなものを使用しますが、これは明らかにSwiftでは機能しません。

[[UIColor blackColor] setStroke]
[myBezier stroke]

それはまったく明らかではありません。実際、それは誤りです。そのコードがObjective-Cで機能する場合は、Swiftでも機能します。直接翻訳:

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

ただし、もちろん、グラフィックスコンテキスト(例:drawRect:またはUIGraphicsBeginImageContext)でない限り、Objective-CまたはSwiftでこれらのことを行うことはできません。

2
matt

Swift 3.2:

//MyPlayground.playground
import UIKit

class MyCustomView :UIView{

    //Write your code in drawRect
    override func draw(_ rect: CGRect) {
        let myBezier = UIBezierPath()
        myBezier.move(to: CGPoint(x: 0, y: 0))
        myBezier.addLine(to: CGPoint(x: 100, y: 0))
        myBezier.addLine(to: CGPoint(x: 50, y: 100))
        myBezier.close()
        UIColor.black.setStroke()
        myBezier.stroke()
    }

}

var view = MyCustomView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
view.backgroundColor = UIColor.white
0
Mohsenasm