我有一个包含CGPoints的NSArray,并绘制了从此类返回的路径。问题是[bezierPath closePath]不会关闭此类中的路径。这是为什么?我需要使用此类给我的曲线将端点连接到数组的第一个点,并使用该类使路径完全闭合/连接并连续。除了[bezierPath closePath]
之外,我还应该做什么?因为在我的drawrect方法中使用它时,它什么也没做。任何帮助表示赞赏。
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
最佳答案
您一直在移动路径(moveToPoint
)。这将形成一条不连续的路径,因此将其关闭仅返回到最后一节的开头。将曲线或直线添加到路径时,路径的当前点将移动到该曲线或直线的末端。设置路径的起点时,仅在起点使用moveToPoint。
关于ios - 为什么[[bezierPath closePath]`方法无法关闭我的路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39683108/