Files
yingmeng-ios-switf/yinmeng-ios/Modules/Home/View/HomeVoiceFollowView.swift
2024-04-03 20:06:02 +08:00

152 lines
5.6 KiB
Swift

//
// HomeVoiceFollowView.swift
// yinmeng-ios
//
// Created by yinmeng on 2024/2/29.
//
import UIKit
typealias ClickAvatarBlcok = (_ model:HomeVoiceModel)->Void
class HomeVoiceFollowView: UIView {
var clickAvatarBlcok:ClickAvatarBlcok?
override init(frame: CGRect) {
super.init(frame: frame)
setUILayout()
}
func setUILayout(){
addSubview(unfoldBtnView)
addSubview(backdropView)
backdropView.addSubview(titleView)
backdropView.addSubview(collectionView)
unfoldBtnView.snp.makeConstraints { make in
make.top.bottom.left.equalTo(self)
make.width.equalTo(44)
}
backdropView.snp.makeConstraints { make in
make.left.equalTo(44)
make.width.equalTo(186)
make.top.bottom.equalTo(self)
}
titleView.snp.makeConstraints { make in
make.top.equalTo(20)
make.left.equalTo(0)
make.right.equalTo(self.backdropView)
make.height.equalTo(22)
}
backdropView.isHidden = true
collectionView.snp.makeConstraints { make in
make.top.equalTo(self.titleView.snp.bottom).offset(20)
make.left.equalTo(0)
make.width.equalTo(186)
make.bottom.equalTo(self.backdropView)
}
collectionView.delegate = self
collectionView.dataSource = self
let layer = CAShapeLayer()
let path = UIBezierPath.init(roundedRect: CGRect(x: 0, y: 0, width: 44, height: 418), byRoundingCorners: [.topRight , .bottomRight] , cornerRadii: CGSize(width: 24, height: 24))
layer.path = path.cgPath
unfoldBtnView.layer.mask = layer
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@objc func unfoldBtnAction(){
unfoldBtnView.isSelected = !unfoldBtnView.isSelected
if unfoldBtnView.isSelected {
self.backdropView.isHidden = false
let layer = CAShapeLayer()
let path = UIBezierPath.init(roundedRect: CGRect(x: 0, y: 0, width: 186, height: 418), byRoundingCorners: [.topRight , .bottomRight] , cornerRadii: CGSize(width: 24, height: 24))
layer.path = path.cgPath
unfoldBtnView.layer.mask = nil
backdropView.layer.mask = layer
UIView .animate(withDuration: 0.1) {
let x = ScreenWidth - 28 - 44 - 3 - 186
self.frame = CGRect(x:x , y: 3, width: 230, height: 418)
}
return
}
let layer = CAShapeLayer()
let path = UIBezierPath.init(roundedRect: CGRect(x: 0, y: 0, width: 44, height: 418), byRoundingCorners: [.topRight , .bottomRight] , cornerRadii: CGSize(width: 24, height: 24))
layer.path = path.cgPath
unfoldBtnView.layer.mask = layer
backdropView.layer.mask = nil
UIView .animate(withDuration: 0.1) {
let x = ScreenWidth - 28 - 44 - 3
self.frame = CGRect(x:x , y: 3, width: 44, height: 418)
}completion: { isC in
self.backdropView.isHidden = true
}
}
var titleView:UILabel = {
let _titleView = UILabel()
_titleView.text = "我关注的声音"
_titleView.font = UIFont.systemFont(ofSize: 16, weight: .semibold)
_titleView.textColor = .white
_titleView.textAlignment = .center
return _titleView
}()
var unfoldBtnView:UIButton = {
let _unfoldBtnView = UIButton()
_unfoldBtnView.backgroundColor = ThemeColor(hexStr: "#1D1F33")
_unfoldBtnView.setImage(UIImage(named: "yin_home_voice_unfold_arrow"), for: .normal)
_unfoldBtnView.setImage(UIImage(named: "yin_home_voice_unfold_rigth_arrow"), for: .selected)
_unfoldBtnView.addTarget(self, action: #selector(unfoldBtnAction), for: .touchUpInside)
return _unfoldBtnView
}()
var collectionView:UICollectionView = {
var layout = UICollectionViewFlowLayout()
layout.minimumLineSpacing = 16
layout.minimumInteritemSpacing = 10
layout.sectionInset = UIEdgeInsets(top: 0, left: 12, bottom:12, right: 12)
layout.itemSize = CGSize(width: 70, height: 69)
var _collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
_collectionView.register(HomeVoiceFollowCell.self, forCellWithReuseIdentifier: "HomeVoiceFollowCell")
return _collectionView
}()
var modelList:[HomeVoiceModel] = [] {
didSet{
self.collectionView.reloadData()
}
}
var backdropView:UIView = {
let _backdropView = UIView()
_backdropView.backgroundColor = ThemeColor(hexStr: "#050505")
return _backdropView
}()
}
extension HomeVoiceFollowView:UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return modelList.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HomeVoiceFollowCell", for: indexPath) as! HomeVoiceFollowCell
cell.model = modelList[indexPath.row]
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if let _clickAvatarBlcok = clickAvatarBlcok{
let model = modelList[indexPath.row]
_clickAvatarBlcok(model)
}
}
}