ios 卡片橫向滑動(dòng)內(nèi)容切換效果

效果圖.gif

簡(jiǎn)介

  • 控件基于UIView封裝完成昌屉,采用UIPanGestureRecognizer監(jiān)聽自身的觸摸事件擂煞,以此處理各種滑動(dòng)動(dòng)畫操作贸宏。
  • 內(nèi)容之間可以循環(huán)切換,采用類似tableView加載機(jī)制家妆,達(dá)到復(fù)用效果。

思路設(shè)計(jì)

整體設(shè)計(jì)
  1. 繼承UIView創(chuàng)建一個(gè)容器SMSwipeView冕茅,用來(lái)放當(dāng)前顯示的cell伤极。
  2. SMSwipeView中實(shí)現(xiàn)滑動(dòng)事件的監(jiān)聽,以此來(lái)處理相應(yīng)的動(dòng)畫效果姨伤,以及cell的移除哨坪、緩存、載入操作姜挺。
  3. 創(chuàng)建一個(gè)Delegate齿税,用來(lái)獲取需要顯示的數(shù)據(jù)量彼硫,以及獲取顯示cell的對(duì)象炊豪。
循環(huán)設(shè)計(jì)

當(dāng)前下標(biāo)index假如為0

  1. 獲取當(dāng)前的三個(gè)要顯示的cell凌箕,便是0、1词渤、2三個(gè)cell牵舱,疊加顯示在當(dāng)前容器之中。
  2. 滑動(dòng)到下一個(gè)界面情況:index++變?yōu)?code>1缺虐,那么index=0cell則顯示在左側(cè)不可見的區(qū)域芜壁,加載index=3cell,如果此時(shí)超出范圍則加載index=0cell高氮。
  3. 手勢(shì)需要返回到上一頁(yè)的時(shí)候慧妄,就移除疊加最底層的cell,并載入上上一個(gè)cell剪芍,如果當(dāng)前的index=0塞淹,則上一個(gè)cell應(yīng)該為最后一個(gè)。
圖片.png

代碼實(shí)現(xiàn)

SMSwipeView.h

//定義協(xié)議
@protocol SMSwipeDelegate <NSObject>
@required
//獲取顯示數(shù)據(jù)內(nèi)容
-(UITableViewCell*)SMSwipeGetView:(SMSwipeView*)swipe withIndex:(int)index;
//獲取數(shù)據(jù)源總量
-(NSInteger)SMSwipeGetTotaleNum:(SMSwipeView*)swipe;
@end

@interface SMSwipeView : BaseView
@property(nonatomic,weak)id<SMSwipeDelegate> delegate;
-(void)reloadData;//加載方法
-(UITableViewCell*)dequeueReusableUIViewWithIdentifier:(NSString*)identifier;//根據(jù)id獲取緩存的cell
@end

SMSwipeView.m

//
//  SMSwipeView.m
//  Base
//
//  Created by whbt_mac on 15/12/28.
//  Copyright ? 2015年 StoneMover. All rights reserved.
//
//  淘寶滑動(dòng)特效效果
#import "SMSwipeView.h"

#define degreeTOradians(x) (M_PI * (x)/180)

const int LEFT_RIGHT_MARGIN=10;//childView距離父View左右的距離
const int TOP_MARGTIN=16;//當(dāng)前view距離父view的頂部的值

@interface SMSwipeView()

@property(nonatomic,weak)UITableViewCell * viewRemove;//已經(jīng)劃動(dòng)到邊界外的一個(gè)view

@property(nonatomic,strong)NSMutableArray * cacheViews;//放當(dāng)前顯示的子View的數(shù)組

@property(nonatomic,assign)int totalNum;//view總共的數(shù)量

@property(nonatomic,assign)int nowIndex;//當(dāng)前的下標(biāo)

@property(nonatomic,assign)CGPoint pointStart;//觸摸開始的坐標(biāo)

@property(nonatomic,assign)CGPoint pointLast;//上一次觸摸的坐標(biāo)

@property(nonatomic,assign)CGPoint pointEnd;//最后一次觸摸的坐標(biāo)

@property(nonatomic,weak)UITableViewCell * nowCell;//正在顯示的cell

@property(nonatomic,weak)UITableViewCell * nextCell;//下一個(gè)cell

@property(nonatomic,weak)UITableViewCell * thirdCell;//第三個(gè)cell

@property(nonatomic,assign)int w;//自身的寬度

@property(nonatomic,assign)int h;//自身的高度

@property(nonatomic,assign)BOOL isFirstLayoutSub;//是否是第一次執(zhí)行

@end

@implementation SMSwipeView

