SynthReel/SynthReel/Class/Home/V/SRHomeBannerView.swift
2025-11-20 16:59:32 +08:00

178 lines
5.8 KiB
Swift

//
// SRHomeBannerView.swift
// SynthReel
//
// Created by on 2025/11/15.
// Copyright © 2025 SR. All rights reserved.
//
import UIKit
import SnapKit
import FSPagerView
class SRHomeBannerView: UIView {
var dataArr: [SRShortModel]? {
didSet {
self.bannerView.reloadData()
self.miniCollectionView.reloadData()
}
}
var didSelectedShort: ((_ model: SRShortModel?) -> Void)?
lazy var bgImageView = UIImageView(image: UIImage(named: "home_banner_bg"))
lazy var bannerView: FSPagerView = {
let view = FSPagerView()
view.delegate = self
view.dataSource = self
view.isInfinite = true
view.automaticSlidingInterval = 5
view.register(SRHomeBannerCell.self, forCellWithReuseIdentifier: "cell")
return view
}()
// lazy var miniBgView: UIView = {
// let view = UIView()
// view.layer.cornerRadius = 4
// view.layer.masksToBounds = true
// view.backgroundColor = ._051_B_22.withAlphaComponent(0.5)
// return view
// }()
lazy var miniCollectionViewLayout: UICollectionViewFlowLayout = {
let layout = UICollectionViewFlowLayout()
layout.itemSize = .init(width: 36, height: 46)
layout.minimumLineSpacing = 4
return layout
}()
lazy var miniCollectionView: SRCollectionView = {
let collectionView = SRCollectionView(frame: .zero, collectionViewLayout: miniCollectionViewLayout)
collectionView.layer.cornerRadius = 4
collectionView.layer.masksToBounds = true
collectionView.backgroundColor = ._051_B_22.withAlphaComponent(0.5)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.showsVerticalScrollIndicator = false
collectionView.contentInset = .init(top: 5, left: 0, bottom: 5, right: 0)
collectionView.register(SRHomeBannerMiniCell.self, forCellWithReuseIdentifier: "cell")
return collectionView
}()
override init(frame: CGRect) {
super.init(frame: frame)
sr_setupUI()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
applyHeteromorphicLayer()
}
///
func applyHeteromorphicLayer() {
let viewSize = bannerView.bounds.size
let path = UIBezierPath()
let layer = CAShapeLayer()
//
let topRight = UIScreen.getRatioWidth(size: 25)
//
let bottomLeft = UIScreen.getRatioWidth(size: 10)
path.move(to: .init(x: 0, y: 0))
path.addLine(to: .init(x: viewSize.width - topRight, y: 0))
path.addLine(to: .init(x: viewSize.width, y: topRight))
path.addLine(to: .init(x: viewSize.width, y: viewSize.height))
path.addLine(to: .init(x: bottomLeft, y: viewSize.height))
path.addLine(to: .init(x: 0, y: viewSize.height - bottomLeft))
path.close()
layer.path = path.cgPath
bannerView.layer.mask = layer
}
}
extension SRHomeBannerView {
func sr_setupUI() {
let bgImage = self.bgImageView.image!
let bgWidth = UIScreen.width - 30
let bgHeight = bgImage.size.height / bgImage.size.width * bgWidth
addSubview(bgImageView)
addSubview(bannerView)
addSubview(miniCollectionView)
bgImageView.snp.makeConstraints { make in
make.left.equalToSuperview().offset(15)
make.right.equalToSuperview().offset(-15)
make.top.equalToSuperview()
make.bottom.equalToSuperview()
make.height.equalTo(bgHeight)
}
bannerView.snp.makeConstraints { make in
make.left.equalTo(bgImageView).offset(11)
make.right.equalTo(bgImageView).offset(-11)
make.top.equalTo(bgImageView).offset(18)
make.bottom.equalTo(bgImageView).offset(-12)
}
miniCollectionView.snp.makeConstraints { make in
make.left.equalTo(bannerView).offset(7)
make.centerY.equalTo(bannerView)
make.top.equalTo(bannerView).offset(7)
make.width.equalTo(46)
}
}
}
//MARK: FSPagerViewDelegate FSPagerViewDataSource
extension SRHomeBannerView: FSPagerViewDelegate, FSPagerViewDataSource {
func numberOfItems(in pagerView: FSPagerView) -> Int {
return self.dataArr?.count ?? 0
}
func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "cell", at: index) as! SRHomeBannerCell
cell.model = self.dataArr?[index]
return cell
}
func pagerView(_ pagerView: FSPagerView, didSelectItemAt index: Int) {
self.didSelectedShort?(self.dataArr?[index])
}
}
//MARK: UICollectionViewDelegate UICollectionViewDataSource
extension SRHomeBannerView: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! SRHomeBannerMiniCell
cell.model = self.dataArr?[indexPath.row]
return cell
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.dataArr?.count ?? 0
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if indexPath.row == self.bannerView.currentIndex { return }
self.bannerView.selectItem(at: indexPath.row, animated: true)
}
}