仿<探探> 卡片滑动

效果图

cardView.gif

主要思路

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

cardView

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

@protocol cardViewDataSource <NSObject>
@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<itemCount; i++) {
        cardItemView * itemView = [self itemViewAtIndex:i];
        itemView.delegate =self;
        itemView.tag =i;
        itemView.bounds =CGRectZero;
        int x = arc4random() % 10;
        int y = arc4random() % 10;
        [UIView animateWithDuration:0.2 delay:i*0.05 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            
            itemView.frame =CGRectMake(x,y, self.bounds.size.width, self.bounds.size.height);
        } completion:^(BOOL finished) {
        
        }];
        [self addSubview:itemView];
    }

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,仅供学习使用

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容