上一篇文章介紹了如何進(jìn)行modal方式的頁面切換
創(chuàng)新互聯(lián)建站專注于阿魯科爾沁企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè),商城網(wǎng)站建設(shè)。阿魯科爾沁網(wǎng)站建設(shè)公司,為阿魯科爾沁等地區(qū)提供建站服務(wù)。全流程按需制作網(wǎng)站,專業(yè)設(shè)計,全程項目跟蹤,創(chuàng)新互聯(lián)建站專業(yè)和態(tài)度為您提供的服務(wù)
自定義的目的控制器在modal切換時完全覆蓋源控制器,本篇文章介紹如何實現(xiàn)一個自定義的過渡效果
實現(xiàn)的效果描述:
目的控制器:占據(jù)屏幕的二分之一大小,且居中,并在源控制器上覆蓋著一個陰影效果,點擊陰影時目的控制器返回
UIPresentationController
用于描述目的控制器通過modal方式切換的過渡效果
實現(xiàn)其子類,可以自定義出特殊的效果
實現(xiàn)步驟:
定義過渡效果:實現(xiàn)UIPresentationController子類
目的控制器,遵循過渡協(xié)議,設(shè)置過渡控制器對象
源控制器進(jìn)行modal切換
UIPresentationController的屬性:
@property(nonatomic, retain, readonly) UIViewController *presentedViewController //目的控制器 @property(nonatomic, retain, readonly) UIViewController*presentingViewController //源控制器 - (UIView *)presentedView //目的view @property(nonatomic, readonly) UIView *containerView //源view
UIPresentationController的子類應(yīng)重寫的方法
1)init方法,可以創(chuàng)建其他輔助過渡效果的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]; //陰影按鈕的初始狀態(tài)是隱藏的 _shadowBtn.backgroundColor = [UIColor grayColor]; _shadowBtn.alpha = 0.f; } }
2)重寫presentationTransitionWillbegin方法,定義實現(xiàn)過渡效果
- (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,表示過渡效果的協(xié)助器
協(xié)助器的animateAlongsideTransition方法定義過渡期間的動畫效果
3)重寫presentationTransitionDidEnd方法,定義過渡效果后的清理工作。
特別是過渡未完成時的清理動作
- (void)presentationTransitionDidEnd:(BOOL)completed
- (void)presentationTransitionDidEnd:(BOOL)completed { if ( !completed ) { [_shadowBtn removeFromSuperview]; } }
4)重寫frameOfPresentedViewInContainerView,設(shè)置被顯示視圖的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方法,設(shè)置返回的過渡效果
- (void)dismissalTransitionWillBegin
- (void)dismissalTransitionWillBegin { id<UIViewControllerTransitionCoordinator> coordinator = self.presetingViewController.transitionCoordinator; [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonull context) { _shadowBtn.alpha = 0.01; } completion:nil]; }
6)重寫dismissalTransitionDidEnd方法,執(zhí)行清理動作
- (void)dismissalTransitionDidEnd:(BOOL)completed
- (void)dismissalTransitionDidEnd:(BOOL)completed { if ( completed ) { [_shadowView removeFromSuperview]; } }
目的控制器設(shè)置過渡效果
目的控制器遵循代理協(xié)議
@interface AMDestViewController () <UIViewControllerTransitioningDelegate>
設(shè)置代理
self.transitioningDelegate = self;
實現(xiàn)代理方法
- (UIPresetationController *) presentationControllerForPresentedViewController:(UIViewController*) presented presentingViewController:(UIViewController*) presenting sourceViewController:(UIViewController*) source { return [[AMPresentationController alloc] initWithPresentedViewController:presented presentingViewController:presenting]; }
源控制器進(jìn)行modal切換
iOS8.0開始支持這種自定義的過渡效果,主要要設(shè)置目的控制器的modalPresentationStyle為自定義。
這種切換方式,在iPhone和iPad上都是可用的。
AMDestViewController * vc = [[AMDestViewController alloct] init]; vc.modalPresentationStyle = UIModalPresentationCustom; [self presentViewController:vc animated:YES completion:nil];
當(dāng)前名稱:UIKit框架(10)自定義modal過渡效果
文章分享:http://aaarwkj.com/article12/iioegc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、網(wǎng)站排名、標(biāo)簽優(yōu)化、電子商務(wù)、企業(yè)建站、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)