react-native-navigation的迁移库

MMDrawerVisualState.m 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright (c) 2013 Mutual Mobile (http://mutualmobile.com/)
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. #import "MMDrawerVisualState.h"
  21. #import <QuartzCore/QuartzCore.h>
  22. @implementation MMDrawerVisualState
  23. +(MMDrawerControllerDrawerVisualStateBlock)slideAndScaleVisualStateBlock{
  24. MMDrawerControllerDrawerVisualStateBlock visualStateBlock =
  25. ^(MMDrawerController * drawerController, MMDrawerSide drawerSide, CGFloat percentVisible){
  26. CGFloat minScale = .90;
  27. CGFloat scale = minScale + (percentVisible*(1.0-minScale));
  28. CATransform3D scaleTransform = CATransform3DMakeScale(scale, scale, scale);
  29. CGFloat maxDistance = 50;
  30. CGFloat distance = maxDistance * percentVisible;
  31. CATransform3D translateTransform = CATransform3DIdentity;
  32. UIViewController * sideDrawerViewController;
  33. if(drawerSide == MMDrawerSideLeft) {
  34. sideDrawerViewController = drawerController.leftDrawerViewController;
  35. translateTransform = CATransform3DMakeTranslation((maxDistance-distance), 0.0, 0.0);
  36. }
  37. else if(drawerSide == MMDrawerSideRight){
  38. sideDrawerViewController = drawerController.rightDrawerViewController;
  39. translateTransform = CATransform3DMakeTranslation(-(maxDistance-distance), 0.0, 0.0);
  40. }
  41. [sideDrawerViewController.view.layer setTransform:CATransform3DConcat(scaleTransform, translateTransform)];
  42. [sideDrawerViewController.view setAlpha:percentVisible];
  43. };
  44. return visualStateBlock;
  45. }
  46. +(MMDrawerControllerDrawerVisualStateBlock)slideVisualStateBlock{
  47. return [self parallaxVisualStateBlockWithParallaxFactor:1.0];
  48. }
  49. +(MMDrawerControllerDrawerVisualStateBlock)swingingDoorVisualStateBlock{
  50. MMDrawerControllerDrawerVisualStateBlock visualStateBlock =
  51. ^(MMDrawerController * drawerController, MMDrawerSide drawerSide, CGFloat percentVisible){
  52. UIViewController * sideDrawerViewController;
  53. CGPoint anchorPoint;
  54. CGFloat maxDrawerWidth = 0.0;
  55. CGFloat xOffset;
  56. CGFloat angle = 0.0;
  57. if(drawerSide==MMDrawerSideLeft){
  58. sideDrawerViewController = drawerController.leftDrawerViewController;
  59. anchorPoint = CGPointMake(1.0, .5);
  60. maxDrawerWidth = MAX(drawerController.maximumLeftDrawerWidth,drawerController.visibleLeftDrawerWidth);
  61. xOffset = -(maxDrawerWidth/2.0) + (maxDrawerWidth)*percentVisible;
  62. angle = -M_PI_2+(percentVisible*M_PI_2);
  63. }
  64. else {
  65. sideDrawerViewController = drawerController.rightDrawerViewController;
  66. anchorPoint = CGPointMake(0.0, .5);
  67. maxDrawerWidth = MAX(drawerController.maximumRightDrawerWidth,drawerController.visibleRightDrawerWidth);
  68. xOffset = (maxDrawerWidth/2.0) - (maxDrawerWidth)*percentVisible;
  69. angle = M_PI_2-(percentVisible*M_PI_2);
  70. }
  71. [sideDrawerViewController.view.layer setAnchorPoint:anchorPoint];
  72. [sideDrawerViewController.view.layer setShouldRasterize:YES];
  73. [sideDrawerViewController.view.layer setRasterizationScale:[[UIScreen mainScreen] scale]];
  74. CATransform3D swingingDoorTransform = CATransform3DIdentity;
  75. if (percentVisible <= 1.f) {
  76. CATransform3D identity = CATransform3DIdentity;
  77. identity.m34 = -1.0/1000.0;
  78. CATransform3D rotateTransform = CATransform3DRotate(identity, angle, 0.0, 1.0, 0.0);
  79. CATransform3D translateTransform = CATransform3DMakeTranslation(xOffset, 0.0, 0.0);
  80. CATransform3D concatTransform = CATransform3DConcat(rotateTransform, translateTransform);
  81. swingingDoorTransform = concatTransform;
  82. }
  83. else{
  84. CATransform3D overshootTransform = CATransform3DMakeScale(percentVisible, 1.f, 1.f);
  85. NSInteger scalingModifier = 1.f;
  86. if (drawerSide == MMDrawerSideRight) {
  87. scalingModifier = -1.f;
  88. }
  89. overshootTransform = CATransform3DTranslate(overshootTransform, scalingModifier*maxDrawerWidth/2, 0.f, 0.f);
  90. swingingDoorTransform = overshootTransform;
  91. }
  92. [sideDrawerViewController.view.layer setTransform:swingingDoorTransform];
  93. };
  94. return visualStateBlock;
  95. }
  96. +(MMDrawerControllerDrawerVisualStateBlock)parallaxVisualStateBlockWithParallaxFactor:(CGFloat)parallaxFactor{
  97. MMDrawerControllerDrawerVisualStateBlock visualStateBlock =
  98. ^(MMDrawerController * drawerController, MMDrawerSide drawerSide, CGFloat percentVisible){
  99. NSParameterAssert(parallaxFactor >= 1.0);
  100. CATransform3D transform = CATransform3DIdentity;
  101. UIViewController * sideDrawerViewController;
  102. if(drawerSide == MMDrawerSideLeft) {
  103. sideDrawerViewController = drawerController.leftDrawerViewController;
  104. CGFloat distance = MAX(drawerController.maximumLeftDrawerWidth,drawerController.visibleLeftDrawerWidth);
  105. if (percentVisible <= 1.f) {
  106. transform = CATransform3DMakeTranslation((-distance)/parallaxFactor+(distance*percentVisible/parallaxFactor), 0.0, 0.0);
  107. }
  108. else{
  109. transform = CATransform3DMakeScale(percentVisible, 1.f, 1.f);
  110. transform = CATransform3DTranslate(transform, drawerController.maximumLeftDrawerWidth*(percentVisible-1.f)/2, 0.f, 0.f);
  111. }
  112. }
  113. else if(drawerSide == MMDrawerSideRight){
  114. sideDrawerViewController = drawerController.rightDrawerViewController;
  115. CGFloat distance = MAX(drawerController.maximumRightDrawerWidth,drawerController.visibleRightDrawerWidth);
  116. if(percentVisible <= 1.f){
  117. transform = CATransform3DMakeTranslation((distance)/parallaxFactor-(distance*percentVisible)/parallaxFactor, 0.0, 0.0);
  118. }
  119. else{
  120. transform = CATransform3DMakeScale(percentVisible, 1.f, 1.f);
  121. transform = CATransform3DTranslate(transform, -drawerController.maximumRightDrawerWidth*(percentVisible-1.f)/2, 0.f, 0.f);
  122. }
  123. }
  124. [sideDrawerViewController.view.layer setTransform:transform];
  125. };
  126. return visualStateBlock;
  127. }
  128. @end