Skip to content

zrcoder/SwipeableCards

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SwipeableCards

A container of views (like cards) can be dragged!

视图容器,视图以卡片形式层叠放置,可滑动。

There are only visible cards in memory, after you drag and removed the top one, it will be reused as the last one.
内存中只会生成可见的卡片,顶部的卡片被划走之后,会作为最后一张卡片循环利用。

You can find an Objective-C version here:
你可以在这里找到Objective-C版:iCards

这里有一篇:《探索之旅:代理原理》,可作为SwipeableCards的详细说明文档。

SwipeableCards SwipeableCards SwipeableCards

Usage:

Here is an example:
用法示例:

    @IBOutlet weak var cards: SwipeableCards!
    var cardsData = [Int]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        makeCardsData()
        cards.dataSource = self
        cards.delegate = self
    }
    
    func makeCardsData() {
        for i in 0..<100 {
            cardsData.append(i)
        }
    }
    
    // SwipeableCardsDataSource methods
    func numberOfTotalCards(in cards: SwipeableCards) -> Int {
        return cardsData.count
    }
    func view(for cards: SwipeableCards, index: Int, reusingView: UIView?) -> UIView {
        var label: UILabel? = reusingView as? UILabel
        if label == nil {
            let labelFrame = CGRect(x: 0, y: 0, width: cardsWidth.constant - 30, height: cardsHeight.constant - 20)
            label = UILabel(frame: labelFrame)
            label!.textAlignment = .center
            label!.layer.cornerRadius = 5
        }
        label!.text = String(cardsData[index])
        label!.layer.backgroundColor = Color.random.cgColor
        return label!
    }
    
    // SwipeableCardsDelegate methods
    func cards(_ cards: SwipeableCards, beforeSwipingItemAt index: Int) {
        print("Begin swiping card \(index)!")
    }
    func cards(_ cards: SwipeableCards, didLeftRemovedItemAt index: Int) {
        print("<--\(index)")
    }
    func cards(_ cards: SwipeableCards, didRightRemovedItemAt index: Int) {
        print("\(index)-->")
    }
    func cards(_ cards: SwipeableCards, didRemovedItemAt index: Int) {
        print("index of removed card:\(index)")
    }

About

A container of views (like cards) can be dragged!

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages