iOS View 指定圆角位置

圆角在iOS开发中是十分常见的视觉效果。一般情况下设置layer的cornerRadiusj就可以实现圆角效果,这样产生的圆角效果是四个角都有效果的,下面的例子提供一种方法,可以让你指定圆角的位置(三个圆角,一个直角)。

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIButton *btnAngle;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //The rectangle that defines the basic shape of the path.
    CGRect rect = [self.btnAngle bounds];
    
    //The radius of each corner oval.
    CGSize radii = CGSizeMake(10,10);
    
    //A bitmask value that identifies the corners that you want rounded.
    UIRectCorner corners = UIRectCornerTopLeft | UIRectCornerTopRight | UIRectCornerBottomLeft;
    
    //Creates and returns a new UIBezierPath object initialized with a rounded rectangular path.
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corners cornerRadii:radii];
    
    
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.strokeColor = [UIColor blueColor].CGColor;
    shapeLayer.fillColor = [UIColor redColor].CGColor;
    shapeLayer.lineWidth = 2;
    shapeLayer.lineJoin = kCALineJoinRound;
    shapeLayer.lineCap = kCALineCapRound;
    shapeLayer.path = path.CGPath;
    
    [self.btnAngle.layer addSublayer:shapeLayer];
}


iOS View 指定圆角位置_第1张图片


你可能感兴趣的:(iOS)