ios - 为什么[[bezierPath closePath]`方法无法关闭我的路径?

标签 ios uiview uibezierpath cgpoint

我有一个包含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/

相关文章:

ios - 如何在 UIBezierPath 弧中获取半径的中心点?

ios - 摆脱应用程序图标上的 UILocalNotification

ios - 在后台模式下循环

iphone - 简单的 uiview 更改不起作用

ios - 自动布局 - 使父 View 高度等于 subview 高度 + subview 为 textView 的常量

ios - 如何平滑贝塞尔路径中的线条连接?

ios - Sine CAShapeLayer 作为 CALayer 的 mask

javascript - 将 NSTaggedPointerString 转换为字符串

ios - 在 iOS 14.8 上调用过期处理程序之前,BGProcessingTaskRequest 仅运行 295 秒

c# - UIControllerView 无法从委托(delegate)函数更新