微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何快速制作形状

如何解决如何快速制作形状

如何制作这样的形状

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。