轉(zhuǎn)場(chǎng)動(dòng)畫

方法1: 利用控制器的 transitionFromViewController:toViewController: ... 方法轉(zhuǎn)場(chǎng),from, to兩個(gè)控制器必須是同一個(gè)控制器的子控制器


    BigPhotoViewController *bigVc = [[BigPhotoViewController alloc] init];
    
    [self addChildViewController:bigVc];
    
    bigVc.image = self.array[indexPath.item];
//    [self presentViewController:bigVc animated:YES completion:nil];
    
    bigVc.view.alpha = 0.0;
    // 1. 利用控制器的 transitionFromViewController:toViewController: ... 方法轉(zhuǎn)場(chǎng),from, to兩個(gè)控制器必須是同一個(gè)控制器的子控制器
    
    [self transitionFromViewController:self.vc1  toViewController:bigVc duration:0.5 options:UIViewAnimationOptionCurveLinear animations:^{
        
        bigVc.view.alpha = 1.0;
        
    } completion:nil];

方法2: 利用CoreAnimation的CATransition

    BigPhotoViewController *bigVc = [[BigPhotoViewController alloc] init];

    bigVc.image = self.array[indexPath.item];

    // 2. 利用CATransition
    CATransition *transition = [CATransition animation];
    transition.type = @"cube";
    
    [self.view.layer addAnimation:transition forKey:nil];
    
    [self presentViewController:bigVc animated:YES completion:nil];

方法3: 利用轉(zhuǎn)場(chǎng)代理transitioningDelegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    BigPhotoViewController *bigVc = [[BigPhotoViewController alloc] init];

    bigVc.image = self.array[indexPath.item];
    bigVc.modalPresentationStyle = UIModalPresentationCustom;
    
    // 3. 利用轉(zhuǎn)場(chǎng)代理
    
    bigVc.transitioningDelegate = self.animator;
    self.animator.delegate = self;
    self.animator.indexPath = indexPath;
    
    [self presentViewController:bigVc animated:YES completion:nil];
    
}

需要遵守<UIViewControllerTransitioningDelegate>協(xié)議的對(duì)象animator做代理


@interface BigPhotoAnimator : NSObject  <UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning>
// 自定義遵守<UIViewControllerTransitioningDelegate>協(xié)議的對(duì)象, 實(shí)現(xiàn)下面方法(present時(shí) / dismiss時(shí)各一個(gè))
#pragma mark - <UIViewControllerTransitioningDelegate>
// 出現(xiàn)時(shí)的動(dòng)畫對(duì)象
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
    self.isPresentAnimate = YES;
    return self;
}

/*
 * 消失時(shí)的動(dòng)畫對(duì)象
 */
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
    self.isPresentAnimate = NO;
    return self;
    
}

這兩個(gè)方法的返回對(duì)象必須遵守<UIViewControllerAnimatedTransitioning>協(xié)議, 可以返回自定義對(duì)象本身,讓該對(duì)象遵守<UIViewControllerAnimatedTransitioning>協(xié)議,實(shí)現(xiàn)以下方法

#pragma mark - <UIViewControllerAnimatedTransitioning>

// 動(dòng)畫時(shí)間
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
    
    return 0.5;
}

