【发布时间】:2016-09-25 03:31:59
【问题描述】:
我有一个包含 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
【问题讨论】:
标签: ios uiview uibezierpath cgpoint