我有一个包含CGPoints的NSArray,我绘制了从这个类返回的路径。问题是[bezierPath closePath]在这个类中没有关闭我的路径。这是为什么?我需要使用此类给出的曲线将终点连接到数组的第一个点,并使用此类使路径完全闭合/连接。除了[bezierPath closePath]
之外我还应该做些什么,因为当我在我的直接方法中使用它时它不会做任何事情。任何帮助表示赞赏。
UIBezierPath(SmoothPath)类的代码:
UIBezierPath+SmoothPath.h:
#import <UIKit/UIKit.h>
@interface UIBezierPath (SmoothPath)
+ (UIBezierPath*)smoothPathFromArray:(NSArray*)arr;
@end
并且
UIBezierPath+SmoothPath.m:
#import "UIBezierPath+SmoothPath.h"
@implementation UIBezierPath (SmoothPath)
+ (UIBezierPath*)smoothPathFromArray:(NSArray*)arr{
if ([arr count] > 0){
UIBezierPath *bezierPath = [UIBezierPath bezierPath];
NSMutableArray *pts = [arr mutableCopy];
int i = 0;
for (; i < pts.count - 4 ; i+= 3){
CGPoint temp = CGPointMake(([pts[i+2] CGPointValue].x + [pts[i+4] CGPointValue].x)/2.0,
([pts[i+2] CGPointValue].y + [pts[i+4] CGPointValue].y)/2.0);
pts[i+3] = [NSValue valueWithCGPoint:temp];
[bezierPath moveToPoint:[pts[i] CGPointValue]];
[bezierPath addCurveToPoint:temp controlPoint1:[pts[i+1] CGPointValue] controlPoint2:[pts[i+2] CGPointValue]];
}
switch (pts.count - i) {
case 4:
[bezierPath moveToPoint:[pts[i] CGPointValue]];
[bezierPath addCurveToPoint:[pts[i+3] CGPointValue] controlPoint1:[pts[i+1] CGPointValue] controlPoint2:[pts[i+2] CGPointValue]];
break;
case 3:
[bezierPath moveToPoint:[pts[i] CGPointValue]];
[bezierPath addCurveToPoint:[pts[i+2] CGPointValue] controlPoint1:[pts[i] CGPointValue] controlPoint2:[pts[i+1] CGPointValue]];
break;
case 2:
[bezierPath moveToPoint:[pts[i] CGPointValue]];
[bezierPath addLineToPoint:[pts[i+1] CGPointValue]];
break;
case 1:
[bezierPath addLineToPoint:[pts[i] CGPointValue]];
break;
default:
}
[bezierpath closePath];
return bezierPath;
}
return nil;
}
@end
答案 0 :(得分:3)
您继续移动路径(moveToPoint
)。这使得一条不连续的路径因此关闭它只是回到最后一节的开头。向路径添加曲线或线时,路径的当前点将移动到该曲线或线的末尾。设置路径的起点时,仅在开头使用moveToPoint。