嗨,我想在ios swift中创建以下视图,以便添加到UIcollection视图中,我如何实现这一点?
我当前要绘制的代码返回圆视图

@IBInspectable public var fillColor: UIColor = #colorLiteral(red: 0.8078431487, green: 0.02745098062, blue: 0.3333333433, alpha: 1)    { didSet { setNeedsLayout() } }
@IBInspectable public var strokeColor: UIColor = #colorLiteral(red: 0.9254902005, green: 0.2352941185, blue: 0.1019607857, alpha: 1)  { didSet { setNeedsLayout() } }
@IBInspectable public var lineWidth: CGFloat = 0     { didSet { setNeedsLayout() } }

lazy private var shapeLayer: CAShapeLayer = {
    let _shapeLayer = CAShapeLayer()
    self.layer.insertSublayer(_shapeLayer, at: 0)
    return _shapeLayer
}()

override func layoutSubviews() {
    super.layoutSubviews()

    let center = CGPoint(x: bounds.midX, y: bounds.midY)
    let radius = (min(bounds.size.width, bounds.size.height) - lineWidth) / 2
    shapeLayer.path = UIBezierPath(arcCenter: center, radius: radius, startAngle: 0, endAngle: 90, clockwise: true).cgPath
    shapeLayer.fillColor = fillColor.cgColor
    shapeLayer.strokeColor = strokeColor.cgColor
    shapeLayer.lineWidth = lineWidth



}

ios - 快速为Ui集合 View 创建弧-LMLPHP

最佳答案

可以使用BezierPath在视图中绘制圆弧。将此arcView添加到集合视图单元格中。
让我们看看ArcView:

class ArcView : UIView {

override init(frame: CGRect) {
    super.init(frame: frame)
    backgroundColor = .white
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override func draw(_ rect: CGRect) {
    super.draw(rect)
    createArc(rect: rect)
}

private func createArc(rect : CGRect) {

    let center = CGPoint(x: rect.width/2, y: rect.height/2)
    let lineWidth : CGFloat = 50.0
    let radius = rect.width / 2 - lineWidth
    let startingAngle = CGFloat(-10.0/180) * CGFloat.pi
    let endingAngle = CGFloat(-80/180.0) * CGFloat.pi
    let bezierPath = UIBezierPath(arcCenter: center, radius: radius, startAngle: startingAngle , endAngle: endingAngle, clockwise: false)
    bezierPath.lineWidth = lineWidth
    UIColor(red: 249/255.0, green: 179/255.0, blue: 127/255.0, alpha: 1.0).setStroke()
    bezierPath.stroke()
}
}

弧线的中心是视图的中间。
沿逆时针方向画-10度到-80度的弧。
在ViewController的视图层次结构中添加ArcView(您将在collectionView单元格中添加此视图)
 import UIKit

 class ViewController: UIViewController {

     private var arcView : ArcView!

     override func viewDidLoad() {
        super.viewDidLoad()

        arcView = ArcView(frame: view.frame)
        view.addSubview(arcView)
        arcView.setNeedsDisplay()
     }

   }

输出:
ios - 快速为Ui集合 View 创建弧-LMLPHP
The sample code is here

关于ios - 快速为Ui集合 View 创建弧,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47953052/

10-09 02:45