简体   繁体   English

Swift-在多个随机CGPoint之间绘制线条

[英]Swift - Draw lines between several random CGPoints

I am trying to create a simple line graph that should display the average monthly user score. 我正在尝试创建一个简单的折线图,该折线图应显示平均每月用户得分。 So far I have created a collectionView with a white dot at the top of each cell, but now I would like to have a straight line between each dot. 到目前为止,我已经创建了一个collectionView,每个单元格的顶部都有一个白色的点,但是现在我希望每个点之间都有一条直线。 I have added a temporary array for testing purposes. 我添加了一个临时数组用于测试。

Since it will be variable server data (the last six months), the location of the dots can be different each time. 由于这将是可变的服务器数据(最近六个月),因此点的位置每次都可以不同。 What should I do to connect the first dot with the second one and the second one with the next one (until all dots are connected)? 我应该怎么做才能将第一个点与第二个点连接,将第二个点与下一个点连接(直到所有点都连接在一起)?

User profile data controller 用户个人资料数据控制器

class UserProfileData: UICollectionViewController, UICollectionViewDelegateFlowLayout {

var cellId = "cellId"

let values: [CGFloat] = [4.5, 3.2, 4.8, 5.0, 2.3, 2.9]

init() {
    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = UICollectionViewScrollDirection.horizontal

    super.init(collectionViewLayout: layout)
}

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

override func viewDidLoad() {
    super.viewDidLoad()

    self.collectionView?.dataSource = self
    self.collectionView?.delegate = self

    collectionView?.backgroundColor = Constants.MAIN_THEME_COLOR
    collectionView?.isScrollEnabled = false
    collectionView?.register(BarCell.self, forCellWithReuseIdentifier: cellId)
}

// MARK: UICollectionViewDataSource
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return values.count
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: 50, height: view.frame.height - 20)
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! BarCell

    if let max = values.max() {
        let value = values[indexPath.item]
        let ratio = value / max

        cell.barHeightConstraint?.constant = view.frame.height * ratio
    }

    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    return UIEdgeInsets(top: 0, left: 4, bottom: 0, right: 4)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 10
}
}

My custom cell 我的自定义单元

// Create custom cell(s)
class BarCell: UICollectionViewCell {

let barView: UIView = {
    let view = UIView()
    view.backgroundColor = .clear
    view.translatesAutoresizingMaskIntoConstraints = false
    return view
}()

let barLabel: UILabel = {
    let label = UILabel()
    label.text = "."
    label.textColor = .white
    label.font = UIFont.systemFont(ofSize: 70)
    label.translatesAutoresizingMaskIntoConstraints = false
    return label
}()

var barHeightConstraint: NSLayoutConstraint?

override init(frame: CGRect) {
    super.init(frame: frame)

    addSubview(barView)
    barView.addSubview(barLabel)

    barHeightConstraint = barView.heightAnchor.constraint(equalToConstant: 300)
    barHeightConstraint?.isActive = true
    barHeightConstraint?.constant = 100

    barView.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
    barView.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
    barView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true

    barLabel.centerXAnchor.constraint(equalTo: barView.centerXAnchor).isActive = true
    barLabel.topAnchor.constraint(equalTo: barView.topAnchor, constant: 5).isActive = true
}

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

A UICollectionView has a backgroundView property. UICollectionView具有backgroundView属性。 You should be able to set the backgroundView of your UICollectionView to a custom UIView that draws the lines between your items. 您应该能够将UICollectionViewbackgroundView设置为自定义UIView,该UIView在项目之间绘制线条。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM