Ios 为什么“[bezierPath closePath]”方法不';你不关闭我的道路吗?

Ios 为什么“[bezierPath closePath]”方法不';你不关闭我的道路吗?,ios,uiview,uibezierpath,cgpoint,Ios,Uiview,Uibezierpath,Cgpoint,我有一个包含CGPoints的NSArray,我画了从这个类返回的路径。问题是[bezierPath closePath]在这个类中没有关闭我的路径。为什么呢?我需要使用此类提供的曲线将端点连接到阵列的第一个点,并使用此类使路径完全闭合/连接且连续。除了[bezierPath closePath]之外,我还应该做些什么,因为当我在drawrect方法中使用它时,它不会做任何事情。感谢您的帮助 UIBezierPath(SmoothPath)类的代码: UIBezierPath+SmoothPat

我有一个包含CGPoints的NSArray,我画了从这个类返回的路径。问题是[bezierPath closePath]在这个类中没有关闭我的路径。为什么呢?我需要使用此类提供的曲线将端点连接到阵列的第一个点,并使用此类使路径完全闭合/连接且连续。除了
[bezierPath closePath]
之外,我还应该做些什么,因为当我在drawrect方法中使用它时,它不会做任何事情。感谢您的帮助

UIBezierPath(SmoothPath)类的代码:

UIBezierPath+SmoothPath.h:
#进口
@接口UIBezierPath(平滑路径)
+(UIBezierPath*)smoothPathFromArray:(NSArray*)arr;
@结束

UIBezierPath+SmoothPath.m:
#导入“UIBezierPath+SmoothPath.h”
@实现UIBezierPath(平滑路径)
+(UIBezierPath*)smoothPathFromArray:(NSArray*)arr{
如果([arr计数]>0){
UIBezierPath*bezierPath=[UIBezierPath bezierPath];
NSMutableArray*pts=[arr mutableCopy];
int i=0;
对于(;i
您一直在移动路径(
moveToPoint
)。这使得一条不连续的路径如此闭合,它只返回到最后一节的开头。将曲线或直线添加到路径时,路径的当前点将移动到该曲线或直线的端点。设置路径的起点时,仅在起点使用moveToPoint

非常感谢,所以我做了这个
[bezierPath moveToPoint:[pts[0]CGPointValue]
并将此行从for循环中带出,然后使用
[path closePath]
关闭路径,它确实关闭了路径,但端点和起点(数组的最后一个索引和第一个索引)之间的部分仍然不平滑,如何使它看起来连续平滑,即使在端点和起点的接合处也是如此?您需要像处理其他点一样计算曲线。关闭路径将始终只做一条直线。将曲线计算回起点,添加该曲线,然后关闭路径。
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