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.

DiscoverButtonsView.swift 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. //
  2. // DiscoverButtonsView.swift
  3. // wyy
  4. //
  5. // Created by 施书顺 on 2021/3/5.
  6. //
  7. import UIKit
  8. import SnapKit
  9. class DiscoverButtonsView: UIView {
  10. let buttonScrollView = UIButtonScrollView()
  11. var buttons = [UIButton]()
  12. init(){
  13. super.init(frame: CGRect.zero)
  14. addSubview(buttonScrollView)
  15. buttonScrollView.snp.makeConstraints{ make in
  16. make.centerX.equalToSuperview()
  17. make.width.equalTo(UIScreen.main.bounds.width)
  18. make.height.equalTo(80)
  19. make.top.equalTo(0)
  20. }
  21. buttonScrollView.canCancelContentTouches = true
  22. buttonScrollView.bounces = true
  23. buttonScrollView.showsHorizontalScrollIndicator = false
  24. buttonScrollView.showsVerticalScrollIndicator = false
  25. buttonScrollView.contentSize = CGSize(width: 70 * 7, height: 80)
  26. addButtonToScrollView(imageName: "calendar", title: "每日推荐")
  27. addButtonToScrollView(imageName: "fm", title: "私人FM")
  28. addButtonToScrollView(imageName: "songlist", title: "歌单")
  29. addButtonToScrollView(imageName: "ranklist", title: "排行榜")
  30. addButtonToScrollView(imageName: "album", title: "数字专辑")
  31. addButtonToScrollView(imageName: "singroom", title: "歌房")
  32. addButtonToScrollView(imageName: "gaming", title: "游戏专区")
  33. }
  34. required init?(coder: NSCoder) {
  35. fatalError("init(coder:) has not been implemented")
  36. }
  37. func addButtonToScrollView(imageName: String, title: String){
  38. let button = UIButton()
  39. button.setImage(UIImage(named: imageName), for: .normal)
  40. button.adjustsImageWhenHighlighted = false
  41. buttonScrollView.addSubview(button)
  42. button.snp.makeConstraints{ make in
  43. if(buttons.isEmpty){
  44. make.left.equalTo(20)
  45. }else{
  46. make.left.equalTo(buttons[buttons.count - 1].snp.right).offset(20)
  47. }
  48. make.top.equalTo(0)
  49. make.width.equalTo(50)
  50. make.height.equalTo(50)
  51. }
  52. buttons.append(button)
  53. let titleLabel = UILabel()
  54. titleLabel.text = title
  55. titleLabel.font = UIFont.systemFont(ofSize: 12)
  56. buttonScrollView.addSubview(titleLabel)
  57. titleLabel.snp.makeConstraints{ make in
  58. make.centerX.equalTo(button.snp.centerX)
  59. make.top.equalTo(button.snp.bottom).offset(10)
  60. }
  61. }
  62. }