需求是多种多样的,比如要一个固定方向是圆角,其他方向正常的view,或许本文可以帮到你.
先看一下效果:这里是左上和右下为圆角,其他角度正常.
代码如下:
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
view.backgroundColor = [UIColor grayColor];
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(50, 50)];
CAShapeLayer *layer = [[CAShapeLayer alloc]init];
layer.frame = view.bounds;
layer.path = maskPath.CGPath;
view.layer.mask = layer;
圆角的方向:
UIRectCornerTopLeft = 1 << 0,
UIRectCornerTopRight = 1 << 1,
UIRectCornerBottomLeft = 1 << 2,
UIRectCornerBottomRight = 1 << 3,
UIRectCornerAllCorners = ~0UL