您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 自定義轉場動畫:以 Present 轉場為例的完整拆解

編輯:iOS 開發綜合

自定義轉場動畫其實並不難,關鍵在於把思路——也就是操作步驟——理清楚。本文以 present 轉場為例,示範如何替換系統預設的推出方式,並換上自己想要的動畫。

為什麼要自定義

若不做任何修改,presentViewController:animated:completion: 這個模態推出方法想必大家都很熟悉,預設一律從螢幕下方把新控制器推上來。自定義的目的就是打破這個固定樣式,同時加入自訂動畫。

這裡有個必須掌握的核心概念:UIViewControllerAnimatedTransitioning 協議負責控制動畫,需要自己實作。

第一步:在來源控制器宣告代理

@interface ViewController () 
@end

第二步:觸發跳轉時指定代理與樣式

- (void)click:(UIButton *)sender {
    ModelViewController *modalViewController = [ModelViewController new];
    // 指定代理
    modalViewController.transitioningDelegate = self;
    // Style = 自定義方式
    modalViewController.modalPresentationStyle = UIModalPresentationCustom;
    [self presentViewController:modalViewController animated:YES completion:NULL];
}

第三步:實作 UIViewControllerTransitioningDelegate

真正決定動畫的是下面這兩個協議方法,建議仔細閱讀 UIViewControllerTransitioningDelegate 裡的註解:

// 推出與收回各回傳一個動畫物件
- (id )animationControllerForPresentedController:(UIViewController *)presented
                                                                      presentingController:(UIViewController *)presenting
                                                                          sourceController:(UIViewController *)source {
    // 推出動畫,參照 POP 動畫 Demo 實作
    return [PresentAnimator new];
}

- (id )animationControllerForDismissedController:(UIViewController *)dismissed {
    // 退出動畫
    return [DismissAnimator new];
}

// 若想加上手勢控制,可在這個方法中處理
- (id )interactionControllerForDismissal:(id )animator {
    return nil;
}

第四步:動畫本體的實作

接下來分別實作 PresentAnimator 與 DismissAnimator,兩者都採用 UIViewControllerAnimatedTransitioning 協議。

@interface PresentAnimator : NSObject 
@end

PresentAnimator.m:

// 動畫時長
- (NSTimeInterval)transitionDuration:(id )transitionContext {
    return 0.5f;
}

// 具體動畫方式,這裡使用 POP
- (void)animateTransition:(id )transitionContext {
    // 取得即將推出的目標 VC 的 view
    UIView *toView = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey].view;
    toView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    // 起始位置放在螢幕上方
    toView.center = CGPointMake(transitionContext.containerView.center.x, -transitionContext.containerView.center.y);
    [transitionContext.containerView addSubview:toView];

    // pop 動畫
    POPSpringAnimation *positionAnim = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPositionY];
    positionAnim.toValue = @(transitionContext.containerView.center.y);
    positionAnim.springBounciness = 10;
    [positionAnim setCompletionBlock:^(POPAnimation *anim, BOOL finished) {
        [transitionContext completeTransition:YES];
    }];

    POPSpringAnimation *scaleAnim = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
    scaleAnim.springBounciness = 20;
    scaleAnim.fromValue = [NSValue valueWithCGPoint:CGPointMake(1.2, 1.4)];

    [toView.layer pop_addAnimation:positionAnim forKey:@"positionAnimation"];
    [toView.layer pop_addAnimation:scaleAnim forKey:@"scaleAnimation"];
}

DismissAnimator.m:

- (NSTimeInterval)transitionDuration:(id )transitionContext {
    return 0.5f;
}

- (void)animateTransition:(id )transitionContext {
    // fromVC 就是從哪個控制器推過來,就回到哪裡去
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];

    POPBasicAnimation *offscreenAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerPositionY];
    offscreenAnimation.toValue = @(-fromVC.view.layer.position.y);
    [offscreenAnimation setCompletionBlock:^(POPAnimation *anim, BOOL finished) {
        [transitionContext completeTransition:YES];
    }];

    [fromVC.view.layer pop_addAnimation:offscreenAnimation forKey:@"offscreenAnimation"];
}

延伸:UINavigationController 的轉場也能自定義

導航控制器的切換動畫同樣可以替換,方法是簽訂並實作 UINavigationControllerDelegate:

// 簽訂協議
self.navigationController.delegate = self;
- (id )navigationController:(UINavigationController *)navigationController
                                  animationControllerForOperation:(UINavigationControllerOperation)operation
                                               fromViewController:(UIViewController *)fromVC
                                                 toViewController:(UIViewController *)toVC {
    if (operation == UINavigationControllerOperationPush) {
        // 動畫方式不變
        return [PresentAnimator new];
    } else {
        return nil;
    }
}

把這幾個角色釐清——代理、動畫物件、動畫協議——自定義轉場就不再神祕。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved