简体   繁体   English

在自定义UIBezierPath周围绘制圆圈

[英]Draw circle around custom UIBezierPath

I have a UIBezierPath drawn by the user on the touchscreen. 我在触摸屏上有一个由用户绘制的UIBezierPath。 Now I want to draw a circle around this path/drawing, and the path has to be in the middle of this circle. 现在,我想在此路径/绘图周围绘制一个圆,并且该路径必须在该圆的中间。 I tried to use: 我尝试使用:

UIBezierPath.bounds

But obviously this does not work :( So my next idea was to go through every point in the UIBezierPath and get the "lowest" point and the most "left" point. After that draw the circle based on those points. 但是显然这是行不通的:(因此,我的下一个想法是遍历UIBezierPath中的每个点,并获得“最低”点和最“左”点。在那之后,基于这些点绘制圆。

My question is: Is there are more elegant and efficient way of drawing a circle around a custom UIBezierPath? 我的问题是:是否有更优雅,更有效的方法围绕自定义UIBezierPath画圆?

Did it this way: 这样做是这样的:

NSMutableArray *bezierPoints = [NSMutableArray array];
CGPathApply(drawPath.CGPath, (__bridge void *)(bezierPoints), MyCGPathApplierFunc);

float minX = 100000.f;
float minY = 100000.f;

for(NSValue* value in bezierPoints)
{
    CGPoint point = value.CGPointValue;

    if (point.x<minX)
        minX = point.x;
    if (point.y<minY)
        minY = point.y;
}
float midX = minX + (drawPath.bounds.size.width/2);
float midY = minY + (drawPath.bounds.size.height/2);

float radius = 0.f;
if (drawPath.bounds.size.width>drawPath.bounds.size.height)
{
    radius = drawPath.bounds.size.width * 0.65;

}
else
{
    radius = drawPath.bounds.size.height * 0.65;
}

CGMutablePathRef myPath = CGPathCreateMutable();
CGPathAddArc(myPath, NULL, midX,midY,radius , 0, M_PI*2, YES);



SKShapeNode* node = [[SKShapeNode alloc]init];
node.path = myPath;
node.fillColor = nil;
node.strokeColor = SKColor.redColor;
[self addChild:node];

void MyCGPathApplierFunc (void *info, const CGPathElement *element) {
NSMutableArray *bezierPoints = (__bridge NSMutableArray *)info;

CGPoint *points = element->points;
CGPathElementType type = element->type;

switch(type) {
    case kCGPathElementMoveToPoint: // contains 1 point
        [bezierPoints addObject:[NSValue valueWithCGPoint:points[0]]];
        break;

    case kCGPathElementAddLineToPoint: // contains 1 point
        [bezierPoints addObject:[NSValue valueWithCGPoint:points[0]]];
        break;

    case kCGPathElementAddQuadCurveToPoint: // contains 2 points
        [bezierPoints addObject:[NSValue valueWithCGPoint:points[0]]];
        [bezierPoints addObject:[NSValue valueWithCGPoint:points[1]]];
        break;

    case kCGPathElementAddCurveToPoint: // contains 3 points
        [bezierPoints addObject:[NSValue valueWithCGPoint:points[0]]];
        [bezierPoints addObject:[NSValue valueWithCGPoint:points[1]]];
        [bezierPoints addObject:[NSValue valueWithCGPoint:points[2]]];
        break;

    case kCGPathElementCloseSubpath: // contains no point
        break;
}
 }  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM