UIBezierPath Stroke (and Fill) in Swift

I am struggling to find a way to stroke (and fill) the Bezier path in a swift direction. Here, what I thought will work, but does not.

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

The UIColor.setStroke(UIColor.blackColor()) line UIColor.setStroke(UIColor.blackColor()) gives me this error in the console:

 Playground execution failed: error: <REPL>:51:1: error: expression resolves to an unused function UIColor.blackColor()) ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

The line myBezier.stroke() gives me this set of errors:

 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. 

In Objective-C, I would use something like this, but this obviously will not work in Swift.

 [[UIColor blackColor] setStroke] [myBezier stroke] 

Any sugestions?

+6
source share
3 answers

setStroke is an instance method of UIColor and does not accept parameters. Use

  UIColor.blackColor().setStroke() 

It means:

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

But you are doing the opposite.

  UIColor.setStroke(UIColor.blackColor()) 

This means that you call the setStroke class setStroke from UIColor and pass blackColor . setStroke is an instance method, not a class method, so you need a UIColor object that is returned by UIColor.blackColor() .

EDIT

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

This will not give an error for the context.

+12
source

In Objective-C, I would use something like this, but this obviously will not work in Swift.

 [[UIColor blackColor] setStroke] [myBezier stroke] 

This is not at all obvious. This is actually not true. If this code worked in Objective-C, then it would work in Swift. Translate directly:

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

But, of course, you cannot do any of these things in Objective-C or in Swift unless you are in a graphical context (e.g. drawRect: or UIGraphicsBeginImageContext).

+2
source

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
source

Source: https://habr.com/ru/post/972510/


All Articles