简体   繁体   English

使用四分之一圆弧(即圆角的“对面”)创建UIBezierPath(或CGPath)

[英]Create a UIBezierPath (or CGPath) with a Quarter-Circle Arc that's the 'Opposite' of a Rounded Corner

I've been using the following code to create views with rounded corners : 我一直在使用以下代码创建带有圆角的视图

- (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;
}

So I've been calling it with something like: 所以我一直用类似这样的方式来称呼它:

[self setMaskTo:someView byRoundingCorners:UIRectCornerAllCorners];

Or: 要么:

[self setMaskTo:someOtherView byRoundingCorners:UIRectCornerBottomRight];

I now want to create some views corresponding to one of the bits on the outside of a single rounded corner ... 我现在要创建一些与单个圆角外部的位之一相对应的视图...

A good approach (think I) is to create the appropriate path so that I can create a CAShapeLayer and then use that as the mask for the view (similar to above). 好的方法(我想想)是创建适当的路径,以便我可以创建CAShapeLayer ,然后将其用作视图的蒙版 (类似于上面的内容)。

I'm not sure what the best approach is, which methods etc., when using the UIBezierPath class (or CGPath ). 我不确定使用UIBezierPath类(或CGPath )时最好的方法是什么,哪种方法等等。

Any suggestions? 有什么建议么?

Using UIView s and a mask layer, to get the shapes that I wanted, I just needed to play around creating paths with an arc ... 使用UIView和遮罩层来获得所需的形状 ,我只需要在用弧线创建路径上玩一下...

I'm aware that the methods for working with a CGPath can be used, but I've worked with what is available with UIBezierPath . 我知道可以使用用于CGPath的方法,但是我已经使用过UIBezierPath可用的方法

I've used the following methods to create the paths that I need: 我使用以下方法创建所需的路径:

- (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;
}

And then something like this to use as a mask with a UIView : 然后将类似的东西用作UIView的蒙版:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM