[英]How to make a shape in swift
How to make a shape like this如何做出这样的形状
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, y: self.frame.height / 2), 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()
}
and as a result i got结果我得到了
Can you help me?你能帮助我吗?
read how a curve work 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)
Here is the drawing that you need.. it will give you perfect circle with UIBezierpath
addArc
method这是您需要的绘图.. 它会用
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
}
}
with Background color带背景色
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.