制作任何尺寸的按钮?

时间:2016-11-22 05:57:30

标签: ios uibutton

我试图创建一个半圆形的按钮。

内部按钮类

 layoutSubviews() { 
    createSemiCirclePath()
 }

 func createSemiCirclePath(){
       self.translatesAutoresizingMaskIntoConstraints = false
       self.clipsToBounds = true
       semiCircleShape.path = UIBezierPath.init(arcCenter: CGPoint(x: self.frame.size.width/2 , y:0), radius: self.frame.size.width/2, startAngle: 0, endAngle: CGFloat(M_PI), clockwise: true).cgPath
       self.layer.addSublayer(semiCircleShape) 
       self.layer.masksToBounds = true

    }

我得到了这个:

enter image description here

但我想这样:

enter image description here

如果我正在尝试

 func createSemiCirclePath(){
       self.translatesAutoresizingMaskIntoConstraints = false
       self.clipsToBounds = true
       semiCircleShape.path = UIBezierPath.init(arcCenter: CGPoint(x: self.frame.size.width/2 , y:0), radius: self.frame.size.width/2, startAngle: 0, endAngle: CGFloat(M_PI), clockwise: true).cgPath
       self.layer.mask = semiCircleShape
       self.layer.masksToBounds = true

    }

我的按钮是

enter image description here

它也允许点击按钮高度的50%以下,这是我不需要的。

(在1个代码中)

问题是,只有黑色部分应该是可点击的,蓝色部分应该被隐藏,即它应该剪切到子层。

任何建议表示赞赏。 感谢。

1 个答案:

答案 0 :(得分:0)

可能是UIControl类的子类,它包含处理触摸事件的方法,它也处理目标/选择例程。 此类也不包含自定义绘制逻辑作为UIButton。 在您的示例中,您应该只扩展UIControl并添加

controlInstance.backgroundColor = [UIColor clearColor];