从iOS8开始,controller之间的跳转特效,需要用新的API UIPresentationController来实现。比如希望实现这样一个特效:显示一个模态窗口,大小和位置是自定义的,遮罩在原来的页面上。在iOS8之前,可以在viewWillAppear里设置superview的frame:


Objc代码 iOS8的UIPresentationController_c代码


  1. - (void)presentModal:(NSDictionary*)result
  2. {
  3. YLSCheckoutSignatureController *controller = [[YLSCheckoutSignatureController alloc] initWithModel:result];

  4. if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
  5. controller.modalPresentationStyle = UIModalPresentationCustom;
  6. }else{
  7. controller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
  8. controller.modalPresentationStyle = UIModalPresentationFormSheet;
  9. }

  10. [self presentViewController:controller animated:YES completion:nil];
  11. }



Objc代码 iOS8的UIPresentationController_c代码


  1. -(void) viewWillAppear:(BOOL)animated
  2. {
  3. // in iOS8, handle by UIPresentationController
  4. if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
  5. return;
  6. }

  7. self.view.superview.layer.cornerRadius = 10;
  8. self.view.superview.layer.borderColor = [UIColor darkGrayColor].CGColor;
  9. self.view.superview.clipsToBounds = YES;
  10. self.view.superview.frame = CGRectMake(62, 114, 900, 540);
  11. }


但是以上的代码,在iOS8里就不再生效了,要用UIPresentationController来实现

首先明确一点,从Controller A->B,B的样式和跳转特效,还是由B来控制的。只不过以前是直接在Controller的生命周期方法里操作,而现在有专门的API来完成而已。这种设计也是合理的,否则如果从A可以跳转到B和C,但是样式和特效不一样,就只能通过在A里面设置实例变量来区分了,容易出错也很别扭。所以把跳转的行为由目标Controller来控制是很合理的

不过这组API的文档不太全,后续SDK升级可能会逐渐完善。以下介绍实现步骤:

目标Controller实现特定protocol

首先目标Controller要实现特定的协议,创建一个UIPresentationController


Objc代码 iOS8的UIPresentationController_c代码


  1. @interface YLSCheckoutSignatureController : UIViewController<UIScrollViewDelegate, UIViewControllerTransitioningDelegate>



Objc代码 iOS8的UIPresentationController_c代码


  1. self.transitioningDelegate = self;

Objc代码 iOS8的UIPresentationController_c代码


  1. - (UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source
  2. {
  3. return [[YLSMainPresentationController alloc] initWithPresentedViewController:presented presentingViewController:presenting];
  4. }


当条件满足时,iOS系统会调用这个方法,于是可以实例化自定义的UIPresentationController子类,定义跳转的样式和特效

自定义UIPresentationController

然后就要实现自定义的UIPresentationController,下面这段实例代码,实现居中展示一个自定义frame的模态页面,同时有半透明背景遮住原来的页面


Objc代码 iOS8的UIPresentationController_c代码


  1. @implementation YLSMainPresentationController

  2. {
  3. UIView *dimmingView;
  4. }

  5. -(id) initWithPresentedViewController:(UIViewController *)presentedViewController presentingViewController:(UIViewController *)presentingViewController
  6. {
  7. self = [super initWithPresentedViewController:presentedViewController presentingViewController:presentingViewController];
  8. if(self){

  9. dimmingView = [[UIView alloc] init];
  10. dimmingView.backgroundColor = [UIColor grayColor];
  11. dimmingView.alpha = 0.0;
  12. }
  13. return self;
  14. }

  15. - (void)presentationTransitionWillBegin
  16. {
  17. dimmingView.frame = self.containerView.bounds;
  18. [self.containerView addSubview:dimmingView];
  19. [self.containerView addSubview:self.presentedView];

  20. id<UIViewControllerTransitionCoordinator> coordinator = self.presentingViewController.transitionCoordinator;

  21. [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
  22. dimmingView.alpha = 0.5;
  23. } completion:nil];
  24. }

  25. - (void)presentationTransitionDidEnd:(BOOL)completed
  26. {
  27. if(!completed){
  28. [dimmingView removeFromSuperview];
  29. }
  30. }

  31. - (void)dismissalTransitionWillBegin
  32. {
  33. id<UIViewControllerTransitionCoordinator> coordinator = self.presentingViewController.transitionCoordinator;

  34. [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
  35. dimmingView.alpha = 0.0;
  36. } completion:nil];
  37. }

  38. - (void)dismissalTransitionDidEnd:(BOOL)completed
  39. {
  40. if(completed){
  41. [dimmingView removeFromSuperview];
  42. }
  43. }

  44. - (CGRect)frameOfPresentedViewInContainerView
  45. {
  46. return CGRectMake(62.f, 114.f, 900.f, 540.f);
  47. }

  48. @end


代码确实比以前复杂了一点,但是其实每个生命周期方法都是比较明确的,开发者可控的粒度也更细了。比如设置presented frame,就有专门的方法,只要返回CGRect就可以了,还是比较方便的

原始的ViewController发起跳转动作

经过前面2步,当自定义跳转发生时,就可以很细致地控制样式和跳转行为。接下来就是由原始controller(presenting view controller)来发起跳转动作:


Objc代码 iOS8的UIPresentationController_c代码


  1. - (void)presentModal:(NSDictionary*)result
  2. {
  3. YLSCheckoutSignatureController *controller = [[YLSCheckoutSignatureController alloc] initWithModel:result];

  4. if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
  5. controller.modalPresentationStyle = UIModalPresentationCustom;
  6. }else{
  7. controller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
  8. controller.modalPresentationStyle = UIModalPresentationFormSheet;
  9. }

  10. [self presentViewController:controller animated:YES completion:nil];
  11. }


关键是设置modalPresentationStyle为UIModalPresentationCustom,然后当presentViewController方法调用时,iOS系统就会创建出UIPresentationController的实例,来控制跳转的行为