仿<探探> 卡片滑动

效果图

cardView.gif

主要思路

容器View类:cardView
item卡片类:cardItemView

cardView

@protocol cardViewDelegate 
//点击响应
-(void)cardView:(cardView *)cardView viewclickedWithIndex:(NSInteger)index;
//左右滑动删除
-(void)cardView:(cardView *)cardView viewdeletWithisLeft:(BOOL)isleft WithIndex:(NSInteger)index;
@end

@protocol cardViewDataSource 
@required
//卡片数
- (NSInteger)numberOfCardView:(cardView *)cardView;
//item卡片创建
- (cardItemView *)cardView:(cardView *)cardView itemViewAtIndex:(NSInteger)index;
//重新刷新卡片
- (void)cardViewNeedMoreData:(cardView *)cardView;

@end

创建卡片主要代码

 if (_dataSource == nil) {
        return ;
    }
    //1.移除
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    //2.添加
    
    itemCount = [self numberOfDataSource];
    
    for (int i=0; i

cardItemView 拖动手势主要代码

 if (pan.state == UIGestureRecognizerStateChanged) {
        
        CGPoint  translate  =   [pan translationInView:self];
        
        if (translate.x<-1) {
            isleft = YES;
        }
        self.center =CGPointMake(self.center.x+translate.x, self.center.y+translate.y);
        
        CGFloat angle = (self.center.x - self.frame.size.width / 2.0) / self.frame.size.width / 4.0;
        
        self.transform = CGAffineTransformMakeRotation(angle);
        
        [pan setTranslation:CGPointZero inView:self];
        
    }else if (pan.state == UIGestureRecognizerStateEnded) {
        
        CGPoint velocity = [pan velocityInView:self];
        
        if (velocity.x>800 ||velocity.x<-800) {
            
            [self removeWithLeft:isleft];
            return;
        }
        if (self.center.x<10||self.center.x>Width-10) {
            
             [self removeWithLeft:isleft];
            return;
        }else{
            [UIView animateWithDuration:0.5 animations:^{
                self.center = _originalCenter;
                self.transform = CGAffineTransformMakeRotation(0);
            }];
        }
        
    }

本文参照Charles,仅供学习使用

你可能感兴趣的:(仿<探探> 卡片滑动)