// // 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 } } }