iOS 圆角设置

使用CAShapeLayer和UIBezierPath设置圆角


 UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight - 50, ScreenWidth, 50)];
    [self.view addSubview:bottomView];
   
    UIBezierPath *maskPath = [UIBezierPath  bezierPathWithRoundedRect:bottomView.bounds byRoundingCorners:UIRectCornerTopRight|UIRectCornerTopLeft cornerRadii:bottomView.bounds.size];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
    //设置大小
    maskLayer.frame = bottomView.bounds;
    //设置图形样子
    maskLayer.path = maskPath.CGPath;
    bottomView.layer.mask = maskLayer;
    bottomView.clipsToBounds = YES;
   
    UIButton *scoreButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 90, 50)];
    [bottomView addSubview:scoreButton];
    scoreButton.backgroundColor = RGB(75, 185, 120);
    [scoreButton setTitle:@"200 积分" forState:UIControlStateNormal];
    scoreButton.titleLabel.font = [UIFont systemFontOfSize:14];
   
    UIButton *jiedaButton = [[UIButton alloc] initWithFrame:CGRectMake(90, 0, ScreenWidth - 90, 50)];
    [bottomView addSubview:jiedaButton];
    jiedaButton.backgroundColor = RGB(235, 175, 88);
    [jiedaButton setTitle:@"解答" forState:UIControlStateNormal];
    [jiedaButton addTarget:self action:@selector(answerDidClicked) forControlEvents:UIControlEventTouchUpInside];
    jiedaButton.titleLabel.font = [UIFont systemFontOfSize:14];


指定需要成为圆角的的角

+ (UIBezierPath *)bezierPathWithRoundedRect:(CGRect)rect

                          byRoundingCorners:(UIRectCorner)corners

                                cornerRadii:(CGSize)cornerRadii

corners参数指定了要成为圆角的角, 枚举类型如下:

typedef NS_OPTIONS(NSUInteger, UIRectCorner) {

    UIRectCornerTopLeft    =1<<0,

    UIRectCornerTopRight    =1<<1,

    UIRectCornerBottomLeft  =1<<2,

    UIRectCornerBottomRight =1<<3,

    UIRectCornerAllCorners  = ~0UL};


©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容