简体   繁体   English

如何在 UIView 中为 swift 添加曲线左侧和右侧

[英]How to add Curve left and right side in UIView for swift

I tried to add a curve but not working.我试图添加一条曲线但不工作。

I have attached the image left side is given by the designer, the right side I have done in swift.我附上了左边的图片是设计师给的,右边是我在swift做的。

here the if I add corner radius not working for the curve if add curve corner radius not working.如果添加曲线拐角半径不起作用,则此处如果我添加拐角半径不适用于曲线。

I have added left side and right side separate views, how it's possible to achieve the remove the super view background-color and curve.我添加了左侧和右侧单独的视图,如何实现删除超级视图背景颜色和曲线。

extension UIView {

   func roundCorners(
      corners: UIRectCorner,
      radius: CGFloat
   ) {
      let path = UIBezierPath(
         roundedRect: bounds,
         byRoundingCorners: corners,
         cornerRadii: CGSize(
            width: radius,
            height: radius
         )
      )

      let mask = CAShapeLayer()
      mask.path = path.cgPath
      layer.mask = mask
   }
}

// here I am using on UITableviewCell in layoutSubview 

    leftCurve.roundCorners(corners: [.topRight,.bottomRight], radius: 20)        
    rightCurver.roundCorners(corners: [.topLeft,.bottomLeft], radius: 20)

在此处输入图像描述

Here is a demo for add curve to both side using UIBezierPath这是使用UIBezierPath向两侧添加曲线的演示

class ShapeView: UIView {
    
    var path: UIBezierPath = UIBezierPath()
    
    // Set you circle position for verticle.
    var circleYPosition: CGFloat = 50 {
        didSet {
            self.setNeedsDisplay()
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.backgroundColor = UIColor.clear
        // Here apply shadow
    }
    
    override func draw(_ rect: CGRect) {
        super.draw(rect)
        // 4 corners radious
        UIBezierPath(roundedRect: rect, byRoundingCorners: .allCorners, cornerRadii: CGSize(width: 10, height: 10)).addClip()
        
        // Left - right circle
        path.move(to: CGPoint(x: 0, y: self.frame.size.height))
        
        //left side
        path.addArc(withCenter: CGPoint(x: 0, y: self.circleYPosition - 15),
            radius: 10,
            startAngle: CGFloat((90 * Double.pi) / 180),
            endAngle: CGFloat((270 * Double.pi) / 180),
            clockwise: false)
        
        path.addLine(to: CGPoint(x: 0, y: 0))
        path.addLine(to: CGPoint(x: self.frame.size.width, y: 0))
        path.addLine(to: CGPoint(x: self.frame.size.width, y: self.frame.size.height))
        
        path.move(to: CGPoint(x: self.frame.size.width, y: self.frame.size.height))
        
        //right side
        path.addArc(withCenter: CGPoint(x: self.frame.size.width, y: self.circleYPosition - 15),
                    radius: 10,
                    startAngle: CGFloat((270 * Double.pi) / 180),
                    endAngle: CGFloat((90 * Double.pi) / 180),
                    clockwise: false)
        
        
        path.close()
        UIColor.white.setFill()
        path.fill()
        
        // Center Dash path
        let dashPath = UIBezierPath()
        dashPath.move(to: CGPoint(x: self.bounds.minX + 12, y: self.circleYPosition - 15))
        dashPath.addLine(to: CGPoint(x: self.bounds.maxX - 12, y: self.circleYPosition - 15))
        dashPath.setLineDash([5,5], count: 2, phase: 0.0)
        dashPath.lineWidth = 1.0
        dashPath.lineCapStyle = .butt
        UIColor.lightGray.set()
        dashPath.stroke()
    }
}

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM