tableview到collectionView自定義轉(zhuǎn)場動畫+手勢驅(qū)動

寫在前面

這兩天還是在搗鼓collectionView,每當(dāng)我切換自己自定義的各種奇奇怪怪的collectionViewLayout的時候焙贷,我都對蘋果對布局切換的動畫處理佩服得五體投地尝偎,如此絲滑般流暢凿滤,同時蘋果也將這種絲滑的動畫效果用到了自定義轉(zhuǎn)場中拔创,從iOS7開始呢诬,在collectionViewController中就伴隨著自定義轉(zhuǎn)場的功能產(chǎn)生了一個新的屬性:useLayoutToLayoutNavigationTransitions,這是一個BOOL值涌哲,如果設(shè)置該值為YES胖缤,如果navigationController push或者pop 一個collectionViewController 到另一個collectionViewController的時候,其所在的navigationController就可以用collectionView的布局轉(zhuǎn)場動畫來替換標(biāo)準(zhǔn)的轉(zhuǎn)場阀圾,這點大家可以自行嘗試一下哪廓,但是顯然,這個屬性的致命的局限性就是你得必須滿足都是collectionViewController初烘,對于collectionView就沒辦法了涡真,所以我就思考了一下如何在兩個collectionView之間轉(zhuǎn)場,進而有了一個更奇怪的想法账月,能不能在一個tableView和collectionView之間實現(xiàn)自定義轉(zhuǎn)場效果综膀,所以就有了如下的效果:

圖1

t1.gif

圖2:小到大 + 手勢驅(qū)動

t3.gif

圖3: 大到小 + 手勢驅(qū)動

t2.gif

關(guān)于效果的邏輯

1、push的時候點擊tableView中的任意一個cell局齿,當(dāng)轉(zhuǎn)場到collectionView中的時候,將collectionView移動到這個cell在第一行的位置顯示橄登,如果這個位置超過了collectionView的最大contentOffset抓歼,則移動到最大contentOffset就行了,這樣了保證點擊的cell的顯示盡量靠前拢锹,比較符合邏輯谣妻;
2、同理卒稳,pop的的時候點擊collectionView中任意cell蹋半,當(dāng)轉(zhuǎn)場到tableView的時候,將tableView移動到把這個cell顯示到最前方的位置充坑,如果超過了最大contentOffset則移動到最大的offset减江;如果點擊了back,就把當(dāng)前可顯示cell的第一個展示到最前方捻爷;

原理

關(guān)于自定義轉(zhuǎn)場的基礎(chǔ)知識辈灼,大家可以參照我在簡書的第一篇文章:iOS自定義轉(zhuǎn)場動畫,所以下面我不在介紹自定義轉(zhuǎn)場的基本知識的也榄,我這里用到的轉(zhuǎn)場管理者和手勢過渡管理者都是來自于這篇文章中的代碼巡莹,畢竟它們被蘋果設(shè)計的相當(dāng)容易復(fù)用,下面主要解釋一下動畫實現(xiàn)的原理甜紫,不過需要吐槽一下降宅,動畫的代碼量比較大,如果真的需要在項目中用到這個效果囚霸,你可能還需要微調(diào)很多腰根,畢竟項目中大部分cell都是自定義的,而且cell的高度可能都不同邮辽,所以計算會更麻煩唠雕,我只是寫出了我的思路贸营,供大家參考而已,github地址請戳->XWTableViewToCollectionViewTransition,如果大家有更好的想法歡迎留言和拍磚岩睁!

1钞脂、首先是push的動畫:(大概邏輯就是根據(jù)點擊的indexPath計算collectionView展示時候應(yīng)該的contentOffset -> 根據(jù)offset得到可collectionView可顯示的item -> 根據(jù)當(dāng)前tableView的cell和可顯示的item 得出需要動畫的所有cell并計算他們的起始和終止的frame,然后動畫)

