画一个半圆形的buttoniOS

我正在画一个半圆形的button。 我在绘制半圆并将其附加到我在xcode中创build的button时遇到了麻烦。 xcode中的button有一个约束,将其固定在屏幕底部并居中。 我参考我的视图控制器中的button,然后尝试重写它为一个半圆如下。 我得到一个空白的button。 我也硬编码在故事板的button所在的坐标。 有没有更好的方法来做到这一点?

let circlePath = UIBezierPath.init(arcCenter: CGPoint(x: 113.0, y: 434.0), radius: 10.0, startAngle: 0.0, endAngle: CGFloat(M_PI), clockwise: true) let circleShape = CAShapeLayer() circleShape.path = circlePath.CGPath sunButton.layer.mask = circleShape 

问题是你的形状图层位于button边界之外。 如果只是将形状图层添加为button图层的子视图,则会看到问题:

 button.layer.addSublayer(circleShape) 

在这里输入图像说明

您必须调整圆弧中心点,以便圆弧位于button的边界内:

 let circlePath = UIBezierPath.init(arcCenter: CGPointMake(button.bounds.size.width / 2, 0), radius: button.bounds.size.height, startAngle: 0.0, endAngle: CGFloat(M_PI), clockwise: true) let circleShape = CAShapeLayer() circleShape.path = circlePath.CGPath button.layer.mask = circleShape 

你会得到这个:

在这里输入图像说明