//從xib中加載該類
-(void)awakeFromNib{
    [super awakeFromNib];
    [self initSelf];
}
//直接用方法初始化
-(instancetype)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    [self initSelf];
    return self;
}

//進(jìn)行一些自身的初始化和設(shè)置
-(void)initSelf{
    self.clipsToBounds=YES;
    self.cacheViews=[[NSMutableArray alloc]init];
    UIPanGestureRecognizer * pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
    [self addGestureRecognizer:pan];
}

//布局subview的方法
-(void)layoutSubviews{
    if(!self.isFirstLayoutSub){
        self.isFirstLayoutSub=YES;
        self.w=self.bounds.size.width;
        self.h=self.bounds.size.height;
        [self reloadData];
    }
    
}

//重新加載數(shù)據(jù)方法罪裹,會(huì)再首次執(zhí)行l(wèi)ayoutSubviews的時(shí)候調(diào)用
-(void)reloadData{
    if (!self.delegate||![self.delegate respondsToSelector:@selector(SMSwipeGetView:withIndex:)]||![self.delegate respondsToSelector:@selector(SMSwipeGetTotaleNum:)]) {
        return;
    }
    self.totalNum=(int)[self.delegate SMSwipeGetTotaleNum:self];
    self.viewRemove=nil;
    UITableViewCell * nowCell=[self.delegate SMSwipeGetView:self withIndex:self.nowIndex];
    UITableViewCell * nextCell=[self.delegate SMSwipeGetView:self withIndex:self.nowIndex+1<self.totalNum?self.nowIndex+1:0];
    UITableViewCell * thirdCell=[self.delegate SMSwipeGetView:self withIndex:self.nowIndex+2<self.totalNum?self.nowIndex+2:self.nowIndex+2-self.totalNum];
    
    [thirdCell removeFromSuperview];
    thirdCell.layer.anchorPoint=CGPointMake(1, 1);
    thirdCell.frame=CGRectMake(LEFT_RIGHT_MARGIN*2, 0, self.w-2*2*LEFT_RIGHT_MARGIN, self.h-TOP_MARGTIN);
    [self addSubview:thirdCell];
    self.thirdCell=thirdCell;
    
    [nextCell removeFromSuperview];
    nextCell.layer.anchorPoint=CGPointMake(1, 1);
    nextCell.frame=CGRectMake(LEFT_RIGHT_MARGIN, TOP_MARGTIN/2*1, self.w-2*LEFT_RIGHT_MARGIN, self.h-TOP_MARGTIN);
    
    [self addSubview:nextCell];
    self.nextCell=nextCell;
    
    [nowCell removeFromSuperview];
    nowCell.layer.anchorPoint=CGPointMake(1, 1);
    nowCell.frame=CGRectMake(0, TOP_MARGTIN, self.w, self.h-TOP_MARGTIN);
    [self addSubview:nowCell];
    self.nowCell=nowCell;
   
    
}





#pragma mark swipe觸摸的相關(guān)手勢(shì)處理
-(void)swipe:(UISwipeGestureRecognizer*)sender{
    NSLog(@"swipe");
}

-(void)pan:(UIPanGestureRecognizer*)sender{
    CGPoint translation = [sender translationInView: self];
    //CGPoint speed=[sender velocityInView:self];//獲取速度
    if (sender.state==UIGestureRecognizerStateBegan) {
        //NSLog(@"begin");
        self.pointStart=translation;
        self.pointLast=translation;
    }
    
    if (sender.state==UIGestureRecognizerStateChanged) {
        //NSLog(@"change");
//        CGFloat xMove=translation.x-self.pointLast.x;
//        CGFloat yMove=translation.y-self.pointLast.y;
//        self.pointLast=translation;
//        
//        CGPoint center=self.nowCell.center;
//        self.nowCell.center=CGPointMake(center.x+xMove, center.y+yMove);
        
        CGFloat xTotalMove=translation.x-self.pointStart.x;
        if (xTotalMove<0) {
            self.nowCell.transform = CGAffineTransformMakeRotation(degreeTOradians(90*xTotalMove/self.w));
            self.nextCell.transform= CGAffineTransformMakeRotation(degreeTOradians(90*xTotalMove/self.w/2));
        }else{
            self.nowCell.transform = CGAffineTransformMakeRotation(degreeTOradians(0));
            self.nextCell.transform= CGAffineTransformMakeRotation(degreeTOradians(0));
        }
        
    }
    
    if (sender.state==UIGestureRecognizerStateEnded) {
        //NSLog(@"end");
        CGFloat xTotalMove=translation.x-self.pointStart.x;
        if (xTotalMove<0) {
            [self swipeEnd];
        }else{
            [self swipeGoBack];
        }
        
    }
//    NSLog(@"%@%f%@%f",@"x:",speed.x,@"y:",speed.y);
    //NSLog(@"%@%f%@%f",@"x:",translation.x,@"y:",translation.y);
}

