主要是采用类似
UITableView
的delegate
与dataSource
思维来构造,同时参考别人Dome并进行改进完善
使用方法
// 懒加载 并设置代理数据源
- (HWDragCardView *)contentView {
if (!_contentView) {
CGFloat w = WidthScale(326);
CGFloat h = HeightScale(439);
CGFloat x = (CCWidth-w)/2;
_contentView = [[HWDragCardView alloc] initWithFrame:CGRectMake(x, 80, w, h) style:HWDragStyleUpOverlay];
_contentView.delegate = self;
_contentView.dataSource = self;
_contentView.remindNumber = 1; // 设置剩余提示数量
}
return _contentView;
}
数据源方法
注意:自定义
cell
必须继承HWDragCardCell
- (NSInteger)numberOfIndragCardView:(HWDragCardView *)dragCardView {
return self.dataSources.count;
}
- (HWDragCardCell *)dragCardView:(HWDragCardView *)dragCardView cellForRowAtIndex:(NSInteger)index {
FateCell *cell = [FateCell hw_loadViewFromNib];
NSString *imageName = self.dataSources[index];
cell.type = FateCellTypeDefault;
cell.imageName = imageName;
cell.seeClickBlcok = ^{
NSLog(@"点击了查看详情");
};
return cell;
}
代理方法
// 拖拽中
- (void)dragCardView:(HWDragCardView *)dragCardView dragDropDirection:(HWDragDropDirection)dragDropDirection widthRatio:(CGFloat)widthRatio heightRatio:(CGFloat)heightRatio currentCell:(id)currentCell {
FateCell *cell = (FateCell *)currentCell;
CGFloat scale = 1 + ((kBoundaryRatio > fabs(widthRatio) ? fabs(widthRatio) : kBoundaryRatio)) / 4;
FateCellType type = FateCellTypeDefault;
switch (dragDropDirection) {
case HWDragDropDirectionLeft:{
self.leftButton.transform = CGAffineTransformMakeScale(scale, scale);
// NSLog(@"<<<<<======滑");
type = FateCellTypeDislike;
}break;
case HWDragDropDirectionRight:{
self.rightButton.transform = CGAffineTransformMakeScale(scale, scale);
// NSLog(@"======>>>>滑");
type = FateCellTypeLike;
}break;
default:{
type = FateCellTypeDefault;
self.leftButton.transform = CGAffineTransformMakeScale(1, 1);
self.rightButton.transform = CGAffineTransformMakeScale(1, 1);
}break;
}
cell.type = type;
}
// 拖拽结束
- (void)dragCardView:(HWDragCardView *)dragCardView dragDropDirection:(HWDragDropDirection)dragDropDirection dragEndWithIndex:(NSInteger)index {
switch (dragDropDirection) {
case HWDragDropDirectionLeft:{
NSLog(@"选择了不喜欢");
}break;
case HWDragDropDirectionRight:{
NSLog(@"选择了喜欢");
}break;
default:{
NSLog(@"啥都没干");
}break;
}
}
// 剩余数量提醒
- (void)residualQuantityReminder:(NSInteger)remindNumber {
if (remindNumber == 1) {
NSLog(@"请求数据了");
} else if (remindNumber == 0) {
NSLog(@"切换下一组了");
[self loadData];
}
}