- (void)doPushAnimation:(id<UIViewControllerContextTransitioning>)transitionContext{
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *containerView = [transitionContext containerView];
    UITableView *tableView = fromVC.view.subviews.lastObject;
    UICollectionView *collectionView = toVC.view.subviews.lastObject;
    [containerView addSubview:toVC.view];
    toVC.view.alpha = 0;
    collectionView.hidden = YES;
    //得到當(dāng)前tableView顯示在屏幕上的indexPath
    NSArray *visibleIndexpaths = [tableView indexPathsForVisibleRows];
    //拿到tableView可顯示的第一個indexPath
    NSIndexPath *tableViewFirstPath = visibleIndexpaths.firstObject;
    //拿到tableView可顯示的最后一個indexPath
    NSIndexPath *tableViewLastPath = visibleIndexpaths.lastObject;
    //得到tableView可顯示的第一個cell
    UITableViewCell *firstVisibleCell = [tableView cellForRowAtIndexPath:tableViewFirstPath];
    //得到當(dāng)前點擊的indexPath
    NSIndexPath *selectIndexPath = [tableView indexPathForSelectedRow];
    //通過點擊的indexPath和collectionView的ContentSize計算collectionView顯示時候的contentOffset
    //獲取點擊indexPath對應(yīng)在collectionView中的attr
    UICollectionViewLayoutAttributes *selectAttr = [collectionView layoutAttributesForItemAtIndexPath:selectIndexPath];
    //獲取collectionView的ContentSize
    CGSize contentSize = [collectionView.collectionViewLayout collectionViewContentSize];
    //計算contentOffset的最大值
    CGFloat maxY = contentSize.height - collectionView.bounds.size.height;
    //計算collectionView顯示時候的offset:如果該offset超過了最大值就去最大值捕儒,否則就取將所選擇的indexPath的item排在可顯示的第一行的時候的indexPath
    CGPoint newOffset = CGPointMake(0, MIN(maxY, selectAttr.frame.origin.y - 64));
    //得到當(dāng)前顯示區(qū)域的frame
    CGRect newFrame = CGRectMake(0, MIN(maxY, selectAttr.frame.origin.y), collectionView.bounds.size.width, collectionView.bounds.size.height);
    //根據(jù)frame得到可顯示區(qū)域內(nèi)所有的item的attrs
    NSArray *showAttrs = [collectionView.collectionViewLayout layoutAttributesForElementsInRect:newFrame];
    //進而得到所有可顯示的item的indexPath
    NSMutableArray *showIndexPaths = @[].mutableCopy;
    for (UICollectionViewLayoutAttributes *attr in showAttrs) {
        [showIndexPaths addObject:attr.indexPath];
    }
    //拿到collectionView可顯示的第一個indexPath
    NSIndexPath *collectionViewFirstPath = showIndexPaths.firstObject;
    //拿到collectionView可顯示的最后一個indexPath
    NSIndexPath *collectionViewLastPath = showIndexPaths.lastObject;
    //現(xiàn)在可以拿到需要動畫的第一個indexpath
    NSIndexPath *animationFirstIndexPath = collectionViewFirstPath.item > tableViewFirstPath.row ? tableViewFirstPath : collectionViewFirstPath;
    //現(xiàn)在可以拿到需要動畫的最后一個indexpath
    NSIndexPath *animationLastIndexPath = collectionViewLastPath.item > tableViewLastPath.row ? collectionViewLastPath : tableViewLastPath;
    //下面就可以計算需要動畫的視圖的起始frame了
    NSMutableArray *animationViews = @[].mutableCopy;
    NSMutableArray *animationIndexPaths = @[].mutableCopy;
    NSMutableArray *images = @[].mutableCopy;
    for (NSInteger i = animationFirstIndexPath.row; i <= animationLastIndexPath.row; i ++) {
        //這里就無法使用截圖大法了冰啃,因為我們要計算可顯示區(qū)域外的cell的位置,所以只有直接通過數(shù)據(jù)源取得圖片刘莹,自己生成ImageView
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:_data[i]]];
        //frame從第一個開始依次向下排列
        imageView.frame = CGRectApplyAffineTransform([[firstVisibleCell imageView] convertRect:[firstVisibleCell imageView].bounds toView:containerView], CGAffineTransformMakeTranslation(0, -60 * (tableViewFirstPath.row - i)));
        //添加imageView到contentView
        [animationViews addObject:imageView];
        [containerView addSubview:imageView];
        [animationIndexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
        //隱藏tableView的imageView
        UIImageView *imgView = (UIImageView *)[[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]] imageView];
        if (imgView) {
            imgView.hidden = YES;
            [images addObject:imgView];
        }
    }
    //終于可以動畫了
    [UIView animateWithDuration:1 animations:^{
        //讓toView顯示出來
        toVC.view.alpha = 1;
        //取出所有的可動畫的imageView阎毅,并移動到對應(yīng)collectionView的正確位置去
        for (int i = 0; i < animationViews.count; i ++) {
            UIView *animationView = animationViews[i];
            NSIndexPath *animationPath = animationIndexPaths[i];
            animationView.frame = CGRectApplyAffineTransform([collectionView layoutAttributesForItemAtIndexPath:animationPath].frame, CGAffineTransformMakeTranslation(0, -newOffset.y));
        }
    } completion:^(BOOL finished) {
        //標(biāo)記轉(zhuǎn)場完成
        [transitionContext completeTransition:YES];
        //設(shè)置collectionView的contentOffset
        [collectionView setContentOffset:newOffset];
        //移除所有的可動畫視圖
        [animationViews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        //顯示出collectionView
        collectionView.hidden = NO;
        //恢復(fù)隱藏的tableViewcell的imageView
        for (int i = 0; i < _data.count; i ++) {
            UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
            cell.imageView.hidden = NO;
        }
    }];
}

