Netease-Music-Demo/wyy/DiscoverView/DiscoverButtonsView.swift
2021-03-06 18:45:01 +08:00

72 lines
2.4 KiB
Swift

//
// DiscoverButtonsView.swift
// wyy
//
// Created by on 2021/3/5.
//
import UIKit
import SnapKit
class DiscoverButtonsView: UIView {
let buttonScrollView = UIButtonScrollView()
var buttons = [UIButton]()
init(){
super.init(frame: CGRect.zero)
addSubview(buttonScrollView)
buttonScrollView.snp.makeConstraints{ make in
make.centerX.equalToSuperview()
make.width.equalTo(UIScreen.main.bounds.width)
make.height.equalTo(80)
make.top.equalTo(0)
}
buttonScrollView.canCancelContentTouches = true
buttonScrollView.bounces = true
buttonScrollView.showsHorizontalScrollIndicator = false
buttonScrollView.showsVerticalScrollIndicator = false
buttonScrollView.contentSize = CGSize(width: 70 * 7, height: 80)
addButtonToScrollView(imageName: "calendar", title: "每日推荐")
addButtonToScrollView(imageName: "fm", title: "私人FM")
addButtonToScrollView(imageName: "songlist", title: "歌单")
addButtonToScrollView(imageName: "ranklist", title: "排行榜")
addButtonToScrollView(imageName: "album", title: "数字专辑")
addButtonToScrollView(imageName: "singroom", title: "歌房")
addButtonToScrollView(imageName: "gaming", title: "游戏专区")
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func addButtonToScrollView(imageName: String, title: String){
let button = UIButton()
button.setImage(UIImage(named: imageName), for: .normal)
button.adjustsImageWhenHighlighted = false
buttonScrollView.addSubview(button)
button.snp.makeConstraints{ make in
if(buttons.isEmpty){
make.left.equalTo(20)
}else{
make.left.equalTo(buttons[buttons.count - 1].snp.right).offset(20)
}
make.top.equalTo(0)
make.width.equalTo(50)
make.height.equalTo(50)
}
buttons.append(button)
let titleLabel = UILabel()
titleLabel.text = title
titleLabel.font = UIFont.systemFont(ofSize: 12)
buttonScrollView.addSubview(titleLabel)
titleLabel.snp.makeConstraints{ make in
make.centerX.equalTo(button.snp.centerX)
make.top.equalTo(button.snp.bottom).offset(10)
}
}
}