Visual_Novel_iOS/crush/Crush/Src/Modules/Home/CardDrag/MeetDragCardExample.swift

288 lines
9.7 KiB
Swift

//
// YFLDragCardExample.swift
// Crush
//
// Created by AI Assistant on 2024/12/19.
// Copyright © 2024 Crush. All rights reserved.
//
import UIKit
import SnapKit
// MARK: - 使
class MeetDragCardExampleViewController: UIViewController {
private var dragCardContainer: MeetDragCardContainer!
private var dataArray: [String] = ["卡片1", "卡片2", "卡片3", "卡片4", "卡片5"]
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
setupDragCardContainer()
}
private func setupUI() {
view.backgroundColor = UIColor.white
//
let lr = 12.0
let cardWidth = UIScreen.width - lr * 2.0
let cardHeight = 600.0 // 400
dragCardContainer = MeetDragCardContainer(frame: CGRect(x: 0, y: 0, width: cardWidth, height: cardHeight))
view.addSubview(dragCardContainer)
dragCardContainer.snp.makeConstraints { make in
make.center.equalToSuperview()
make.width.equalTo(cardWidth)
make.height.equalTo(cardHeight)
}
//
dragCardContainer.dataSource = self
dragCardContainer.delegate = self
//
setupControlButtons()
}
private func setupControlButtons() {
let leftButton = UIButton(type: .system)
leftButton.setTitle("左滑", for: .normal)
leftButton.backgroundColor = UIColor.red
leftButton.setTitleColor(.white, for: .normal)
leftButton.addTarget(self, action: #selector(leftButtonTapped), for: .touchUpInside)
view.addSubview(leftButton)
let rightButton = UIButton(type: .system)
rightButton.setTitle("右滑", for: .normal)
rightButton.backgroundColor = UIColor.green
rightButton.setTitleColor(.white, for: .normal)
rightButton.addTarget(self, action: #selector(rightButtonTapped), for: .touchUpInside)
view.addSubview(rightButton)
let reloadButton = UIButton(type: .system)
reloadButton.setTitle("重新加载", for: .normal)
reloadButton.backgroundColor = UIColor.blue
reloadButton.setTitleColor(.white, for: .normal)
reloadButton.addTarget(self, action: #selector(reloadButtonTapped), for: .touchUpInside)
view.addSubview(reloadButton)
leftButton.snp.makeConstraints { make in
make.bottom.equalTo(view.safeAreaLayoutGuide).offset(-20)
make.left.equalToSuperview().offset(20)
make.width.equalTo(80)
make.height.equalTo(40)
}
rightButton.snp.makeConstraints { make in
make.bottom.equalTo(view.safeAreaLayoutGuide).offset(-20)
make.centerX.equalToSuperview()
make.width.equalTo(80)
make.height.equalTo(40)
}
reloadButton.snp.makeConstraints { make in
make.bottom.equalTo(view.safeAreaLayoutGuide).offset(-20)
make.right.equalToSuperview().offset(-20)
make.width.equalTo(80)
make.height.equalTo(40)
}
}
private func setupDragCardContainer() {
//
dragCardContainer.reloadData()
}
@objc private func leftButtonTapped() {
dragCardContainer.removeCardViewForDirection(.left)
}
@objc private func rightButtonTapped() {
dragCardContainer.removeCardViewForDirection(.right)
}
@objc private func reloadButtonTapped() {
dragCardContainer.reloadData()
}
}
// MARK: - MeetDragCardContainerDataSource
extension MeetDragCardExampleViewController: MeetDragCardContainerDataSource {
func numberOfRowsInYFLDragCardContainer(_ container: MeetDragCardContainer) -> Int {
return dataArray.count
}
func container(_ container: MeetDragCardContainer, viewForRowsAt index: Int) -> MeetDragCardView {
let cardView = CustomDragCardView()
cardView.configure(with: dataArray[index])
return cardView
}
}
// MARK: - MeetDragCardContainerDelegate
extension MeetDragCardExampleViewController: MeetDragCardContainerDelegate {
func container(_ container: MeetDragCardContainer, didSelectRowAt index: Int) {
print("点击了卡片: \(index)")
}
func container(_ container: MeetDragCardContainer, dataSourceIsEmpty isEmpty: Bool) {
if isEmpty {
print("数据源为空,可以加载更多数据")
//
loadMoreData()
}
}
func container(_ container: MeetDragCardContainer, canDragForCardView cardView: MeetDragCardView) -> Bool {
return true
}
func container(_ container: MeetDragCardContainer, dargingForCardView cardView: MeetDragCardView, direction: ContainerDragDirection, widthRate: CGFloat, heightRate: CGFloat) {
//
if direction == .left {
container.showNopeLogo(true, widthRate: CGFloat(abs(widthRate)))
} else if direction == .right {
container.showLikeLogo(true, widthRate: CGFloat(abs(widthRate)))
} else {
container.showNopeLogo(false, widthRate: 0)
container.showLikeLogo(false, widthRate: 0)
}
}
func container(_ container: MeetDragCardContainer, canDragFinishForDirection direction: ContainerDragDirection, forCardView cardView: MeetDragCardView) -> Bool {
return true
}
func container(_ container: MeetDragCardContainer, dragDidFinshForDirection direction: ContainerDragDirection, forCardView cardView: MeetDragCardView) {
print("卡片拖拽完成,方向: \(direction)")
//
switch direction {
case .left:
// container.showNopeLottie()
break
case .right:
// container.showLikeLottie()
break
default:
break
}
}
func container(_ container: MeetDragCardContainer, lookingBack direction: ContainerDragDirection, forCardView cardView: MeetDragCardView) {
print("卡片回看,方向: \(direction)")
}
func container(_ container: MeetDragCardContainer, enterSmallCardMode smallCardMode: Bool, forCardView cardView: MeetDragCardView) {
print("进入小卡片模式: \(smallCardMode)")
}
private func loadMoreData() {
//
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
self.dataArray.append(contentsOf: ["新卡片1", "新卡片2", "新卡片3"])
self.dragCardContainer.reloadData()
}
}
}
// MARK: -
class CustomDragCardView: MeetDragCardView {
private let titleLabel = UILabel()
private let contentLabel = UILabel()
private let imageView = UIImageView()
override init(frame: CGRect) {
super.init(frame: frame)
setupSubviews()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setupSubviews()
}
private func setupSubviews() {
backgroundColor = UIColor.systemBlue
//
layer.cornerRadius = 12
layer.shadowColor = UIColor.black.cgColor
layer.shadowOffset = CGSize(width: 0, height: 2)
layer.shadowRadius = 4
layer.shadowOpacity = 0.1
//
addSubview(imageView)
addSubview(titleLabel)
addSubview(contentLabel)
//
imageView.backgroundColor = UIColor.lightGray
imageView.contentMode = .scaleAspectFill
imageView.layer.cornerRadius = 8
imageView.clipsToBounds = true
titleLabel.font = UIFont.boldSystemFont(ofSize: 18)
titleLabel.textColor = .white
titleLabel.textAlignment = .center
contentLabel.font = UIFont.systemFont(ofSize: 14)
contentLabel.textColor = .white
contentLabel.textAlignment = .center
contentLabel.numberOfLines = 0
// 使SnapKit
imageView.snp.makeConstraints { make in
make.top.equalToSuperview().offset(20)
make.centerX.equalToSuperview()
make.width.height.equalTo(100)
}
titleLabel.snp.makeConstraints { make in
make.top.equalTo(imageView.snp.bottom).offset(20)
make.left.right.equalToSuperview().inset(20)
}
contentLabel.snp.makeConstraints { make in
make.top.equalTo(titleLabel.snp.bottom).offset(10)
make.left.right.equalToSuperview().inset(20)
make.bottom.lessThanOrEqualToSuperview().offset(-20)
}
}
func configure(with text: String) {
titleLabel.text = text
contentLabel.text = "这是 \(text) 的详细内容描述"
}
override func YFLDragCardViewLayoutSubviews() {
super.YFLDragCardViewLayoutSubviews()
//
}
override func startAnimatingForDirection(_ direction: ContainerDragDirection) {
super.startAnimatingForDirection(direction)
//
switch direction {
case .left:
//
UIView.animate(withDuration: 0.3) {
self.transform = self.transform.rotated(by: -CGFloat.pi / 6)
self.alpha = 0.7
}
case .right:
//
UIView.animate(withDuration: 0.3) {
self.transform = self.transform.rotated(by: CGFloat.pi / 6)
self.alpha = 0.7
}
default:
break
}
}
}