您好,登錄后才能下訂單哦!
上一篇文章介紹了如何進行modal方式的頁面切換
自定義的目的控制器在modal切換時完全覆蓋源控制器,本篇文章介紹如何實現一個自定義的過渡效果
實現的效果描述:
目的控制器:占據屏幕的二分之一大小,且居中,并在源控制器上覆蓋著一個陰影效果,點擊陰影時目的控制器返回
UIPresentationController
用于描述目的控制器通過modal方式切換的過渡效果
實現其子類,可以自定義出特殊的效果
實現步驟:
定義過渡效果:實現UIPresentationController子類
目的控制器,遵循過渡協議,設置過渡控制器對象
源控制器進行modal切換
UIPresentationController的屬性:
@property(nonatomic, retain, readonly) UIViewController *presentedViewController //目的控制器 @property(nonatomic, retain, readonly) UIViewController*presentingViewController //源控制器 - (UIView *)presentedView //目的view @property(nonatomic, readonly) UIView *containerView //源view
UIPresentationController的子類應重寫的方法
1)init方法,可以創建其他輔助過渡效果的view
- (instancetype)initWithPresentedViewController:(UIViewController*)presentedViewController presentingViewController:(UIViewController*)presentingViewController
如:
- (instancetype)initWithPresentedViewController:(UIViewController*)presentedViewController presentingViewController:(UIViewController*)presentingViewController { if ( self = [super initWithPresentedViewController:presentedViewController presentingViewController:presentingViewController] ) { _shadowBtn = [UIButton buttonWithType:UIButtonTypeCustom]; //陰影按鈕的初始狀態是隱藏的 _shadowBtn.backgroundColor = [UIColor grayColor]; _shadowBtn.alpha = 0.f; } }
2)重寫presentationTransitionWillbegin方法,定義實現過渡效果
- (void)presentationTransitionWillBegin
如:
- (void)presentationTransitionWillBegin { [self.containerView addSubview:_shadowBtn]; [self.containerView addSubview:self.presentedView]; _shadowBtn.frame = self.containerView.bounds; id <UIViewControllerTransitionCoordinator> coordinate = self.presetingViewController.transitionCoordinator; [coordinate animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) { _shadowBtn.alpha = 0.5; } completion:nil]; }
使用到UIViewController的transitionCoordinator,表示過渡效果的協助器
協助器的animateAlongsideTransition方法定義過渡期間的動畫效果
3)重寫presentationTransitionDidEnd方法,定義過渡效果后的清理工作。
特別是過渡未完成時的清理動作
- (void)presentationTransitionDidEnd:(BOOL)completed
- (void)presentationTransitionDidEnd:(BOOL)completed { if ( !completed ) { [_shadowBtn removeFromSuperview]; } }
4)重寫frameOfPresentedViewInContainerView,設置被顯示視圖的frame
- (CGRect)frameOfPresentedViewInContainerView
- (CGRect)frameOfPresentedViewInContainerView { CGFloat x, y, w, h; w = self.containerView.frame.size.width/2; h = self.containerView.frame.size.height/2; x = self.containerView.frame.size.width/4; y = self.containerView.frame.size.height/4; return CGRectMake(x, y, w, h); }
5)重寫dismissTransitionWillBegin方法,設置返回的過渡效果
- (void)dismissalTransitionWillBegin
- (void)dismissalTransitionWillBegin { id<UIViewControllerTransitionCoordinator> coordinator = self.presetingViewController.transitionCoordinator; [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonull context) { _shadowBtn.alpha = 0.01; } completion:nil]; }
6)重寫dismissalTransitionDidEnd方法,執行清理動作
- (void)dismissalTransitionDidEnd:(BOOL)completed
- (void)dismissalTransitionDidEnd:(BOOL)completed { if ( completed ) { [_shadowView removeFromSuperview]; } }
目的控制器設置過渡效果
目的控制器遵循代理協議
@interface AMDestViewController () <UIViewControllerTransitioningDelegate>
設置代理
self.transitioningDelegate = self;
實現代理方法
- (UIPresetationController *) presentationControllerForPresentedViewController:(UIViewController*) presented presentingViewController:(UIViewController*) presenting sourceViewController:(UIViewController*) source { return [[AMPresentationController alloc] initWithPresentedViewController:presented presentingViewController:presenting]; }
源控制器進行modal切換
iOS8.0開始支持這種自定義的過渡效果,主要要設置目的控制器的modalPresentationStyle為自定義。
這種切換方式,在iPhone和iPad上都是可用的。
AMDestViewController * vc = [[AMDestViewController alloct] init]; vc.modalPresentationStyle = UIModalPresentationCustom; [self presentViewController:vc animated:YES completion:nil];
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。