自定义alertcontroller

1. 根据不同的警告框风格设置不同的警告框内容视图

2. 自定义alertAction,可仿照系统警告框action定义

3. 自定义模态视图present动画

  1. 定义controller 继承于UIPresentationController 添加属性dimmingView 用作alertController 的背景遮罩,在下面两个方法中对遮罩进行控制
    @interface CustomAlertPresentationController : UIPresentationController
// 即将推出模态视图时调用
-(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];
}

// 模态视图即将消失时调用
-(void)dismissalTransitionWillBegin
{
 __weak typeof (self) weakSelf = self;
 [self.presentedViewController.transitionCoordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
     weakSelf.dimmingView.alpha = 0.0;
 } completion:nil];
}
  1. 定义模态视图的弹出动画 (实现UIViewControllerAnimatedTransitioning协议)
    @interface CustomAlertAnimation : NSObject <UIViewControllerAnimatedTransitioning>
// 动画时长
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
  return 0.2;
}

// 动画
- (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 已经开始dismiss
  if (fromVC.isBeingDismissed)
  {
      BOOL isCancelled = [transitionContext transitionWasCancelled];
      [transitionContext completeTransition:!isCancelled];
  }
}
  1. 实现专场动画代理(UIViewControllerTransitioningDelegate)
    @interface CustomModalTransitionDelegate : NSObject <UIViewControllerTransitioningDelegate>
// present 动画
-(id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
    return [CustomAlertAnimation new];
   
}

// dismiss 动画
-(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 中设置转场动画
alertController.modalPresentationStyle = UIModalPresentationCustom;
alertController.alertDelegate = [CustomModalTransitionDelegate new];
alertController.transitioningDelegate = alertController.alertDelegate
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,282评论 3 119
  • 冯歆跟许畅聊着聊着,就会突然被什么提醒一下,你应该以一位姐姐的立场和口吻面对这个大男生。因为她显然会忘记这个自己在...
    如贝衔珠阅读 465评论 1 1
  • 我爱上了一座城 爱这座城的空气 它慷慨地滋养着我爱的人,给予他生命 爱这座城的阳光 它温润地照耀着我爱的人,给予他...
    末夏诗雨阅读 468评论 4 9
  • 无眠之夜后重新整理, 再一次出发。 旅途和人生一样, 走走停停, 因机缘和认知来来去去, 一切都在不断地分离和组合...
    相守于心阅读 309评论 0 0

友情链接更多精彩内容