IOS8 's Uipresentationcontroller

Source: Internet
Author: User

This article reprinted to http://kyfxbl.iteye.com/blog/2147888

Starting with iOS8, the jump effects between controllers need to be implemented with a new API Uipresentationcontroller. For example, to achieve a special effect: Display a modal window, size and position is custom, mask on the original page. Before IOS8, you can set the Superview frame in the viewwillappear:

OBJC Code
  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 Code
  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 ( 540);
  11. }

But the above code, in the iOS8 is no longer effective, to use Uipresentationcontroller to achieve

First, make it clear that the style and jump effects from controller a->b,b are controlled by B. It's just that it was done directly in the controller's life cycle approach, and now there's a dedicated API to do it. This design is also reasonable, otherwise if from a can jump to B and C, but the style and special effects, you can only by setting the instance variable in a to distinguish, error-prone is also very awkward. So it's reasonable to have the jump behavior controlled by the target controller.

However, the documentation for this set of APIs is not complete, and subsequent SDK upgrades may evolve. The implementation steps are described below:

Target Controller implements specific protocol

First, the target controller implements a specific protocol to create a Uipresentationcontroller

OBJC Code
    1. @interface Ylscheckoutsignaturecontroller:uiviewcontroller<uiscrollviewdelegate, Uiviewcontrollertransitioningdelegate>

OBJC Code
    1. Self.transitioningdelegate = self;
OBJC Code
    1. -(Uipresentationcontroller *) Presentationcontrollerforpresentedviewcontroller: (Uiviewcontroller *) presented Presentingviewcontroller: (Uiviewcontroller *) presenting Sourceviewcontroller: (Uiviewcontroller *) source
    2. {
    3. return [[Ylsmainpresentationcontroller alloc] initwithpresentedviewcontroller:presented presentingviewcontroller: Presenting];
    4. }

The iOS system calls this method when the condition is met, so you can instantiate a custom Uipresentationcontroller subclass, define the style of the jump and customize the effect Uipresentationcontroller

Then we will implement the custom Uipresentationcontroller, the following example code, the implementation of a custom frame to display a modal page, and a translucent background to cover the original page

OBJC Code
  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

The code is really a bit more complicated than it used to be, but in fact each life cycle approach is relatively clear, and the developer can control the granularity more granular. For example, set presented frame, there is a special method, as long as the return to CGRect on it, or more convenient

The original Viewcontroller initiates the jump action

With the previous 2 steps, when a custom jump occurs, you can control the style and jump behavior in a very granular manner. The next step is to initiate the jump action by the original controller (presenting view Controller):

OBJC Code
  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. }

The key is to set Modalpresentationstyle to Uimodalpresentationcustom, and then when the Presentviewcontroller method is called, The iOS system will create an instance of Uipresentationcontroller to control the behavior of the jump

IOS8 's Uipresentationcontroller

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.