iOS(視圖控制器轉場)

來源:互聯網
上載者:User

標籤:擷取   協議   creat   off   height   資料   resizable   fork   blank   

轉場需要提供轉場代理,不使用預設的代理則需要自己實現代理方式,有UINavigationController、UITabBarController、UIViewController三種代理,實現以下三種協議
<UINavigationControllerDelegate>     //push和pop切換<UITabBarControllerDelegate>       //tab切換
<UIViewControllerTransitioningDelegate>  //UICollectionViewController 與 UINavigationController 結合的轉場方式

轉場觸發時,需要UIKit 將要求轉場代理將提供轉場動畫的核心構件:動畫控制器和互動控制器


動畫控制器(Animation Controller):

 最重要的部分,負責添加視圖以及執行動畫;遵守<UIViewControllerAnimatedTransitioning>協議;由我們實現。

  

 互動控制器

 通過互動手段,通常是手勢來驅動動畫控制器實現的動畫,使得使用者能夠控制整個過程;遵守<UIViewControllerInteractiveTransitioning>協議;系統已經打包好現成的類供我們使用

 

   轉場環境(Transition Context):

   提供轉場中需要的資料;遵守<UIViewControllerContextTransitioning>協議;由 UIKit 在轉場開始前產生並提供給我們提交的動畫控制 器和互動控制器使用。

  

 轉場協調器(Transition Coordinator):

可在轉場動畫發生的同時並存執行其他的動畫,其作用與其說協調不如說輔助,主要在 Modal 轉場和互動轉場取消時使用,其他時候很少用到;遵守<UIViewControllerTransitionCoordinator>協議;由 UIKit 在轉場時產生,UIViewController 在 iOS 7 中新增了方法transitionCoordinator()返回一個遵守該協議的對象,且該方法只在該控制器處於轉場過程中才返回一個此類對象,不參與轉場時返回 nil

動畫控制器協議實現:
 
 

//返回動畫時間


- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {


    


    return self.duration;


}


 


//執行動畫


- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {


    //返回[內容] 檢視,轉場發生的地方


    //擷取參與轉場的視圖控制器,有 UITransitionContextFromViewControllerKey 和 UITransitionContextToViewControllerKey 兩個 Key。


    //通過viewForKey:擷取的視圖是viewControllerForKey:返回的控制器的根視圖,或者 nil。viewForKey:方法返回 nil 只有一種情況: UIModalPresentationCustom 模式下的 Modal 轉場 ,通過此方法擷取 presentingView 時得到的將是 nil,在後面的 Modal 轉場裡會詳細解釋。


    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];


    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];


    UIView *toView = toVC.view;


    UIView *fromView = fromVC.view;


    


    [self animateTransition:transitionContext fromVC:fromVC toVC:toVC fromView:fromView toView:toView];


}


- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext fromVC:(UIViewController *)fromVC toVC:(UIViewController *)toVC fromView:(UIView *)fromView toView:(UIView *)toView {

    

    // Add the toView to the container

    UIView* containerView = [transitionContext containerView];

    [containerView addSubview:toView];

    [containerView sendSubviewToBack:toView];

    

    CGSize size = toView.frame.size;

    

    NSMutableArray *snapshots = [NSMutableArray new];

    

    CGFloat xFactor = 10.0f;

    CGFloat yFactor = xFactor * size.height / size.width;

    

    // snapshot the from view, this makes subsequent snaphots more performant

    UIView *fromViewSnapshot = [fromView snapshotViewAfterScreenUpdates:NO];

    

    // create a snapshot for each of the exploding pieces

    for (CGFloat x=0; x < size.width; x+= size.width / xFactor) {

        for (CGFloat y=0; y < size.height; y+= size.height / yFactor) {

            CGRect snapshotRegion = CGRectMake(x, y, size.width / xFactor, size.height / yFactor);

            UIView *snapshot = [fromViewSnapshot resizableSnapshotViewFromRect:snapshotRegion  afterScreenUpdates:NO withCapInsets:UIEdgeInsetsZero];

            snapshot.frame = snapshotRegion;

            [containerView addSubview:snapshot];

            [snapshots addObject:snapshot];

        }

    }

    

    [containerView sendSubviewToBack:fromView];

    

    // animate

    NSTimeInterval duration = [self transitionDuration:transitionContext];

    [UIView animateWithDuration:duration animations:^{

        for (UIView *view in snapshots) {

            CGFloat xOffset = [self randomFloatBetween:-100.0 and:100.0];

            CGFloat yOffset = [self randomFloatBetween:-100.0 and:100.0];

            view.frame = CGRectOffset(view.frame, xOffset, yOffset);

            view.alpha = 0.0;

            view.transform = CGAffineTransformScale(CGAffineTransformMakeRotation([self randomFloatBetween:-10.0 and:10.0]), 0.01, 0.01);

        }

    } completion:^(BOOL finished) {

        for (UIView *view in snapshots) {

            [view removeFromSuperview];

        }

        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];

    }];

    

}

 

- (float)randomFloatBetween:(float)smallNumber and:(float)bigNumber {

    float diff = bigNumber - smallNumber;

    return (((float) (arc4random() % ((unsigned)RAND_MAX + 1)) / RAND_MAX) * diff) + smallNumber;

}

 

 

參考連結:http://blog.devtang.com/2016/03/13/iOS-transition-guide/


  

iOS(視圖控制器轉場)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.