美文网首页
自定义alertcontroller

自定义alertcontroller

作者: Shorebloom_59f6 | 来源:发表于2018-12-02 16:17 被阅读0次

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

相关文章

网友评论

      本文标题:自定义alertcontroller

      本文链接:https://www.haomeiwen.com/subject/xzkjcqtx.html