2016-11-22 27 views
0

我试图创建一个按钮,形状为半圆形。制作任意大小的按钮?

里面的按钮类

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代码)

的问题是,只有黑色部分应该点击能够和蓝色部分应该被隐藏,即,它应该夹到子层。

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

+0

请检查该http://stackoverflow.com/questions/1561585/iphone-button-with-non-rectangle-shape –

+0

的HTTP可能重复:/ /stackoverflow.com/a/33310108/5215474 – Saranjith

+0

是的,但完整的矩形按钮是点击不仅能够半圆部分。 –

回答

0

可能是你可以继承UIControl类,它类包含的方法来处理触摸事件也是它类手柄目标/选择程序。
另外这个类不包含自定义绘制逻辑的UIButton。 在你例子中,你应该只延长UIControl并添加

controlInstance.backgroundColor = [UIColor clearColor]; 
+0

nope,它直接从UIButton类继承。 –

+0

这不是你的课吗? –

+0

类半圆:UIButton的这个我 –