Code Monkey home page Code Monkey logo

2jycardview's Introduction

JYCardView

  • Using UICollectionView to implement the card stack layout.

  • 使用UICollectionView实现的轻量级卡牌堆叠式布局。

image

Usage

import UIKit

class ViewController: UIViewController {

    private var dataSource = createDataSource()
    
    private var cardView: JYCardView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .lightGray
        
        cardView = JYCardView(frame: CGRect(origin: .zero, size: CGSize(width: 280, height: 380)))
        cardView.center = CGPoint(x: view.bounds.width * 0.5, y: view.bounds.height * 0.5)
        cardView.register(cellClass: CardViewCell.self, forCellWithReuseIdentifier: CardViewCell.cellID)
        cardView.cardViewDataSource = self
        cardView.backgroundColor = view.backgroundColor
        view.addSubview(cardView)
    }
    
    static func createDataSource() -> [CardViewModel] {
        return [
            CardViewModel(imageURLString: "https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcSlV31scIL9wgD5QF390sm8LZ7o-C6ZGfdUPA&usqp=CAU", title: "彭于晏"),
            CardViewModel(imageURLString: "https://imgs.jushuo.com/album/2018-09-27/5bac2fa689055.com/large/pgc-image/1537954274800026ad2a9c8", title: "张馨予"),
            CardViewModel(imageURLString: "https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcQq0BYYn6kuYmDMK46tMtTGEYeXDMb74K2kdQ&usqp=CAU", title: "柳岩"),
            CardViewModel(imageURLString: "https://p1.meituan.net/movie/bfdf65acdf8cca2e4a832888a6da594d157051.jpg@750w_680h_1e_1c", title: "刘德华"),
            CardViewModel(imageURLString: "https://5b0988e595225.cdn.sohucs.com/images/20191031/f6b1282ab0fb407689461b8875557db0.jpeg", title: "吴彦祖"),
            CardViewModel(imageURLString: "https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcS5BjeIcnfYJn-Y4KnzR1of-w0hGZx3D2BdWg&usqp=CAU", title: "吴京")
        ]
    }
}

extension ViewController: JYCardViewDataSource {
    func numberOfItems(in cardView: JYCardView) -> Int {
        dataSource.count
    }
    
    func cardView(cardView: JYCardView, cellForItemAt index: Int) -> UICollectionViewCell {
        let cell = cardView.dequeueReusableCell(withReuseIdentifier: CardViewCell.cellID, forIndex: index) as! CardViewCell
        cell.cardViewModel = dataSource[index]
        return cell
    }
    
    func cardView(cardView: JYCardView, didRemoveCell cell: UICollectionViewCell, updateCallback:((Bool)->Void)?) {
        dataSource.removeFirst()
        
        cardView.performBatchUpdates(updates: {
            cardView.collectionView.deleteItems(at: [IndexPath(item: 0, section: 0)])
        }, completion: {
            if self.dataSource.count < 2 { // 加载更多数据
                
                let newModels = ViewController.createDataSource()
                let indexes: [IndexPath] = newModels.indices.map {
                    IndexPath(item: $0+self.dataSource.count, section: 0)
                }
                
                self.dataSource.append(contentsOf: newModels)
                cardView.insertCells(atIndexPath: indexes)
            }
            updateCallback?($0)
        })
    }
}

2jycardview's People

Contributors

coderkingxy avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.