// // DiscoverRecommendView.swift // wyy // // Created by 施书顺 on 2021/3/5. // import UIKit class DiscoverRecommendView: UIView { let recommendLabel = UILabel() let moreLabel = UILabel() let scrollView = UIButtonScrollView() var albumButtons = [AlbumButton]() struct AlbumData { var imageName: String var title: String var count: Int } var data = [AlbumData(imageName: "album1", title: "这是一个歌单标题", count: 1234), AlbumData(imageName: "album2", title: "歌单标题", count: 100), AlbumData(imageName: "album3", title: "我是一个歌单~~~~~", count: 99999), AlbumData(imageName: "album1", title: "这是一个歌单标题", count: 1234), AlbumData(imageName: "album2", title: "歌单标题", count: 100), AlbumData(imageName: "album3", title: "我是一个歌单~~~~~", count: 99999), AlbumData(imageName: "album3", title: "我是一个歌单~~~~~", count: 99999),] init(){ super.init(frame: CGRect.zero) recommendLabel.text = "推荐歌单" recommendLabel.font = UIFont.systemFont(ofSize: 20, weight: .bold) addSubview(recommendLabel) recommendLabel.snp.makeConstraints{ make in make.top.equalTo(5) make.left.equalTo(20) } moreLabel.text = " 更多 >" moreLabel.font = UIFont.systemFont(ofSize: 15) moreLabel.textAlignment = .center moreLabel.layer.borderColor = UIColor.gray.withAlphaComponent(0.5).cgColor moreLabel.layer.borderWidth = 1.0 moreLabel.layer.cornerRadius = 10 addSubview(moreLabel) moreLabel.snp.makeConstraints{ make in make.top.equalTo(0) make.right.equalTo(-10) make.width.equalTo(60) make.height.equalTo(30) } scrollView.canCancelContentTouches = true scrollView.bounces = true scrollView.showsHorizontalScrollIndicator = false scrollView.showsVerticalScrollIndicator = false // 20 * 2 + 100 * 6 + 15 * 4 scrollView.contentSize = CGSize(width: 20 * 2 + 100 * data.count + 15 * 4, height: 120) addSubview(scrollView) scrollView.snp.makeConstraints{ make in make.top.equalTo(recommendLabel.snp.bottom).offset(10) make.width.equalToSuperview() make.height.equalTo(140) } for item in data { let alb = AlbumButton(imageName: item.imageName, title: item.title, count: item.count) scrollView.addSubview(alb) alb.snp.makeConstraints{ make in if albumButtons.isEmpty { make.left.equalTo(20) } else { make.left.equalTo(albumButtons.last!.snp.right).offset(15) } make.top.equalTo(0) } albumButtons.append(alb) } } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } }