1. 根據(jù)不同的警告框風(fēng)格設(shè)置不同的警告框內(nèi)容視圖
2. 自定義alertAction,可仿照系統(tǒng)警告框action定義
3. 自定義模態(tài)視圖present動(dòng)畫
- 定義controller 繼承于UIPresentationController 添加屬性
dimmingView 用作alertController 的背景遮罩,在下面兩個(gè)方法中對遮罩進(jìn)行控制
@interface CustomAlertPresentationController : UIPresentationController
// 即將推出模態(tài)視圖時(shí)調(diào)用
-(void)presentationTransitionWillBegin
{
_dimmingView = [[UIView alloc]init];
[self.containerView addSubview:_dimmingView];
_dimmingView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.5];
_dimmingView.frame = self.containerView.frame;
__weak typeof (self) weakSelf = self;
[self.presentedViewController.transitionCoordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
weakSelf.dimmingView.bounds = weakSelf.containerView.bounds;
} completion:nil];
}
// 模態(tài)視圖即將消失時(shí)調(diào)用
-(void)dismissalTransitionWillBegin
{
__weak typeof (self) weakSelf = self;
[self.presentedViewController.transitionCoordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
weakSelf.dimmingView.alpha = 0.0;
} completion:nil];
}
- 定義模態(tài)視圖的彈出動(dòng)畫 (實(shí)現(xiàn)UIViewControllerAnimatedTransitioning協(xié)議)
@interface CustomAlertAnimation : NSObject <UIViewControllerAnimatedTransitioning>
// 動(dòng)畫時(shí)長
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
return 0.2;
}
// 動(dòng)畫
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIView *containerView = [transitionContext containerView];
UIView *toView = toVC.view;
CGFloat duration = [self transitionDuration:transitionContext];
if (toVC.isBeingPresented)
{
[containerView addSubview:toView];
toView.transform = CGAffineTransformMakeScale(1.1, 1.1);
[UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionTransitionNone animations:^{
toView.transform = CGAffineTransformMakeScale(1.0, 1.0);
} completion:^(BOOL finished) {
BOOL isCancelled = [transitionContext transitionWasCancelled];
[transitionContext completeTransition:!isCancelled];
}];
}
//Dismissal 已經(jīng)開始dismiss
if (fromVC.isBeingDismissed)
{
BOOL isCancelled = [transitionContext transitionWasCancelled];
[transitionContext completeTransition:!isCancelled];
}
}
- 實(shí)現(xiàn)專場動(dòng)畫代理(UIViewControllerTransitioningDelegate)
@interface CustomModalTransitionDelegate : NSObject <UIViewControllerTransitioningDelegate>
// present 動(dòng)畫
-(id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
return [CustomAlertAnimation new];
}
// dismiss 動(dòng)畫
-(id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
return [CustomAlertAnimation new];
}
// UIPresentationController
- (nullable UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(nullable UIViewController *)presenting sourceViewController:(UIViewController *)source NS_AVAILABLE_IOS(8_0)
{
return [[CustomAlertPresentationController alloc]initWithPresentedViewController:presented presentingViewController:presenting];
}
- 在自定義的alertcontroller 中設(shè)置轉(zhuǎn)場動(dòng)畫
alertController.modalPresentationStyle = UIModalPresentationCustom;
alertController.alertDelegate = [CustomModalTransitionDelegate new];
alertController.transitioningDelegate = alertController.alertDelegate
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。