美文网首页
Button单边倒角

Button单边倒角

作者: Leon1024 | 来源:发表于2018-11-19 17:35 被阅读0次
    // 创建一个Button
       UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
       button.frame = CGRectMake(100, 200, 100, 40);
       [button setTitle:@"按钮" forState:UIControlStateNormal];
       [button setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
       [button setBackgroundColor:UIColor.blueColor];
       [self.view addSubview:button];
       
       // 按bounds来画贝塞尔曲线路径
       CGRect rect = button.bounds;
       CGSize radii = CGSizeMake(rect.size.height * 0.5, rect.size.height * 0.5);
       // 贝塞尔曲线倒角
       UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft cornerRadii:radii];
       // 创建并按曲线绘制图层
       CAShapeLayer *layer = [CAShapeLayer layer];
       layer.path = path.CGPath;
       // 将图层设置为button的遮罩层
       button.layer.mask = layer;
    
    
    Swift:
    // CACornerMask.layerMaxXMinYCorner.rawValue 右上,
    // CACornerMask.layerMaxXMaxYCorner.rawValue 右下
    if #available(iOS 11.0, *) {
              // iOS11:只需要带用这个系统方法就可以随意设置View的圆角了,是不是很方便,赶快试一下吧
               userInfoBackgroundView.layer.cornerRadius = 21
               userInfoBackgroundView.layer.maskedCorners = CACornerMask(rawValue: CACornerMask.layerMaxXMinYCorner.rawValue | CACornerMask.layerMaxXMaxYCorner.rawValue)
           } else {
               let rect = CGRect(x: 0, y: 0, width: 180, height: 42)
               let size = CGSize(width: rect.width * 0.5, height: rect.height * 0.5);
               // 贝塞尔曲线倒角
               let path = UIBezierPath(roundedRect: rect, byRoundingCorners: [.topRight, .bottomRight], cornerRadii: size);
               // 创建并按曲线绘制图层
               let layer = CAShapeLayer();
               layer.path = path.cgPath;
               // 将图层设置为button的遮罩层
               userInfoBackgroundView.layer.mask = layer;
           }
    

    相关文章

      网友评论

          本文标题:Button单边倒角

          本文链接:https://www.haomeiwen.com/subject/rapafqtx.html