2、然后是pop動畫:(大概邏輯就是根據(jù)點擊的indexPath計算tableView展示時候應(yīng)該的contentOffset点弯,并將tableView移動到該位置 -> 根據(jù)offset得到可tableView可顯示的cell -> 根據(jù)當(dāng)前collectionView的可顯示item和tableview可顯示的cell得出需要動畫的所有cell并計算他們的起始和終止的frame扇调,然后動畫)

- (void)doPopAnimation:(id<UIViewControllerContextTransitioning>)transitionContext{
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *containerView = [transitionContext containerView];
    UITableView *tableView = toVC.view.subviews.lastObject;
    UICollectionView *collectionView = fromVC.view.subviews.lastObject;
    [containerView addSubview:toVC.view];
    toVC.view.alpha = 0;
    //collectionView可顯示的所有cell
    NSArray *visibleCells = [collectionView visibleCells];
    //collectionView可顯示的所有indexPath
    NSMutableArray *collectionViewVisbleIndexPaths = @[].mutableCopy;
    for (UICollectionViewCell *cell in visibleCells) {
        [collectionViewVisbleIndexPaths addObject:[collectionView indexPathForCell:cell]];
        cell.hidden = YES;
        
    }
    //由于取出的順序不是從小到大,所以排序一次
    [collectionViewVisbleIndexPaths sortUsingComparator:^NSComparisonResult(NSIndexPath * obj1, NSIndexPath * obj2) {
        return obj1.item < obj2.item ? NSOrderedAscending : NSOrderedDescending;
    }];
    //當(dāng)前選中的cell
    NSIndexPath *selectIndexPath = [collectionView indexPathsForSelectedItems].firstObject;
    //如果不存在抢肛,比如直接back狼钮,取可顯示的第一個cell
    if (!selectIndexPath) {
        selectIndexPath = collectionViewVisbleIndexPaths.firstObject;
    }
    //計算tableView最大的contentOffsetY
    CGFloat maxY = tableView.contentSize.height - tableView.frame.size.height;
    //根據(jù)點擊的selectIndexPath和maxY得到當(dāng)前tableView應(yīng)該移動到的offset
    CGPoint newOffset = CGPointMake(0, MIN(maxY, 60 * selectIndexPath.item - 64));
    
    //設(shè)置tableView的newOffset,必須先設(shè)置,下面的操作都建于此設(shè)置之后
    [tableView setContentOffset:newOffset];
    //取出newOffset下的可顯示cell,隱藏cell的imageView
    NSMutableArray *tableViewVisibleIndexPaths = @[].mutableCopy;
    for (UITableViewCell *cell in [tableView visibleCells]) {
        cell.imageView.hidden = YES;
        [tableViewVisibleIndexPaths addObject:[tableView indexPathForCell:cell]];
    }
    //計算可動畫的第一個indexPath
    NSIndexPath *animationFirstIndexPath = [tableViewVisibleIndexPaths.firstObject row] > [collectionViewVisbleIndexPaths.firstObject row] ? collectionViewVisbleIndexPaths.firstObject : tableViewVisibleIndexPaths.firstObject;
    //計算可動畫的最后一個indexPath
    NSIndexPath *animationLastIndexPath = [tableViewVisibleIndexPaths.lastObject row] > [collectionViewVisbleIndexPaths.lastObject row] ? tableViewVisibleIndexPaths.lastObject : collectionViewVisbleIndexPaths.lastObject;
    //生成所有需要動畫的臨時UIImageView存在一個臨時數(shù)組
    NSMutableArray *animationViews = @[].mutableCopy;
    NSMutableArray *animationIndexPaths = @[].mutableCopy;
    for (NSInteger i = animationFirstIndexPath.row; i <= animationLastIndexPath.row; i ++) {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:_data[i]]];
        //frame為當(dāng)前對應(yīng)的item減去offset的值
        imageView.frame = CGRectApplyAffineTransform([collectionView layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]].frame, CGAffineTransformMakeTranslation(0,  -collectionView.contentOffset.y));
        [containerView addSubview:imageView];
        [animationViews addObject:imageView];
        [animationIndexPaths addObject:[NSIndexPath indexPathForItem:i inSection:0]];
    }
    //開始動畫
    [UIView animateWithDuration:1 animations:^{
        //顯示出toView
        toVC.view.alpha = 1;
        //取出所有的動畫視圖設(shè)置其動畫結(jié)束的frame捡絮,frame有indexPath和newOffset決定
        for (int i = 0; i < animationViews.count; i ++) {
            UIView *animationView = animationViews[i];
            NSIndexPath *animationPath = animationIndexPaths[i];
            animationView.frame = CGRectMake(15, 60 * [animationPath row]  - newOffset.y, 60, 60);
        }
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
        if (![transitionContext transitionWasCancelled]) {
            //如果成功了
            //顯示visiblecell中的imageView
            for (UITableViewCell *cell in [tableView visibleCells]) {
                cell.imageView.hidden = NO;
            }
        }else{
            //否者顯示出隱藏的collectionView的item
            for (UICollectionViewCell *cell in visibleCells) {
                [collectionViewVisbleIndexPaths addObject:[collectionView indexPathForCell:cell]];
                cell.hidden = NO;
            }
        }
        //移除所有的臨時視圖
        [animationViews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    }];
}

