如何快速制作形状

问题描述

如何制作这样的形状

enter image description here

func createShape() {
        bezierPath = UIBezierPath()
        bezierPath.move(to: .zero)
        bezierPath.addLine(to: CGPoint(x:self.frame.width,y: self.frame.origin.y))
        bezierPath.addLine(to: CGPoint(x: self.frame.width,y: self.frame.height / 2))
        bezierPath.addCurve(to: CGPoint(x:self.frame.width/2,y: self.frame.height),controlPoint1: CGPoint(x: self.frame.width,y: self.frame.height / 2),controlPoint2: CGPoint(x:self.frame.width/2  + 33,y: self.frame.height))
        bezierPath.addCurve(to: CGPoint(x: 0,controlPoint1: CGPoint(x:  self.frame.width/2 - 33,y:self.frame.height),controlPoint2: CGPoint(x: 0,y: self.frame.height / 2))
        bezierPath.addLine(to: .zero)
        bezierPath.close()
    }

结果我得到了

enter image description here

你能帮我吗?

解决方法

了解曲线的工作方式here

let bezierPath = UIBezierPath()
        bezierPath.move(to: .zero)
        bezierPath.addLine(to: CGPoint(x: 0,y: self.frame.height/2))
        bezierPath.addCurve(to:  CGPoint(x:self.frame.width,y: self.frame.height / 2),controlPoint1: CGPoint(x: 0,y: (self.frame.height+self.frame.width)/2),controlPoint2: CGPoint(x: self.frame.width,y: (self.frame.height+self.frame.width)/2))
        bezierPath.addLine(to: CGPoint(x: self.frame.width,y: 0))
        bezierPath.addLine(to: .zero)

enter image description here

,

这是您需要的图形..它将通过UIBezierpath addArc方法为您提供完美的圆

import UIKit

@IBDesignable class CustomView: UIView {

 private lazy var shapeLayer = CAShapeLayer()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
        
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        commonInit()
    }
    
    private func commonInit() {
        
        shapeLayer.fillColor = UIColor.black.cgColor
        shapeLayer.strokeColor = UIColor.red.cgColor
        shapeLayer.borderWidth = 5
        shapeLayer.frame = bounds
        
        
        layer.addSublayer(shapeLayer)
        
    }
    
    override func layoutSubviews() {
        
        shapeLayer.path = drawShape()
    }
    
    
    private func drawShape() -> CGPath {
        
        let bezierPath = UIBezierPath()
        
        bezierPath.move(to: .zero)
        bezierPath.addLine(to: CGPoint(x: 0,y: bounds.midY/2))
        bezierPath.addArc(withCenter: CGPoint(x: bounds.midX,y:  bounds.midY),radius: bounds.midX,startAngle: .pi,endAngle: 0,clockwise: false)
        
        bezierPath.addLine(to: CGPoint(x: bounds.maxX,y: 0))
        bezierPath.close()
        
        return bezierPath.cgPath
        
        
    }
    
    
}

enter image description here

具有背景色

enter image description here

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...