ios - 以曲线形状剪切贝塞尔曲线路径

标签 ios swift uikit uibezierpath curve

我使用 Swift 在手机 UI 中创建自定义按钮,需要将按钮的底部剪成凸状。

Like this

问题是它应该是完美的并且底部曲线应该匹配更大的按钮

Like this

我尝试了不同的方法,但没有得到完美的结果。

  1. 使用圆角

    if buttonDirection == 0 {
        path = UIBezierPath(roundedRect: rect, byRoundingCorners:[UIRectCorner.topLeft, .topRight], cornerRadii: CGSize(width: self.bounds.width/2, height: 0))
        path.close()  
    }
    
  2. 使用 QuardCurve 创建没有圆角曲线的整个路径//底部仍然不是反向曲线加上拐角也不像 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()
        }
    
  3. 从 1 创建路径,然后只是 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()
    } 
    

我也尝试了很多其他的东西但没有得到结果,我想要的只是从第一条路径剪下底部曲线,比如如何从另一条路径上剪下一条路径(阅读苹果文档后我开始知道我可以使用路径.reverse() 也 )

这是一张图片中的所有按钮,当您单击按钮时说 vol + 它会改变颜色

All four buttons vol+, vol-, ch+,ch- and centre button(which itself is subdivided in five-part :-pc)

PS 编辑了原始问题,因为图像令人困惑,我找到了一个解决方案,但仍然想知道是否有更好的解决方案,只有一条路径和光滑的边缘。

最佳答案

以下方法有效,但如果有人有更好的解决方案,请发帖。 基本上我在这里做的是首先将整个按钮的底部剪裁成凸面,在底部使用 addLines 和 Quardcurve,然后使用右上角和左上角的 bezier roundedRect 初始化创建新路径。

if buttonDirection == 0 {
     let curve = UIBezierPath()
     curve.move(to: CGPoint(x: 0, y: self.bounds.height))
     curve.addLine(to: CGPoint(x: 0, y: 0))
     curve.addLine(to: CGPoint(x: self.bounds.width, y: 0))
     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.height - self.bounds.height/4))
     curve.close()
     curve.addClip()
     path = UIBezierPath(roundedRect: rect, byRoundingCorners:[UIRectCorner.topLeft, .topRight], cornerRadii: CGSize(width: self.bounds.width/2, height: 0))
     path.close()
} 

关于ios - 以曲线形状剪切贝塞尔曲线路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50699680/

相关文章:

objective-c - 将地址转换为 GeoPoint - 服务器端还是客户端?

ios - 创建基于自动布局的指标 View

ios - 幽灵 View Controller

ios - 在 UIWebView/WKWebview 中禁用自动完成

ios - 转发 UIAlertView 的可变参数

iphone - iOS 多任务手势 - 禁用/启用

swift - 添加新的 View Controller B 并将其更改为初始 View Controller 后出现 "View Controller A unreachable because it has no entry points"错误

ios - PLCrashReporter ios6 崩溃

ios - Alamofire 4 重试器和适配器无法看到更改的 accessToken

iphone - UIControl:将多个目标- Action 对添加到同一事件时的调用顺序?