仿<探探> 卡片滑動

效果圖

cardView.gif

主要思路

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

cardView

@protocol cardViewDelegate <NSObject>
//點擊響應(yīng)
-(void)cardView:(cardView *)cardView viewclickedWithIndex:(NSInteger)index;
//左右滑動刪除
-(void)cardView:(cardView *)cardView viewdeletWithisLeft:(BOOL)isleft WithIndex:(NSInteger)index;
@end

@protocol cardViewDataSource <NSObject>
@required
//卡片數(shù)
- (NSInteger)numberOfCardView:(cardView *)cardView;
//item卡片創(chuàng)建
- (cardItemView *)cardView:(cardView *)cardView itemViewAtIndex:(NSInteger)index;
//重新刷新卡片
- (void)cardViewNeedMoreData:(cardView *)cardView;

@end

創(chuàng)建卡片主要代碼

 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,僅供學(xué)習(xí)使用

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容