首页 > 代码库 > 自定义PresentController
自定义PresentController
分四个步骤
1 自定义presentationController 继承UIPresentationController
- (void)presentationTransitionWillBegin { self.bgView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.blurView = [[UIVisualEffectView alloc]initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]]; self.blurView.frame = self.containerView.bounds; [self.bgView insertSubview:self.blurView atIndex:0]; self.contentView = self.containerView; [self.contentView addSubview:self.presentedViewController.view]; [self.contentView addSubview:self.bgView]; [self.contentView addSubview:self.presentedView]; self.bgView.alpha = 0; self.transitionCoodinator = self.presentingViewController.transitionCoordinator; [self.transitionCoodinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) { self.bgView.alpha = 0.7; self.presentingViewController.view.transform = CGAffineTransformTranslate(self.presentingViewController.view.transform, 0, 30); } completion:nil]; } - (BOOL)shouldRemovePresentersView { return NO; } - (void)presentationTransitionDidEnd:(BOOL)completed { if (!completed) { [self.bgView removeFromSuperview]; } } - (void)dismissalTransitionWillBegin { self.transitionCoodinator = self.presentingViewController.transitionCoordinator; [self.transitionCoodinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) { self.bgView.alpha = 0; self.presentingViewController.view.transform = CGAffineTransformIdentity; } completion:nil]; } - (void)dismissalTransitionDidEnd:(BOOL)completed { if (completed) { [self.bgView removeFromSuperview]; [[self.presentingViewController childViewControllers][1] viewDidLoad]; [[[UIApplication sharedApplication] keyWindow] addSubview: self.presentingViewController.view]; } }
2.自定义percentDrivenTransition 继承 UIPercentDrivenInteractiveTransition绑定手势
- (void)wireToViewController:(UIViewController *)viewController { self.presentingVC = viewController; UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; [viewController.view addGestureRecognizer:gesture]; } -(CGFloat)completionSpeed { return 1 - self.percentComplete; } - (void)handleGesture:(UIPanGestureRecognizer *)gestureRecognizer { CGPoint translation = [gestureRecognizer translationInView:gestureRecognizer.view.superview]; switch (gestureRecognizer.state) { case UIGestureRecognizerStateBegan: self.interacting = YES; [self.presentingVC dismissViewControllerAnimated:YES completion:nil]; break; case UIGestureRecognizerStateChanged: { CGFloat fraction = translation.y / 400.0; fraction = fminf(fmaxf(fraction, 0.0), 1.0); self.shouldComplete = (fraction > 0.5); [self updateInteractiveTransition:fraction]; break; } case UIGestureRecognizerStateEnded: case UIGestureRecognizerStateCancelled: { self.interacting = NO; if (!self.shouldComplete || gestureRecognizer.state == UIGestureRecognizerStateCancelled) { [self cancelInteractiveTransition]; } else { [self finishInteractiveTransition]; } break; } default: break; } }
3自定义persentAnimation 继承NSObjec < UIViewControllerAnimatedTransitioning> present动画
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext { return 0.8f; } - (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext { UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]; CGRect screenBounds = [[UIScreen mainScreen] bounds]; CGRect finalFrame = [transitionContext finalFrameForViewController:toVC]; toVC.view.frame = CGRectOffset(finalFrame, 0, screenBounds.size.height); UIView *containerView = [transitionContext containerView]; [containerView addSubview:toVC.view]; NSTimeInterval duration = [self transitionDuration:transitionContext]; [UIView animateWithDuration:duration delay:0.0 usingSpringWithDamping:0.7 initialSpringVelocity:0.0 options:UIViewAnimationOptionCurveLinear animations:^{ toVC.view.frame = finalFrame; } completion:^(BOOL finished) { [transitionContext completeTransition:YES]; }]; }
4自定义dismissAnimation 继承NSObject<UIViewControllerAnimatedTransitioning>
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext { return 0.4f; } - (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext { UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey]; UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey] ; if ([toVC isKindOfClass:[UINavigationController class]]) { UINavigationController *vc = (UINavigationController *)toVC; vc.navigationBar.frame = CGRectMake(0, 0, vc.navigationBar.width, 64); } CGRect screenBounds = [[UIScreen mainScreen] bounds]; CGRect initFrame = [transitionContext initialFrameForViewController:fromVC]; CGRect finalFrame = CGRectOffset(initFrame, 0, screenBounds.size.height); UIView *containerView = [transitionContext containerView]; [containerView addSubview:toVC.view]; [containerView sendSubviewToBack:toVC.view]; NSTimeInterval duration = [self transitionDuration:transitionContext]; [UIView animateWithDuration:duration animations:^{ fromVC.view.frame = finalFrame; } completion:^(BOOL finished) { [transitionContext completeTransition:![transitionContext transitionWasCancelled]]; }]; }
自定义PresentController
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。