12

我正在尝试为我的观点制作圆角并解决一个奇怪的问题。

我使用以下代码为我的视图制作圆角

bezierPath = [UIBezierPath bezierPathWithRoundedRect:btnView.bounds
                                       byRoundingCorners:UIRectCornerAllCorners
                                             cornerRadii:radius];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = btnView.bounds;
    maskLayer.path = bezierPath.CGPath;
    btnView.layer.mask = maskLayer;

问题是,它不适用于底角。如果我使用UIRectCornerBottomLeft没有任何反应,如果我UIRectCornerAllCorners只使用顶角是圆形的。

编辑:我不使用 layer.cornerRadius 因为我只想圆底角。

事实证明我没有问题,如果我UIViewAutoresizingFlexibleHeight从我的视图中删除自动调整大小的掩码。但我想使用自动调整大小的面具。如果可以,有可能吗?

我已经尝试在设置图层蒙版之前和之后设置 autoresizingMask。没运气!

4

9 回答 9

12

我刚刚遇到了一个需要几个小时才能解决的类似问题 - 答案非常简单:将我的代码从 viewDidLoad 移动到 viewDidLayoutSubviews:这是自动布局完成后调用的第一个方法....所以我怀疑我确实有 4 个圆角但不知何故,他们离开了屏幕的边缘。

于 2014-06-25T19:52:32.517 回答
3

在我得到另一个答案之前,如果可以将 autoresizingMask 与 一起使用UIBezierPath我会说“不”并标记为正确答案。

我不会删除我的问题,因为任何人都可以在不知道原因的情况下排除同样的问题。

于 2013-04-23T10:03:20.270 回答
1

解决方案:

-(void)roundCorners:(UIRectCorner)corner radius:(float)radius
{   
    _cornerRadius = radius;
    _corner = corner;
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
    // Drawing code
    [super drawRect:rect];

    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:_corner cornerRadii:CGSizeMake(_cornerRadius, _cornerRadius)];

    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;

    self.layer.mask = maskLayer;
}
于 2015-01-28T23:06:13.867 回答
1

然后用波纹管代码代替圆角...

btnView.layer.cornerRadius = 8.0;

QuartzCore/QuartzCore.h只需在您.m喜欢的下面导入...

#import <QuartzCore/QuartzCore.h>

更新:

为此,您可以使用不同RoundingCorners的属性,UIBezierPath如下所示....

例如:UIRectCornerBottomLeftUIRectCornerBottomRightUIRectCornerTopLeftUIRectCornerTopRightUIRectCornerAllCorners

UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect: btnView.bounds
                                               byRoundingCorners:UIRectCornerBottomLeft
                                                     cornerRadii:CGSizeMake(10.0, 10.0)];

CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = btnView.bounds;
maskLayer.path = maskPath.CGPath;
btnView.layer.mask = maskLayer;
[btnView setNeedsDisplay];
于 2013-04-23T09:09:21.090 回答
1

我认为你应该使用

bezierPath.lineCapStyle = CGLineCap.Round
于 2016-07-12T02:21:39.717 回答
0

您可以用户view.layer.cornerRadius设置圆角。

编辑:使用以下

 layer.mask = MTDContextCreateRoundedMask(layer.bounds, topleftRadius, topRightRadius, bottomLeftRadius,bottomRightRadius);

这是支持的方法。

