1. 程式人生 > >ios怎麼讓圖片只有上半邊圓角?

ios怎麼讓圖片只有上半邊圓角?

可以使用UIBezierPath的
+ (UIBezierPath *)bezierPathWithRoundedRect:(CGRect)rect
                          byRoundingCorners:(UIRectCorner)corners
                                cornerRadii:(CGSize)cornerRadii
引數corners指定了想要需要成為圓角的角。可選值為:
enum {
   UIRectCornerTopLeft     = 1 << 0,
   UIRectCornerTopRight    = 1 <<
1, UIRectCornerBottomLeft = 1 << 2, UIRectCornerBottomRight = 1 << 3, UIRectCornerAllCorners = ~0 }; typedef NSUInteger UIRectCorner;
從名字就能看出來其代表的意義。
例子:
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(120, 10, 80, 80)];
view2.backgroundColor = [UIColor redColor];
[self
.view addSubview:view2]; UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view2.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)]; CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = view2.bounds; maskLayer
.path = maskPath.CGPath; view2.layer.mask = maskLayer;
如果需要將UIView的4個角全部都為圓角,做法相當簡單,只需設定其Layer的cornerRadius屬性即可。