如何检查UIBezierPath
是否为闭合路径(闭合路径就像闭合轮廓意味着它会创建任何形状,如三角形,正方形,多边形等),如果它被关闭则只填写路径?< / p>
以下表示应填充形状的区域;最后2个形状定义闭合轮廓和简单闭合轮廓,只能填充颜色:
以下是我的相同代码,其中有4个正方形,但只填充3个正方形;也想知道是否有可能找到平方英尺的填充区域,因为我在这里得到4个方格如何检查它在4个方格中覆盖的总面积?
UIBezierPath *mainPath =[UIBezierPath bezierPath];
[mainPath moveToPoint:CGPointMake(50, 100)];
[mainPath addLineToPoint:CGPointMake(0, 100)];
[mainPath addLineToPoint:CGPointMake(0, 150)];
[mainPath addLineToPoint:CGPointMake(50, 150)];
[mainPath addLineToPoint:CGPointMake(50, 200)];
[mainPath addLineToPoint:CGPointMake(100, 200)];
[mainPath addLineToPoint:CGPointMake(100, 150)];
[mainPath addLineToPoint:CGPointMake(50, 150)];
[mainPath addLineToPoint:CGPointMake(50, 100)];
[mainPath addLineToPoint:CGPointMake(100, 100)];
[mainPath addLineToPoint:CGPointMake(150, 100)];
[mainPath addLineToPoint:CGPointMake(150, 150)];
[mainPath addLineToPoint:CGPointMake(100, 150)];
[mainPath addLineToPoint:CGPointMake(100, 100)];
CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];
shapeLayer.lineWidth = 5.0;
shapeLayer.strokeColor = [UIColor blueColor].CGColor;
shapeLayer.path =mainPath.CGPath;
shapeLayer.fillColor = [[UIColor redColor] colorWithAlphaComponent:0.5].CGColor;
[[self.view layer] addSublayer:shapeLayer];
答案 0 :(得分:0)
为什么不使用mainPath.closePath
它会自动关闭您的路径并填写。如果您希望自动或手动获得有关关闭路径的更多信息,可以查看here,它说:
不同之处在于[closePath]方法实际上添加了一个 支持底层CGPath的附加路径元素 UIBezierPath。
如果使用[closePath],则使用其他类型的CGPathElement kCGPathElementCloseSubpath将附加到路径的末尾 紧接在最后一个线段之后。
对于所覆盖的区域,您可以获得UIBezierPath
的边界框,但我认为您可以更简单地使用不同的UIBezierPath
(每个方块一个)然后计算每个方块的边界框。如果你想拥有路径的边界框:
let bounds = CGPathGetBoundingBox(yourPath.CGPath)
答案 1 :(得分:0)
我已回答了关于CGPath
here的类似问题。由于您可以获得CGPath
的{{1}},我认为它也适用于此处。这适用于Swift 3.x.我从this answer借了严重,添加了UIBezierPath
函数。
isClosed()
以下是您使用它的方式:
func isClosed() -> Bool {
var isClosed = false
forEach { element in
if element.type == .closeSubpath { isClosed = true }
}
return isClosed
}
func forEach( body: @convention(block) (CGPathElement) -> Void) {
typealias Body = @convention(block) (CGPathElement) -> Void
let callback: @convention(c) (UnsafeMutableRawPointer, UnsafePointer<CGPathElement>) -> Void = { (info, element) in
let body = unsafeBitCast(info, to: Body.self)
body(element.pointee)
}
print(MemoryLayout.size(ofValue: body))
let unsafeBody = unsafeBitCast(body, to: UnsafeMutableRawPointer.self)
self.apply(info: unsafeBody, function: unsafeBitCast(callback, to: CGPathApplierFunction.self))
}
}