ios - 在 swift 中为 Ui Collection View 创建一个 Arc

标签 ios swift

您好,我想在 ios swift 中创建以下 View 以添加到 UIcollection View 中,我该如何实现?

我当前要绘制的代码正在返回圆形 View

@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



}

enter image description here

最佳答案

您可以使用 BezierPath 在 View 中绘制圆弧。将此 arcView 添加到您的 Collection View 单元格中。

让我们看看 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()
}
}

圆弧的中心是 View 的中间。 沿逆时针方向绘制从 -10 度到 -80 度的圆弧。

在 ViewController 的 View 层次结构中添加 ArcView(您将在 collectionView Cell 中添加此 View )

 import UIKit

 class ViewController: UIViewController {

     private var arcView : ArcView!

     override func viewDidLoad() {
        super.viewDidLoad()

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

   }

输出:

The Arc

The sample code is here

关于ios - 在 swift 中为 Ui Collection View 创建一个 Arc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47953052/

相关文章:

ios - NSUserDefaults objectForKey 有时为零

ios - cocos2d中的交错 map 显示错误

ios - 当我选择单元格时,UIImageView.image 在我的 UITableViewCell 中为 nil,即使我知道它应该有一个值

iOS 10 : can't get authorization to use images in MessagesExtensions app

ios - FaSTLane Boarding 与双因素身份验证

ios - 字符串格式问题 - 新行未正确打印

swift - 提交后如何摆脱 xib?

ios - 表格 View 末尾的巨大空白区域

ios - 在 PromiseKit 中获取第一个响应对象,然后完成 block

swift - 如何从 uitableviewcell 中的 uiview 获取单元格索引 (swift/xcode)