/**
 *  @author StoneMover, 16-12-29 14:12:33
 *
 *  @brief 獲取為顯示的cell,復(fù)用機(jī)制
 *
 *  @param identifier id標(biāo)志
 *
 *  @return 返回的cell,如果緩存中沒有則返回空
 */
-(UITableViewCell*)dequeueReusableUIViewWithIdentifier:(NSString *)identifier{
    
    for (UITableViewCell * cell in self.cacheViews) {
        if ([identifier isEqualToString:cell.reuseIdentifier]) {
            [self.cacheViews removeObject:cell];
            return cell;
        }
    }
    
    return nil;
}

//滑動(dòng)到下一個(gè)界面
-(void)swipeEnd{
    [UIView animateWithDuration:0.3 animations:^{
        self.nextCell.transform= CGAffineTransformMakeRotation(degreeTOradians(0));
    }];
    
    //self.nowCell.transform= CGAffineTransformMakeRotation(degreeTOradians(0));
    CGPoint center=self.nowCell.center;
    [UIView animateWithDuration:0.3 animations:^{
        self.nowCell.center=CGPointMake(center.x-self.w, center.y);
        self.nowCell.transform= CGAffineTransformMakeRotation(degreeTOradians(0));
//        [self.nowCell setAlpha:0.0];
    } completion:^(BOOL finished) {
        self.nowIndex++;
        self.nowIndex=self.nowIndex<self.totalNum?self.nowIndex:0;
        if (self.viewRemove&&[self isNeedAddToCache:self.viewRemove]) {
            [self.cacheViews addObject:self.viewRemove];
            [self.viewRemove removeFromSuperview];
        }
        self.viewRemove=self.nowCell;
        //self.viewRemove.layer.anchorPoint=CGPointMake(0, 0);
        //self.viewRemove.transform=CGAffineTransformMakeRotation(degreeTOradians(-35));
        
        
        self.nowCell=self.nextCell;
        self.nextCell=self.thirdCell;
        
        UITableViewCell * thirdCell=[self.delegate SMSwipeGetView:self withIndex:self.nowIndex+2<self.totalNum?(int)self.nowIndex+2:(int)self.nowIndex+2-(int)self.totalNum];
        
        [thirdCell removeFromSuperview];
        thirdCell.layer.anchorPoint=CGPointMake(1, 1);
        thirdCell.frame=CGRectMake(LEFT_RIGHT_MARGIN*2, 0, self.w-2*2*LEFT_RIGHT_MARGIN, self.h-TOP_MARGTIN);
        self.thirdCell=thirdCell;
        [self insertSubview:thirdCell belowSubview:self.nextCell];
        
        [UIView animateWithDuration:0.2 animations:^{
            self.nowCell.frame=CGRectMake(0, TOP_MARGTIN, self.w, self.h-TOP_MARGTIN);
            self.nextCell.frame=CGRectMake(LEFT_RIGHT_MARGIN, TOP_MARGTIN/2*1, self.w-2*LEFT_RIGHT_MARGIN, self.h-TOP_MARGTIN);
        }];
    }];
}

//滑動(dòng)到上一個(gè)界面
-(void)swipeGoBack{
    
    if (!self.viewRemove) {
        NSLog(@"!viewRemove");
        return;
    }
    
    if (self.nowIndex==0) {
        NSLog(@"!viewRemove+index");
        return;
    }
    
    CGPoint center=self.viewRemove.center;
    
    self.nowIndex--;
    
//    if ([self isNeedAddToCache:self.thirdCell]) {
//        [self.cacheViews addObject:self.thirdCell];
//    }
    [self.thirdCell removeFromSuperview];
    
    
    self.thirdCell=self.nextCell;
    self.nextCell=self.nowCell;
    self.nowCell=self.viewRemove;
    
    if (self.nowIndex==0) {
        self.viewRemove=nil;
        
    }else{
        UITableViewCell * cell=[self.delegate SMSwipeGetView:self withIndex:(int)self.nowIndex-1];
        [cell removeFromSuperview];
        [self insertSubview:cell aboveSubview:self.nowCell];
        cell.layer.anchorPoint=CGPointMake(1, 1);
        cell.frame=self.viewRemove.frame;
        self.viewRemove=cell;
    }
    
    [UIView animateWithDuration:.5 animations:^{
        self.nowCell.center=CGPointMake(center.x+self.w, center.y);
        self.nowCell.transform= CGAffineTransformMakeRotation(degreeTOradians(0));
        self.nextCell.frame=CGRectMake(LEFT_RIGHT_MARGIN, TOP_MARGTIN/2*1, self.w-2*LEFT_RIGHT_MARGIN, self.h-TOP_MARGTIN);
        self.thirdCell.frame=CGRectMake(LEFT_RIGHT_MARGIN*2, 0, self.w-2*2*LEFT_RIGHT_MARGIN, self.h-TOP_MARGTIN);
    }];
}

