【问题标题】:Draw circle around custom UIBezierPath围绕自定义 UIBezierPath 绘制圆圈
【发布时间】:2014-06-06 14:24:11
【问题描述】:

我有一个用户在触摸屏上绘制的 UIBezierPath。现在我想在这个路径/绘图周围画一个圆圈,路径必须在这个圆圈的中间。我尝试使用:

UIBezierPath.bounds

但显然这不起作用:(所以我的下一个想法是遍历 UIBezierPath 中的每个点并获得“最低”点和最“左”点。然后根据这些点绘制圆。

我的问题是:有没有更优雅、更高效的方式围绕自定义 UIBezierPath 画一个圆?

【问题讨论】:

  • 使用bounds 显示您的代码并描述它的错误之处。
  • 哦,很简单,当边界宽度 = 10 高度 = 1 时,圆是平的,但不是圆的。不同的形状会发生这种情况。
  • 所以问题只是你需要总是从边界得到一个圆?

标签: ios objective-c uibezierpath


【解决方案1】:

这样做的:

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;
}
 }  

【讨论】:

    猜你喜欢
    • 2013-06-19
    • 1970-01-01
    • 1970-01-01
    • 2015-07-21
    • 2022-01-20
    • 1970-01-01
    • 2016-07-19
    • 2019-01-30
    • 2012-10-25
    相关资源
    最近更新 更多