zrcoder / iCards

A container of views like cards can be dragged!

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

iCards

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 a Swift version here:
你可以在这里找到Swift版:SwipeableCards

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

pod surpported:
支持pod :

target ‘xxx’ do
pod ‘iCards’
end

iCards iCards iCards

Usage:

Here is an example:
用法示例:

#import "iCards.h"
@property (weak, nonatomic) IBOutlet iCards *cards;
@property (nonatomic, strong) NSMutableArray *cardsData;
- (void)viewDidLoad {
    [super viewDidLoad];
    [self makeCardsData];
    self.cards.dataSource = self;
    self.cards.delegate = self;
}
- (void)makeCardsData {
    for (int i=0; i<100; i++) {
        [self.cardsData addObject:@(i)];
    }
}
- (NSMutableArray *)cardsData {
    if (_cardsData == nil) {
        _cardsData = [NSMutableArray array];
    }
    return _cardsData;
}

#pragma mark - iCardsDataSource methods

- (NSInteger)numberOfItemsInCards:(iCards *)cards {
    return self.cardsData.count;
}

- (UIView *)cards:(iCards *)cards viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view {
    UILabel *label = (UILabel *)view;
    if (label == nil) {
        CGSize size = cards.frame.size;
        CGRect labelFrame = CGRectMake(0, 0, size.width - 30, size.height - 20);
        label = [[UILabel alloc] initWithFrame:labelFrame];
        label.textAlignment = NSTextAlignmentCenter;
        label.layer.cornerRadius = 5;
    }
    label.text = [self.cardsData[index] stringValue];
    label.layer.backgroundColor = [Color randomColor].CGColor;
    return label;
}

#pragma mark - iCardsDelegate methods

- (void)cards:(iCards *)cards beforeSwipingItemAtIndex:(NSInteger)index {
    NSLog(@"Begin swiping card %ld!", index);
}

- (void)cards:(iCards *)cards didLeftRemovedItemAtIndex:(NSInteger)index {
    NSLog(@"<--%ld", index);
}

- (void)cards:(iCards *)cards didRightRemovedItemAtIndex:(NSInteger)index {
    NSLog(@"%ld-->", index);
}

- (void)cards:(iCards *)cards didRemovedItemAtIndex:(NSInteger)index {
    NSLog(@"index of removed card: %ld", index);
}

About

A container of views like cards can be dragged!

License:MIT License


Languages

Language:Objective-C 97.3%Language:Ruby 2.7%