代码之家  ›  专栏  ›  技术社区  ›  Sam Shaikh

UIBezierPath的开始和结束角度?

  •  13
  • Sam Shaikh  · 技术社区  · 10 年前

    我在iOS中使用以下代码为半圆 UIBezier路径 CAShape层 .

     clockWiseLayer = [[CAShapeLayer alloc] init];
    
    CGFloat startAngle = -M_PI_2;
    CGFloat endAngle = M_PI + M_PI_2;
    
    CGFloat width = CGRectGetWidth(imageView.frame)/2.0f + 30;
    CGFloat height = CGRectGetHeight(imageView.frame)/2.0f +50;
    CGPoint centerPoint = CGPointMake(width, height);
    
    float radius = CGRectGetWidth(imageView.frame)/2+10;
    
    clockWiseLayer.path = [UIBezierPath bezierPathWithArcCenter:centerPoint
                                                        radius:radius
                                                    startAngle:startAngle
                                                      endAngle:endAngle
                                                     clockwise:YES].CGPath;
    
    clockWiseLayer.fillColor = [UIColor clearColor].CGColor;
    clockWiseLayer.strokeColor = [UIColor blueColor].CGColor;
    clockWiseLayer.borderColor = [UIColor greenColor].CGColor;
    clockWiseLayer.backgroundColor = [UIColor redColor].CGColor;
    
    clockWiseLayer.strokeStart = 0.0f;
    clockWiseLayer.strokeEnd = 0.5f;
    
    clockWiseLayer.lineWidth = 2.0f;
    clockWiseLayer.borderWidth = 5.0f;
    
    clockWiseLayer.shouldRasterize = NO;
    [self.layer addSublayer:clockWiseLayer];
    

    结果如下。

    enter image description here

    我想要这个蓝色的半圆形在世界地球的对面。

    它是半圆形的,但我希望它在另一侧,也是逆时针方向。

    我无法设置开始和结束角度,而 顺时针:否 .

    谢谢

    3 回复  |  直到 10 年前
        1
  •  34
  •   Grzegorz Krukowski    8 年前

    检查坐标文件: http://i.stack.imgur.com/1yJo6.png

    enter image description here

    在数学中,Y轴与标准坐标系相反。

    您应该从1/2*PI(底部锚点)绘制到3/2*PI(顶部锚点),然后将strokeStart设置为0.0f,strokeEnd设置为1.0f(因此它将填充整个路径)。

    使用iOS常量的工作代码:

    CGFloat startAngle = M_PI_2;
    CGFloat endAngle = startAngle + M_PI;
    CGFloat width = CGRectGetWidth(imageView.frame)/2.0f;
    CGFloat height = CGRectGetHeight(imageView.frame)/2.0f;
    CGPoint centerPoint = CGPointMake(width, height);
    float radius = CGRectGetWidth(imageView.frame)/2+10;
    CAShapeLayer* clockWiseLayer = [[CAShapeLayer alloc] init];
    clockWiseLayer.path = [UIBezierPath bezierPathWithArcCenter:centerPoint
                                                         radius:radius
                                                     startAngle:startAngle
                                                       endAngle:endAngle
                                                      clockwise:YES].CGPath;
    
    clockWiseLayer.fillColor = [UIColor clearColor].CGColor;
    clockWiseLayer.strokeColor = [UIColor blueColor].CGColor;
    clockWiseLayer.borderColor = [UIColor greenColor].CGColor;
    clockWiseLayer.backgroundColor = [UIColor redColor].CGColor;
    
    clockWiseLayer.strokeStart = 0.0f;
    clockWiseLayer.strokeEnd = 1.0f;
    
    clockWiseLayer.lineWidth = 2.0f;
    clockWiseLayer.borderWidth = 5.0f;
    [self.layer addSublayer:clockWiseLayer];
    
        2
  •  7
  •   libec    10 年前

    你从圆圈顶部开始 -M_PI_2 结束于 M_PI + M_PI_2 (如果您想要完整的圆圈,请使用 strokeEnd , strokeStart ). 然后将圆路径设置为从路径末端的一半(图像左侧)绘制,而不是从开始到一半(图像右侧)绘制

    CGFloat startAngle = -M_PI_2;
    CGFloat endAngle = M_PI + M_PI_2;
    
    clockWiseLayer.strokeStart = .5f;
    clockWiseLayer.strokeEnd = 1.0f;
    
        3
  •  1
  •   Avt    10 年前

    我玩过 bezierPathWithArcCenter 如果 clockwise = NO 。但是,如果您想创建一个具有逆时针方向的圆bezier路径,则可以创建一个顺时针路径,然后使用 bezierPathByReversingPath

    具有相同路径形状的新路径对象,但其路径已沿相反方向创建。