2014-04-22 88 views
0

我有一個用戶在觸摸屏上繪製的UIBezierPath。現在,我想圍繞此路徑繪製一個圓,路徑必須位於此圓的中間。我試圖用:圍繞自定義UIBezierPath繪製圓

UIBezierPath.bounds 

但顯然,這並不工作:(因此,我的下一個想法是要經過在UIBezierPath每一個點,並獲得了「最低」的點,最「左」點之後。借鑑基於這些點圓

我的問題是:?是否有周圍繪製一個自定義UIBezierPath圓的更優雅和有效的方式

+2

展你的代碼使用'bounds'並描述它做錯了什麼。 – Wain

+0

哦,這很簡單,當邊界寬度= 10和高度= 1時,圓是平的,但不是圓的。這發生在不同的形狀上。 – user1990524

+0

所以問題就在於你需要總是從界限中取出一個圓圈? – Wain

回答

0

就是這麼做的:

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