static inline UIImage* MTDContextCreateRoundedMask(CGRect rect, CGFloat radius_tl, CGFloat radius_tr, CGFloat radius_bl, CGFloat radius_br)
{
    CGContextRef context;
    CGColorSpaceRef colorSpace;

    colorSpace = CGColorSpaceCreateDeviceRGB();

    context = CGBitmapContextCreate( NULL, rect.size.width, rect.size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast );

    CGColorSpaceRelease(colorSpace);

    if ( context == NULL ) {
        return NULL;
    }


    CGFloat minx = CGRectGetMinX( rect ), midx = CGRectGetMidX( rect ), maxx = CGRectGetMaxX( rect );
    CGFloat miny = CGRectGetMinY( rect ), midy = CGRectGetMidY( rect ), maxy = CGRectGetMaxY( rect );

    CGContextBeginPath( context );
    CGContextSetGrayFillColor( context, 1.0, 0.0 );
    CGContextAddRect( context, rect );
    CGContextClosePath( context );
    CGContextDrawPath( context, kCGPathFill );

    CGContextSetGrayFillColor( context, 1.0, 1.0 );
    CGContextBeginPath( context );
    CGContextMoveToPoint( context, minx, midy );
    CGContextAddArcToPoint( context, minx, miny, midx, miny, radius_bl );
    CGContextAddArcToPoint( context, maxx, miny, maxx, midy, radius_br );
    CGContextAddArcToPoint( context, maxx, maxy, midx, maxy, radius_tr );
    CGContextAddArcToPoint( context, minx, maxy, minx, midy, radius_tl );
    CGContextClosePath( context );
    CGContextDrawPath( context, kCGPathFill );

    CGImageRef bitmapContext = CGBitmapContextCreateImage( context );
    CGContextRelease( context );

    UIImage *theImage = [UIImage imageWithCGImage:bitmapContext];
    CGImageRelease(bitmapContext);

    return theImage.layer.mask;
}
于 2013-04-23T09:10:51.503 回答
0

不得不改变视角。由于自动调整大小,viewDidLoad 中的底角无法使用。不得不将代码移动到 viewDidAppear。

-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];

CGRect bounds = self.view.bounds;
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerBottomLeft) cornerRadii:CGSizeMake(5.0, 5.0)];

CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = bounds;
maskLayer.path = maskPath.CGPath;
self.view.layer.mask = maskLayer;

self.view.layer.masksToBounds = YES;
[self.view setNeedsDisplay];
}
于 2015-04-14T23:39:41.223 回答
0

最后我成功了,它确实有效。试试看。

CGRect frame = self.accountBackgroundImage2.frame;
frame.size.height = self.view.bounds.size.height;
UIBezierPath *path=[UIBezierPath bezierPathWithRoundedRect:self.accountBackgroundImage2.bounds byRoundingCorners:(UIRectCornerBottomLeft |UIRectCornerBottomRight) cornerRadii:CGSizeMake(12.5, 12.5)];

CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];
shapeLayer.frame = self.accountBackgroundImage2.bounds;
shapeLayer.path  = path.CGPath;
_accountBackgroundImage2.layer.mask = shapeLayer;
self.accountBackgroundImage2.frame = frame;
于 2015-05-21T10:56:07.507 回答
0

另一种方式

在“ viewWillLayoutSubviews ”中执行

- (void)viewWillLayoutSubviews{
[self roundCornersOnView:self.buttonJoin onTopLeft:NO topRight:YES bottomLeft:YES bottomRight:NO radius:10.0];
}

功能

-(UIButton *)roundCornersOnView:(UIButton *)button onTopLeft:(BOOL)tl topRight:(BOOL)tr bottomLeft:(BOOL)bl bottomRight:(BOOL)br radius:(float)radius {

if (tl || tr || bl || br) {
    UIRectCorner corner = 0; //holds the corner
    //Determine which corner(s) should be changed
    if (tl) {
        corner = corner | UIRectCornerTopLeft;
    }
    if (tr) {
        corner = corner | UIRectCornerTopRight;
    }
    if (bl) {
        corner = corner | UIRectCornerBottomLeft;
    }
    if (br) {
        corner = corner | UIRectCornerBottomRight;
    }

    UIButton *roundedView = button;
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:roundedView.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(radius, radius)];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = roundedView.bounds;
    maskLayer.path = maskPath.CGPath;
    roundedView.layer.mask = maskLayer;
    return roundedView;
} else {
    return button;
}

}

于 2015-08-04T20:56:50.040 回答