我正在使用Swift在手机UI中创建自定义按钮,并需要将按钮底部剪裁成凸形。问题是它应该是完美的,底部曲线应该与更大的按钮匹配。我尝试了不同的方法,但没有得到完美的结果。如下图所示:
这是所有按钮在一个图片中,当你点击按钮时,比如说vol+,它会改变颜色。
PS:编辑了原问题,因为图片很混乱,我找到了一个解决方案,但仍然想知道是否有更好的解决方案只需要一条路径和平滑的边缘。
Using rounded corners
if buttonDirection == 0 { path = UIBezierPath(roundedRect: rect, byRoundingCorners:[UIRectCorner.topLeft, .topRight], cornerRadii: CGSize(width: self.bounds.width/2, height: 0)) path.close() }
Create whole path without rounded curve using QuardCurve // still bottom part not inverse curve plus corner are also not smooth as 1
if buttonDirection == 0 { //path = UIBezierPath(roundedRect: rect, byRoundingCorners:[UIRectCorner.topLeft, .topRight], cornerRadii: CGSize(width: self.bounds.width/2, height: 0)) //path.close() let curve = UIBezierPath() curve.move(to: CGPoint(x: 0, y: self.bounds.height)) curve.addLine(to:CGPoint(x: 0, y: self.bounds.width/3)) curve.addQuadCurve(to: CGPoint(x: self.bounds.width, y: self.bounds.width/3), controlPoint: CGPoint(x: self.bounds.width/2, y: -5)) curve.addLine(to: CGPoint(x: self.bounds.width, y: self.bounds.height)) curve.addQuadCurve(to: CGPoint(x: 0, y: self.bounds.height), controlPoint: CGPoint(x: self.bounds.width/2, y: self.bounds.width - 10)) UIColor.black.setFill() curve.fill() }
Create the path from 1 and then just Clip QuardCurve
if buttonDirection == 0 { path = UIBezierPath(roundedRect: rect, byRoundingCorners:[UIRectCorner.topLeft, .topRight], cornerRadii: CGSize(width: self.bounds.width/2, height: 0)) path.close() path.move(to: CGPoint(x: 0, y: self.bounds.height)) path.addQuadCurve(to: CGPoint(x: self.bounds.width, y: self.bounds.height), controlPoint: CGPoint(x: self.bounds.width/2, y: self.bounds.height/2)) path.addClip() UIColor.black.setFill() path.fill() }
这是所有按钮在一个图片中,当你点击按钮时,比如说vol+,它会改变颜色。
PS:编辑了原问题,因为图片很混乱,我找到了一个解决方案,但仍然想知道是否有更好的解决方案只需要一条路径和平滑的边缘。