//是否需要加入到緩存中去
-(BOOL)isNeedAddToCache:(UITableViewCell*)cell{
    for (UITableViewCell * cellIn in self.cacheViews) {
        if ([cellIn.reuseIdentifier isEqualToString:cell.reuseIdentifier]) {
            
            return NO;
        }
    }
    return YES;
}

@end

代碼地址

https://github.com/StoneMover/alldemo/tree/master/swipe

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末饱普,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子状共,更是在濱河造成了極大的恐慌套耕,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,546評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件峡继,死亡現(xiàn)場(chǎng)離奇詭異冯袍,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)碾牌,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,224評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門颠猴,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人小染,你說(shuō)我怎么就攤上這事翘瓮。” “怎么了裤翩?”我有些...
    開封第一講書人閱讀 164,911評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵资盅,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我踊赠,道長(zhǎng)呵扛,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,737評(píng)論 1 294
  • 正文 為了忘掉前任筐带,我火速辦了婚禮今穿,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘伦籍。我一直安慰自己蓝晒,他們只是感情好腮出,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,753評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著芝薇,像睡著了一般胚嘲。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上洛二,一...
    開封第一講書人閱讀 51,598評(píng)論 1 305
  • 那天馋劈,我揣著相機(jī)與錄音,去河邊找鬼晾嘶。 笑死妓雾,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的垒迂。 我是一名探鬼主播君珠,決...
    沈念sama閱讀 40,338評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼娇斑!你這毒婦竟也來(lái)了策添?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,249評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤毫缆,失蹤者是張志新(化名)和其女友劉穎唯竹,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體苦丁,經(jīng)...
    沈念sama閱讀 45,696評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡浸颓,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,888評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了旺拉。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片产上。...
    茶點(diǎn)故事閱讀 40,013評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖蛾狗,靈堂內(nèi)的尸體忽然破棺而出晋涣,到底是詐尸還是另有隱情,我是刑警寧澤沉桌,帶...
    沈念sama閱讀 35,731評(píng)論 5 346
  • 正文 年R本政府宣布谢鹊,位于F島的核電站,受9級(jí)特大地震影響留凭,放射性物質(zhì)發(fā)生泄漏佃扼。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,348評(píng)論 3 330
  • 文/蒙蒙 一蔼夜、第九天 我趴在偏房一處隱蔽的房頂上張望兼耀。 院中可真熱鬧,春花似錦、人聲如沸瘤运。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,929評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)尽超。三九已至,卻和暖如春梧躺,著一層夾襖步出監(jiān)牢的瞬間似谁,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,048評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工掠哥, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留巩踏,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,203評(píng)論 3 370
  • 正文 我出身青樓续搀,卻偏偏與公主長(zhǎng)得像塞琼,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子禁舷,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,960評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)彪杉、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,105評(píng)論 4 62
  • 書名:三體 作者:劉慈欣 評(píng)論:劉慈欣本為一名工程師牵咙,后成為科幻小說(shuō)作家派近。他以過硬的物理學(xué)功底,描述了三體星人入侵...
    lixb閱讀 977評(píng)論 5 1
  • “就在今天中午我在飯桌上跟我奶奶說(shuō)”洁桌,我在過兩三天去順昌渴丸,她說(shuō):“可以,但是有個(gè)條件要我下學(xué)期好好讀書另凌!”我答應(yīng)她...
    或者說(shuō)是閱讀 243評(píng)論 0 1
  • 待完善 親身的數(shù)據(jù)采集和對(duì)比顯示吠谢,你以為vs真相土童,方法告知。 再挖一步讓“懶”變得更有理有據(jù)工坊,做精明的生活家D壬取!征...
    念即起行閱讀 218評(píng)論 0 0
  • “像個(gè)孫子一樣栅组,我堅(jiān)持了四年雀瓢,容易嗎?” 鄭希高坐在三爻村小楊烤肉店最里面靠窗的位置一口氣干掉了三瓶啤酒玉掸,“誰(shuí)他媽...
    輕讀漫寫閱讀 274評(píng)論 0 1