Netease-Music-Demo by sss
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DiscoverPopularSongsView.swift 4.2KB

3 anni fa
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // DiscoverPopularSongs.swift
  3. // wyy
  4. //
  5. // Created by 施书顺 on 2021/3/5.
  6. //
  7. import UIKit
  8. class DiscoverPopularSongsView: UIView, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
  9. struct SongData {
  10. var albumImageName: String
  11. var songName: String
  12. var authorName: String
  13. var comment: String
  14. }
  15. let popularSongLabel = UILabel()
  16. let playButton = UIButton()
  17. var collectionView : UICollectionView!
  18. let layout = UICollectionViewFlowLayout()
  19. var dataSource = [
  20. SongData(albumImageName: "album1", songName: "Song Name", authorName: "Author", comment: "This is a comment"),
  21. SongData(albumImageName: "album2", songName: "Song Name", authorName: "Author", comment: "This is a comment"),
  22. SongData(albumImageName: "album3", songName: "Song Name", authorName: "Author", comment: "This is a comment"),
  23. SongData(albumImageName: "album3", songName: "Song Name", authorName: "Author", comment: "This is a comment"),
  24. SongData(albumImageName: "album2", songName: "Song Name", authorName: "Author", comment: "This is a comment"),
  25. SongData(albumImageName: "album1", songName: "Song Name", authorName: "Author", comment: "This is a comment")]
  26. init(){
  27. super.init(frame: CGRect.zero)
  28. layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
  29. layout.itemSize = CGSize(width: 250, height: 50)
  30. layout.scrollDirection = .horizontal
  31. collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
  32. collectionView.collectionViewLayout = layout
  33. collectionView.dataSource = self
  34. collectionView.delegate = self
  35. collectionView.backgroundColor = UIColor.white
  36. // collectionView.alwaysBounceVertical = true
  37. collectionView.register(PopularSongCell.self, forCellWithReuseIdentifier: "cell")
  38. // collectionView.contentSize = CGSize(width: 600, height: 200)
  39. addSubview(collectionView)
  40. collectionView.snp.makeConstraints{ make in
  41. make.width.equalToSuperview()
  42. make.height.equalTo(200)
  43. make.top.equalTo(20)
  44. }
  45. popularSongLabel.text = "歌曲推荐"
  46. popularSongLabel.font = UIFont.systemFont(ofSize: 20, weight: .bold)
  47. addSubview(popularSongLabel)
  48. popularSongLabel.snp.makeConstraints{ make in
  49. make.top.equalTo(5)
  50. make.left.equalTo(15)
  51. }
  52. playButton.setTitle(" ▶ 播放 ", for: .normal)
  53. playButton.setTitleColor(.black, for: .normal)
  54. playButton.titleLabel?.font = UIFont.systemFont(ofSize: 15)
  55. playButton.layer.borderColor = UIColor.gray.withAlphaComponent(0.5).cgColor
  56. playButton.layer.borderWidth = 1.0
  57. playButton.layer.cornerRadius = 10
  58. addSubview(playButton)
  59. playButton.snp.makeConstraints{ make in
  60. make.top.equalTo(0)
  61. make.right.equalTo(-10)
  62. make.width.equalTo(60)
  63. make.height.equalTo(30)
  64. }
  65. collectionView.reloadData()
  66. }
  67. required init?(coder: NSCoder) {
  68. fatalError("init(coder:) has not been implemented")
  69. }
  70. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  71. dataSource.count
  72. }
  73. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  74. var cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? PopularSongCell
  75. let data = dataSource[indexPath.row]
  76. if cell == nil {
  77. cell = PopularSongCell(frame: CGRect.zero)
  78. }
  79. cell!.albumImageView.image = UIImage(named: data.albumImageName)
  80. cell!.songLabel.text = data.songName
  81. cell!.authorLabel.text = " - \(data.authorName)"
  82. cell!.commentLabel.text = data.comment
  83. return cell!
  84. }
  85. // func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  86. // return CGSize(width: 200, height: 50)
  87. // }
  88. }