cgpath

How to get the distance to a CGPath for hit-testing?

橙三吉。 提交于 2021-02-18 12:47:14
问题 I have an open CGPath/UIBezierPath for which I want to detect if the user touches it, i.e. whether a point is within a certain distance from the path. The path is open (i.e. a line/curve, not a shape). It can contain both straight & curved elements. How do I get the distance to the path to hit-test it? 回答1: It seems neither CGPath/UIBezierPath has a function to do this. EDIT: As per @nielsbot's suggestion, you could write a custom implementation using CGPathApply(…) . Computing the distance

Get random CGPoint on CGPath

柔情痞子 提交于 2021-02-07 09:12:55
问题 I have a CGPath that represents an ellipse. I'd like to place a sprite node at a random point on that path. How can I get a random CGPoint on that CGPath? 回答1: Speaking about a closed CGPath and thinking to a fast method to obtain a random point between infinite points inside a CGPath , first of all I've translated to Swift this: extension CGPath { func forEach(@noescape body: @convention(block) (CGPathElement) -> Void) { typealias Body = @convention(block) (CGPathElement) -> Void func

How to position CAShapeLayer in a UIView

折月煮酒 提交于 2021-01-29 11:46:40
问题 I saw many answers on Stack overflow, but none helped me. I created a DrawView where it's possible to draw. I created an instance of UIBezierPath for drawing. I want to transfer my drawing on a little view (the red one on the image). That's what I did: // This function is called when the user has finished to draw. override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { let shapeLayer = CAShapeLayer() //The CAShapeLayer has the same path of the drawing (currentPath is the

CGPath copy lineJoin and miterLimit has no apparent affect

会有一股神秘感。 提交于 2020-12-29 11:57:13
问题 I am offsetting a CGPath using copy(strokingWithWidth:lineCap:lineJoin:miterLimit:transform‌​:). The problem is the offset path introduces all kinds of jagged lines that seem to be the result of a miter join. Changing the miterLimit to 0 has no effect, and using a bevel line join also makes no difference. In this image there is the original path (before applying strokingWithWidth ), an offset path using miter join, and an offset path using bevel join. Why doesn't using bevel join have any

CGPath copy lineJoin and miterLimit has no apparent affect

眉间皱痕 提交于 2020-12-29 11:56:49
问题 I am offsetting a CGPath using copy(strokingWithWidth:lineCap:lineJoin:miterLimit:transform‌​:). The problem is the offset path introduces all kinds of jagged lines that seem to be the result of a miter join. Changing the miterLimit to 0 has no effect, and using a bevel line join also makes no difference. In this image there is the original path (before applying strokingWithWidth ), an offset path using miter join, and an offset path using bevel join. Why doesn't using bevel join have any

Drawing dashed line in Sprite Kit using SKShapeNode

萝らか妹 提交于 2020-05-27 05:02:31
问题 I fount this solution but I can't make it into swift code This what I try var pattern[2]:CGFloat; this var dashed: CGPathRef = CGPathCreateCopyByDashingPath(CGPathCreateCopyByDashingPath(path, transform, phase, lengths, count); var myShapeNode: SKShapeNode!; var CGPathCreateCopyByDashingPath:CGPathRef; 回答1: This is how you can draw a dashed line in swift. You can change the parameters as you want. let bezierPath = UIBezierPath() let startPoint = CGPointMake(0, 250) let endPoint = CGPointMake

Resize UIView to fit a CGPath

做~自己de王妃 提交于 2020-01-12 09:55:46
问题 I have a UIView subclass on which the user can add a random CGPath. The CGPath is added by processing UIPanGestures. I would like to resize the UIView to the minimal rect possible that contains the CGPath. In my UIView subclass, I have overridden sizeThatFits to return the minimal size as such: - (CGSize) sizeThatFits:(CGSize)size { CGRect box = CGPathGetBoundingBox(sigPath); return box.size; } This works as expected and the UIView is resized to the value returned, but the CGPath is also

Resize UIView to fit a CGPath

你离开我真会死。 提交于 2020-01-12 09:50:37
问题 I have a UIView subclass on which the user can add a random CGPath. The CGPath is added by processing UIPanGestures. I would like to resize the UIView to the minimal rect possible that contains the CGPath. In my UIView subclass, I have overridden sizeThatFits to return the minimal size as such: - (CGSize) sizeThatFits:(CGSize)size { CGRect box = CGPathGetBoundingBox(sigPath); return box.size; } This works as expected and the UIView is resized to the value returned, but the CGPath is also

Trim/subtract CGPath from CGPath?

你离开我真会死。 提交于 2020-01-12 03:26:08
问题 I have some CGPath (a poly with two circle) carried out on a CAShapeLayer . Can I trim/subtract paths like this? 回答1: Instead of do the math, the result can be achieved at drawing time using kCGBlendModeClear . // Create the poly. CGContextBeginPath(context); CGContextMoveToPoint (context, a_.x, a_.y); CGContextAddLineToPoint (context, b_.x, b_.y); CGContextAddLineToPoint (context, c_.x, c_.y); CGContextAddLineToPoint (context, d_.x, d_.y); CGContextClosePath(context); // Draw with the

Applying alpha outside intersection of 2 CGPath objects

限于喜欢 提交于 2020-01-05 07:26:33
问题 I have a CGPathRef for an oddly shaped polygon. I need to apply an alpha to the area outside of this path. This is pretty straightforward. CGContextAddPath(context, crazyPolygon); CGContextSetFillColor(context, someAlphaColor); CGContextEOFillPath(context); I need to do something similar with a circle, also straightforward. CGMutablePathRef circlePath = CGPathCreateMutable(); CGPathAddRect(circlePath, NULL, rect); CGPathAddEllipseInRect(circlePath, NULL, circleBox); CGContextAddPath(context,