【问题标题】:Create a UIBezierPath (or CGPath) with a Quarter-Circle Arc that's the 'Opposite' of a Rounded Corner创建一个带有与圆角“相反”的四分之一圆弧的 UIBezierPath(或 CGPath)
【发布时间】:2014-05-04 13:12:06
【问题描述】:

我一直在使用以下代码创建圆角视图

- (void)setMaskTo:(UIView*)view byRoundingCorners:(UIRectCorner)corners
{
    UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:view.bounds
                                                  byRoundingCorners:corners
                                                        cornerRadii:CGSizeMake(_cornerRadius, _cornerRadius)];
    CAShapeLayer* shape = [[CAShapeLayer alloc] init];
    [shape setPath:rounded.CGPath];
    view.layer.mask = shape;
}

所以我一直用类似这样的方式来称呼它:

[self setMaskTo:someView byRoundingCorners:UIRectCornerAllCorners];

或者:

[self setMaskTo:someOtherView byRoundingCorners:UIRectCornerBottomRight];

我现在想创建一些视图,对应于单个圆角的外侧...

上的某个位

一个好方法(我想)是创建适当的 path 以便我可以创建一个 CAShapeLayer 然后将其用作视图的 mask (类似上面)。

我不确定使用UIBezierPath 类(或CGPath)时最好的方法是什么,哪些方法等。

有什么建议吗?

【问题讨论】:

    标签: ios cocoa-touch uiview calayer uibezierpath


    【解决方案1】:

    使用UIViews 和遮罩层,为了获得我想要的形状,我只需要尝试用arc创建paths /em>...

    我知道可以使用 CGPath 的方法,但我已经使用了 UIBezierPath 可用的方法。

    我使用了以下方法来创建我需要的路径:

    - (UIBezierPath*)oppositeArcOfPathForBottomRightCorner
    {
        float width = _cornerRadius;
        UIBezierPath* path = [UIBezierPath new];
        [path moveToPoint:CGPointMake(0, width)];
        [path addLineToPoint:CGPointMake(width, width)];
        [path addLineToPoint:CGPointMake(width, 0)];
        [path addArcWithCenter:CGPointMake(0, 0) radius:width startAngle:0 endAngle:(M_PI / 2.0f) clockwise:YES];
        [path closePath];
        return path;
    }
    
    - (UIBezierPath*)oppositeArcOfPathForTopLeftCorner
    {
        float width = _cornerRadius;
        UIBezierPath* path = [UIBezierPath new];
        [path moveToPoint:CGPointMake(0, width)];
        [path addArcWithCenter:CGPointMake(width, width) radius:width startAngle:M_PI endAngle:(M_PI * 1.5f) clockwise:YES];
        [path addLineToPoint:CGPointMake(0, 0)];
        [path closePath];
        return path;
    }
    

    然后像这样用作带有UIView 的掩码:

    - (void)setMaskTo:(UIView*)view fromPath:(UIBezierPath*)path
    {
        CAShapeLayer* shape = [[CAShapeLayer alloc] init];
        [shape setPath:path.CGPath];
        view.layer.mask = shape;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-07
      • 2016-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多