自定義alertcontroller

1. 根據(jù)不同的警告框風(fēng)格設(shè)置不同的警告框內(nèi)容視圖

2. 自定義alertAction,可仿照系統(tǒng)警告框action定義

3. 自定義模態(tài)視圖present動(dòng)畫

  1. 定義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];
}
  1. 定義模態(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];
  }
}
  1. 實(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];
}
  1. 在自定義的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ù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1、通過CocoaPods安裝項(xiàng)目名稱項(xiàng)目信息 AFNetworking網(wǎng)絡(luò)請求組件 FMDB本地?cái)?shù)據(jù)庫組件 SD...
    陽明AI閱讀 16,211評論 3 119
  • 馮歆跟許暢聊著聊著,就會(huì)突然被什么提醒一下,你應(yīng)該以一位姐姐的立場和口吻面對這個(gè)大男生。因?yàn)樗@然會(huì)忘記這個(gè)自己在...
    如貝銜珠閱讀 462評論 1 1
  • 我愛上了一座城 愛這座城的空氣 它慷慨地滋養(yǎng)著我愛的人,給予他生命 愛這座城的陽光 它溫潤地照耀著我愛的人,給予他...
    末夏詩雨閱讀 463評論 4 9
  • 無眠之夜后重新整理, 再一次出發(fā)。 旅途和人生一樣, 走走停停, 因機(jī)緣和認(rèn)知來來去去, 一切都在不斷地分離和組合...
    相守于心閱讀 299評論 0 0

友情鏈接更多精彩內(nèi)容