最后加上手勢過渡管理這就可以達成手勢驅(qū)動的效果了熬芜,整體的效果還是達到了預(yù)期了

最后

是不是想吐槽實現(xiàn)起來相當(dāng)麻煩,的確是這樣的福稳,因為我們還需要考慮屏幕之外的布局涎拉,或者說是重用池中的那些cell做考慮,才能保證每個cell能夠移動到正確位置的圆,所以不像以前僅僅需要對屏幕中的視圖動畫了鼓拧!不過相比于這個例子最后希望大家多多提出改進意見或者新的便捷的思路,或者能在github上給一顆星星鼓勵一下略板!github地址請戳->XWTableViewToCollectionViewTransition

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末毁枯,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子叮称,更是在濱河造成了極大的恐慌种玛,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,372評論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件瓤檐,死亡現(xiàn)場離奇詭異赂韵,居然都是意外死亡,警方通過查閱死者的電腦和手機挠蛉,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評論 3 392
  • 文/潘曉璐 我一進店門祭示,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人谴古,你說我怎么就攤上這事质涛〕砬福” “怎么了?”我有些...
    開封第一講書人閱讀 162,415評論 0 353
  • 文/不壞的土叔 我叫張陵汇陆,是天一觀的道長怒炸。 經(jīng)常有香客問我,道長毡代,這世上最難降的妖魔是什么阅羹? 我笑而不...
    開封第一講書人閱讀 58,157評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮教寂,結(jié)果婚禮上捏鱼,老公的妹妹穿的比我還像新娘。我一直安慰自己酪耕,他們只是感情好导梆,可當(dāng)我...
    茶點故事閱讀 67,171評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著因妇,像睡著了一般问潭。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上婚被,一...
    開封第一講書人閱讀 51,125評論 1 297
  • 那天,我揣著相機與錄音梳虽,去河邊找鬼址芯。 笑死,一個胖子當(dāng)著我的面吹牛窜觉,可吹牛的內(nèi)容都是我干的谷炸。 我是一名探鬼主播,決...
    沈念sama閱讀 40,028評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼禀挫,長吁一口氣:“原來是場噩夢啊……” “哼旬陡!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起语婴,我...
    開封第一講書人閱讀 38,887評論 0 274
  • 序言:老撾萬榮一對情侶失蹤描孟,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后砰左,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體匿醒,經(jīng)...
    沈念sama閱讀 45,310評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,533評論 2 332
  • 正文 我和宋清朗相戀三年缠导,在試婚紗的時候發(fā)現(xiàn)自己被綠了廉羔。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,690評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡僻造,死狀恐怖憋他,靈堂內(nèi)的尸體忽然破棺而出孩饼,到底是詐尸還是另有隱情,我是刑警寧澤竹挡,帶...
    沈念sama閱讀 35,411評論 5 343
  • 正文 年R本政府宣布镀娶,位于F島的核電站,受9級特大地震影響此迅,放射性物質(zhì)發(fā)生泄漏汽畴。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,004評論 3 325
  • 文/蒙蒙 一耸序、第九天 我趴在偏房一處隱蔽的房頂上張望忍些。 院中可真熱鬧,春花似錦坎怪、人聲如沸罢坝。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽嘁酿。三九已至,卻和暖如春男应,著一層夾襖步出監(jiān)牢的瞬間闹司,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,812評論 1 268
  • 我被黑心中介騙來泰國打工沐飘, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留游桩,地道東北人。 一個月前我還...
    沈念sama閱讀 47,693評論 2 368
  • 正文 我出身青樓耐朴,卻偏偏與公主長得像借卧,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子筛峭,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,577評論 2 353

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