// 具體動(dòng)畫實(shí)現(xiàn)
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    // 通過上下文獲得from控制器
    UIViewController *fromVc = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    
    // 通過上下文獲得to控制器
    UIViewController *toVc = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
    // 通過上下文獲得from的view
    UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
    // 通過上下文獲得to的view
    UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];

    // 獲取動(dòng)畫時(shí)間
    NSTimeInterval duration = [self transitionDuration:transitionContext];
    
    if (self.isPresentAnimate) {  // 彈出動(dòng)畫
        
        toView.alpha = 0.0;
        UIImage *image = nil;
        CGRect rect = CGRectZero;
        
        
        // 自定義協(xié)議,通過代理實(shí)現(xiàn)協(xié)議的方法獲得要做動(dòng)畫的imageView的圖片和大小
        if (_delegate) {
            image = [_delegate animator:self ImageForIndexPath:self.indexPath];
            rect = [_delegate animator:self RectForIndexPath:self.indexPath];
        }
        
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        imageView.frame = rect;
        
        // 把要?jiǎng)赢嫷膙iew添加到containerView  記得!!
        [[transitionContext containerView] addSubview:toView];
        [[transitionContext containerView] addSubview:imageView];
        
        
        toView.alpha = 0.0;
        
        // 動(dòng)畫實(shí)現(xiàn)
        [UIView animateWithDuration:duration animations:^{
            
            
            imageView.frame = [UIScreen mainScreen].bounds;
            
        } completion:^(BOOL finished) {
            
            [imageView removeFromSuperview];
            toView.alpha = 1.0;
            
            // 通過上下文通知?jiǎng)赢嫿Y(jié)束
            [transitionContext completeTransition:YES];
        }];
        
    }
    else {  // 消失動(dòng)畫
        
        
        toView.alpha = 0.0;
        
        UIImage *image = nil;
        CGRect rect = CGRectZero;
        
        // 自定義協(xié)議,通過代理實(shí)現(xiàn)協(xié)議的方法獲得要做動(dòng)畫的imageView的圖片和大小
        if (_delegate) {
            
            image = [_delegate animator:self ImageForIndexPath:self.indexPath];
            rect = [_delegate animator:self RectForIndexPath:self.indexPath];
        }
        
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        imageView.frame = [UIScreen mainScreen].bounds;
        
        
        // 添加到containerView
        [[transitionContext containerView] addSubview:imageView];
        [[transitionContext containerView] addSubview:fromView];
        
        fromView.alpha = 0.0;
        
        // 動(dòng)畫實(shí)現(xiàn)
        [UIView animateWithDuration:duration animations:^{
            
            imageView.frame = rect;
            
        } completion:^(BOOL finished) {
            
            [imageView removeFromSuperview];
            
            
            // 通過上下文通知?jiǎng)赢嫿Y(jié)束
            [transitionContext completeTransition:YES];
        }];
        
    }
        
  
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子丹拯,更是在濱河造成了極大的恐慌几莽,老刑警劉巖逛万,帶你破解...
    沈念sama閱讀 216,544評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件畦戒,死亡現(xiàn)場(chǎng)離奇詭異季俩,居然都是意外死亡昼蛀,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,430評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門陡舅,熙熙樓的掌柜王于貴愁眉苦臉地迎上來抵乓,“玉大人,你說我怎么就攤上這事≡痔浚” “怎么了茎芋?”我有些...
    開封第一講書人閱讀 162,764評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)蜈出。 經(jīng)常有香客問我田弥,道長(zhǎng),這世上最難降的妖魔是什么铡原? 我笑而不...
    開封第一講書人閱讀 58,193評(píng)論 1 292
  • 正文 為了忘掉前任偷厦,我火速辦了婚禮,結(jié)果婚禮上燕刻,老公的妹妹穿的比我還像新娘只泼。我一直安慰自己,他們只是感情好卵洗,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,216評(píng)論 6 388
  • 文/花漫 我一把揭開白布请唱。 她就那樣靜靜地躺著,像睡著了一般过蹂。 火紅的嫁衣襯著肌膚如雪十绑。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,182評(píng)論 1 299
  • 那天酷勺,我揣著相機(jī)與錄音本橙,去河邊找鬼。 笑死鸥印,一個(gè)胖子當(dāng)著我的面吹牛勋功,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播库说,決...
    沈念sama閱讀 40,063評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼片择!你這毒婦竟也來了潜的?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,917評(píng)論 0 274
  • 序言:老撾萬榮一對(duì)情侶失蹤字管,失蹤者是張志新(化名)和其女友劉穎啰挪,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體嘲叔,經(jīng)...
    沈念sama閱讀 45,329評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡亡呵,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,543評(píng)論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了硫戈。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片锰什。...
    茶點(diǎn)故事閱讀 39,722評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出汁胆,到底是詐尸還是另有隱情梭姓,我是刑警寧澤,帶...
    沈念sama閱讀 35,425評(píng)論 5 343
  • 正文 年R本政府宣布嫩码,位于F島的核電站誉尖,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏铸题。R本人自食惡果不足惜铡恕,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,019評(píng)論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望丢间。 院中可真熱鬧没咙,春花似錦、人聲如沸千劈。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,671評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽墙牌。三九已至涡驮,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間喜滨,已是汗流浹背捉捅。 一陣腳步聲響...
    開封第一講書人閱讀 32,825評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留虽风,地道東北人棒口。 一個(gè)月前我還...
    沈念sama閱讀 47,729評(píng)論 2 368
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像辜膝,于是被迫代替她去往敵國(guó)和親无牵。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,614評(píng)論 2 353

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