CGRect rect = CGRectMake(100, 100, 150, 250);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:rect];
imageView.image = [UIImage imageNamed:@"brand_launch"];
[self.view addSubview:imageView];
CAShapeLayer *layer = [CAShapeLayer layer];
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(0, 15)];
[path addLineToPoint:CGPointMake(15, 15)];
[path addArcWithCenter:CGPointMake(15+5, 5) radius:5 startAngle:M_PI endAngle:1.5*M_PI clockwise:YES];
[path addArcWithCenter:CGPointMake(150-5, 5) radius:5 startAngle:1.5*M_PI endAngle:2*M_PI clockwise:YES];
[path addArcWithCenter:CGPointMake(150-5, 250-5) radius:5 startAngle:0 endAngle:M_PI_2 clockwise:YES];
[path addArcWithCenter:CGPointMake(15+5, 250-5) radius:5 startAngle:M_PI_2 endAngle:M_PI clockwise:YES];
[path addLineToPoint:CGPointMake(15, 30)];
[path closePath];
layer.path = path.CGPath;
imageView.layer.mask = layer;
複製程式碼
"思路: 如果想應用在多種圖片格式上? "
我可能會考慮把這個圓角放到一個UIView上,
然後通過重寫View的LayoutSubviews方法來實時取到正確的view的寬高, 來實現繪製
各人按需求封裝一下吧
複製程式碼