Netease-Music-Demo by sss
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

DiscoverRecommendView.swift 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //
  2. // DiscoverRecommendView.swift
  3. // wyy
  4. //
  5. // Created by 施书顺 on 2021/3/5.
  6. //
  7. import UIKit
  8. class DiscoverRecommendView: UIView {
  9. let recommendLabel = UILabel()
  10. let moreLabel = UILabel()
  11. let scrollView = UIButtonScrollView()
  12. var albumButtons = [AlbumButton]()
  13. struct AlbumData {
  14. var imageName: String
  15. var title: String
  16. var count: Int
  17. }
  18. var data = [AlbumData(imageName: "album1", title: "这是一个歌单标题", count: 1234),
  19. AlbumData(imageName: "album2", title: "歌单标题", count: 100),
  20. AlbumData(imageName: "album3", title: "我是一个歌单~~~~~", count: 99999),
  21. AlbumData(imageName: "album1", title: "这是一个歌单标题", count: 1234),
  22. AlbumData(imageName: "album2", title: "歌单标题", count: 100),
  23. AlbumData(imageName: "album3", title: "我是一个歌单~~~~~", count: 99999),
  24. AlbumData(imageName: "album3", title: "我是一个歌单~~~~~", count: 99999),]
  25. init(){
  26. super.init(frame: CGRect.zero)
  27. recommendLabel.text = "推荐歌单"
  28. recommendLabel.font = UIFont.systemFont(ofSize: 20, weight: .bold)
  29. addSubview(recommendLabel)
  30. recommendLabel.snp.makeConstraints{ make in
  31. make.top.equalTo(5)
  32. make.left.equalTo(20)
  33. }
  34. moreLabel.text = " 更多 >"
  35. moreLabel.font = UIFont.systemFont(ofSize: 15)
  36. moreLabel.textAlignment = .center
  37. moreLabel.layer.borderColor = UIColor.gray.withAlphaComponent(0.5).cgColor
  38. moreLabel.layer.borderWidth = 1.0
  39. moreLabel.layer.cornerRadius = 10
  40. addSubview(moreLabel)
  41. moreLabel.snp.makeConstraints{ make in
  42. make.top.equalTo(0)
  43. make.right.equalTo(-10)
  44. make.width.equalTo(60)
  45. make.height.equalTo(30)
  46. }
  47. scrollView.canCancelContentTouches = true
  48. scrollView.bounces = true
  49. scrollView.showsHorizontalScrollIndicator = false
  50. scrollView.showsVerticalScrollIndicator = false
  51. // 20 * 2 + 100 * 6 + 15 * 4
  52. scrollView.contentSize = CGSize(width: 20 * 2 + 100 * data.count + 15 * 4, height: 120)
  53. addSubview(scrollView)
  54. scrollView.snp.makeConstraints{ make in
  55. make.top.equalTo(recommendLabel.snp.bottom).offset(10)
  56. make.width.equalToSuperview()
  57. make.height.equalTo(140)
  58. }
  59. for item in data {
  60. let alb = AlbumButton(imageName: item.imageName, title: item.title, count: item.count)
  61. scrollView.addSubview(alb)
  62. alb.snp.makeConstraints{ make in
  63. if albumButtons.isEmpty {
  64. make.left.equalTo(20)
  65. } else {
  66. make.left.equalTo(albumButtons.last!.snp.right).offset(15)
  67. }
  68. make.top.equalTo(0)
  69. }
  70. albumButtons.append(alb)
  71. }
  72. }
  73. required init?(coder: NSCoder) {
  74. fatalError("init(coder:) has not been implemented")
  75. }
  76. }