Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIBezierPath Stroke (and Fill) in Swift

I'm struggling to find a way to stroke (and fill) a Bezier path in swift. Here's what I thought would work but doesn't.

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 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 is obviously not going to work in Swift.

[[UIColor blackColor] setStroke]
[myBezier stroke]

Any Sugestions?

like image 443
Old Name Avatar asked Jul 19 '14 18:07

Old Name


2 Answers

setStroke is an instance method of UIColor and takes no parameters. Use

  UIColor.blackColor().setStroke()

This means:

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

But you are doing opposite.

 UIColor.setStroke(UIColor.blackColor())

This means you are calling the class method setStroke of UIColor, and passing blackColor. setStroke is an instance method, not a class method, so you need a UIColor object, which 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 error for context

like image 80
codester Avatar answered Sep 18 '22 12:09

codester


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

[[UIColor blackColor] setStroke]
[myBezier stroke]

It's not obvious at all. In fact, it's false. If that code would have worked in Objective-C, then it will work in Swift. Translate directly:

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

But of course you can't do any of those things, in Objective-C or in Swift, unless you are in a graphics context (e.g. drawRect: or UIGraphicsBeginImageContext).

like image 28
matt Avatar answered Sep 20 '22 12:09

matt