Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
370 views
in Technique[技术] by (71.8m points)

iphone - How to draw polygons with CGPath?

I have been reading thru the documentation however it is not immediatly clear to me how to draw a polygon using CGPath. All I need to do is to draw CGPath around something like this:

__
   
   
  \__

Could anyone please provide an snippet on how to do this?

Additionally I assume CGPathContainsPoint will help me determine if a point is inside such path?, or does the path have to be a solid drawing

Also how can i move the cgpath around? Is this as easy as changing something like the origin just like in cgrect?

Thank you.

-Oscar

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is an example of how to create a triangle using CGPath, you only have to put the points.

var path = CGPathCreateMutable()
CGPathMoveToPoint(path, nil, 0, 0) //start from here
CGPathAddLineToPoint(path, nil, 20, 44) 
CGPathAddLineToPoint(path, nil, 40, 0) 
CGPathAddLineToPoint(path, nil, 0, 0)

//and to use in SpriteKit, for example

var tri = SKShapeNode(path: path) 
var color = NSColor.blueColor()
tri.strokeColor = color
tri.fillColor = color

This is the result

Triangle with CGPath


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...