2

I have tried to call CGPathMoveToPoint() in Swift using the CGMutablePathRef and a CGPoint as input and NULL/nil instead of the CGAffineTransformation like this

CGPathMoveToPoint(path, nil, point.x as CGFloat, point.y as CGFloat)

However, I always get an error stating the expression cannot be converted to type NilLiteralConvertible. Do you have any idea why this code does not work? What should be used in Swift insted of the nil argument? I tried using CGAffineTransformidentity instead of nil but it doesn't work either. Thanks!

user1612877
  • 391
  • 1
  • 5
  • 19
  • Can you show more code, specifically where you create `path` and `point`? Your one line works for me, if I have `var path = CGPathCreateMutable()` and `var point = CGPoint(x:10, y:20)` before it. – Kurt Revis Dec 15 '14 at 01:56

5 Answers5

3

This code fills a red rectangle in the view:

let context:CGContextRef = NSGraphicsContext.currentContext()!.CGContext

var pathPoint:CGPoint = CGPoint()
pathPoint.x = CGFloat(225.0)
pathPoint.y = CGFloat(100.0)

var myMutablePath:CGMutablePath = CGPathCreateMutable()
CGPathMoveToPoint(myMutablePath, nil, pathPoint.x, pathPoint.y)
CGPathAddLineToPoint(myMutablePath, nil, pathPoint.x + CGFloat(50.0), pathPoint.y)
CGPathAddLineToPoint(myMutablePath, nil, pathPoint.x + CGFloat(50.0), pathPoint.y  + CGFloat(50.0))
CGPathAddLineToPoint(myMutablePath, nil, pathPoint.x, pathPoint.y  + CGFloat(50.0))
CGPathAddLineToPoint(myMutablePath, nil, pathPoint.x, pathPoint.y)

CGContextAddPath(context, myMutablePath);

CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0)
CGContextFillPath(context)
jwlaughton
  • 905
  • 1
  • 6
  • 11
2

If this comes up with Swift3, just check the new syntax. The Xcode error messages are misleading, this is not a problem just with the nil. The example of the new syntax for your code:

path.move(to: point)
t1ser
  • 1,574
  • 19
  • 18
1

I took jwlaughton's answer, put it in the context of a UIView subclass, made the layout more flexible, and updated the Core Graphics calls to Swift 4 as indicated by Xcode 9.4 autocomplete.

class RedSquareView: UIView {
    override func draw(_ rect: CGRect) {
        guard let context = UIGraphicsGetCurrentContext() else { return }
        let length = rect.width < rect.height ? (rect.width * 0.25) : (rect.height * 0.25)
        let start:CGPoint = CGPoint(x: rect.midX - (length/2), y:rect.midY - (length/2))

        let path = CGMutablePath()
        path.move(to: start)
        path.addLine(to: CGPoint(x:start.x + length, y:start.y))
        path.addLine(to: CGPoint(x:start.x + length, y:start.y + length))
        path.addLine(to: CGPoint(x:start.x, y:start.y + length))
        path.addLine(to: CGPoint(x:start.x, y:start.y))

        context.addPath(path)
        context.setFillColor(red:1.0, green:0.0, blue:0.0, alpha:1.0)
        context.fillPath()
    }
}
Jeff
  • 3,829
  • 1
  • 31
  • 49
0

I suspect your path - you have not shown what it is, so how do we know it is really a CGPath? However, there's also this:

point.x as CGFloat

That is not Swift. You want to say CGFloat(point.x) (and so too for point.y) if it is not a CGFloat already.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • changing to CGFloat(point.x) did the trick for me. I am honored and humbled to have been helped by my favorite author. Thanks! – user1612877 Dec 18 '14 at 21:28
  • I am honored and happy to have helped! Think of it this way: `as` is just a way to inform the compiler of what something already is. `CGFloat()` actually _makes_ a CGFloat value, which is what you want to do. – matt Dec 19 '14 at 01:10
0

The code below compiles OK for me. Note that it uses a CGMutablePath, not a CGMutablePathRef.

var myMutablePath:CGMutablePath = CGPathCreateMutable()
CGPathMoveToPoint(myMutablePath, nil, CGFloat(1.0), CGFloat(1.0))
jwlaughton
  • 905
  • 1
  • 6
  • 11