侧滑菜单

Posted [Michael]

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了侧滑菜单相关的知识,希望对你有一定的参考价值。

NO0 引入库

  1. platform :ios, \'7.0\'
  2. use_frameworks!
  3. target \'MGuardian\' do
  4. pod \'ECSlidingViewController\', \'~> 2.0.3\'
  5. end

侧滑动画见附件

NO1 纯代码

  1. #import "MEAppDelegate.h"
  2. #import "ECSlidingViewController.h"
  3. @interface MEAppDelegate ()
  4. @property (nonatomic, strong) ECSlidingViewController *slidingViewController;
  5. @end
  6. @implementation MEAppDelegate
  7. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  8. {
  9. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  10. UIViewController *topViewController = [[UIViewController alloc] init];
  11. UIViewController *underLeftViewController = [[UIViewController alloc] init];
  12. UIViewController *underRightViewController = [[UIViewController alloc] init];
  13. // configure top view controller
  14. UIBarButtonItem *anchorRightButton = [[UIBarButtonItem alloc] initWithTitle:@"Left" style:UIBarButtonItemStylePlain target:self action:@selector(anchorRight)];
  15. UIBarButtonItem *anchorLeftButton = [[UIBarButtonItem alloc] initWithTitle:@"Right" style:UIBarButtonItemStylePlain target:self action:@selector(anchorLeft)];
  16. topViewController.navigationItem.title = @"Layout Demo";
  17. topViewController.navigationItem.leftBarButtonItem = anchorRightButton;
  18. topViewController.navigationItem.rightBarButtonItem = anchorLeftButton;
  19. topViewController.view.backgroundColor = [UIColor whiteColor];
  20. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:topViewController];
  21. // configure under left view controller
  22. underLeftViewController.view.layer.borderWidth = 20;
  23. underLeftViewController.view.layer.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0].CGColor;
  24. underLeftViewController.view.layer.borderColor = [UIColor colorWithWhite:0.8 alpha:1.0].CGColor;
  25. underLeftViewController.edgesForExtendedLayout = UIRectEdgeTop | UIRectEdgeBottom | UIRectEdgeLeft; // don\'t go under the top view
  26. // configure under right view controller
  27. underRightViewController.view.layer.borderWidth = 20;
  28. underRightViewController.view.layer.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0].CGColor;
  29. underRightViewController.view.layer.borderColor = [UIColor colorWithWhite:0.8 alpha:1.0].CGColor;
  30. underRightViewController.edgesForExtendedLayout = UIRectEdgeTop | UIRectEdgeBottom | UIRectEdgeRight; // don\'t go under the top view
  31. // configure sliding view controller
  32. self.slidingViewController = [ECSlidingViewController slidingWithTopViewController:navigationController];
  33. self.slidingViewController.underLeftViewController = underLeftViewController;
  34. self.slidingViewController.underRightViewController = underRightViewController;
  35. // enable swiping on the top view
  36. [navigationController.view addGestureRecognizer:self.slidingViewController.panGesture];
  37. // configure anchored layout
  38. self.slidingViewController.anchorRightPeekAmount = 100.0;
  39. self.slidingViewController.anchorLeftRevealAmount = 250.0;
  40. self.window.rootViewController = self.slidingViewController;
  41. [self.window makeKeyAndVisible];
  42. return YES;
  43. }

NO2 结合 Storyboard

  1. //
  2. // HomeController.m
  3. // MGuardian
  4. //
  5. // Created by krmao on 16/5/10.
  6. // Copyright © 2016年 krmao. All rights reserved.
  7. //
  8. #import "HomeController.h"
  9. #import "MLibrary.h"
  10. //NO0 侧滑菜单:头文件
  11. #import "UIViewController+ECSlidingViewController.h"
  12. #import "METransitions.h"
  13. @interface HomeController ()
  14. //NO1 侧滑菜单:成员变量
  15. @property (nonatomic, strong) UIPanGestureRecognizer *dynamicTransitionPanGesture;
  16. @property (nonatomic, strong) METransitions *transitions;
  17. @end
  18. @implementation HomeController
  19. @synthesize label;
  20. - (void)viewDidLoad {
  21. [super viewDidLoad];
  22. //NO2 侧滑菜单:初始化
  23. [self initSlidingMenu];
  24. }
  25. //NO3 侧滑菜单:初始化
  26. -(void)initSlidingMenu{
  27. //NO3.1设置菜单边距颜色等
  28. self.slidingViewController.underLeftViewController.view.layer.borderWidth = 5;
  29. self.slidingViewController.underLeftViewController.view.layer.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0].CGColor;
  30. self.slidingViewController.underLeftViewController.view.layer.borderColor = [UIColor colorWithWhite:0.8 alpha:1.0].CGColor;
  31. self.slidingViewController.underLeftViewController.edgesForExtendedLayout = UIRectEdgeTop | UIRectEdgeBottom | UIRectEdgeLeft; // don\'t go under the top view
  32. //NO3.2设置菜单宽度
  33. self.slidingViewController.anchorRightPeekAmount = 200.0;//从左->右滑动时,左侧菜单距离主布局右边的距离
  34. //self.slidingViewController.anchorLeftRevealAmount = 100.0;//从右->左滑动时,右侧菜单距离主布局左边的距离
  35. //NO3.3:设置滑动事件
  36. //METransitions *transitions = [[METransitions alloc] init];
  37. NSDictionary *transitionData = self.transitions.all[1];//支持的位移动画 Dictionary
  38. self.transitions.dynamicTransition.slidingViewController = self.slidingViewController;
  39. //UIPanGestureRecognizer *dynamicTransitionPanGesture= [[UIPanGestureRecognizer alloc] initWithTarget:self.transitions.dynamicTransition action:@selector(handlePanGesture:)];
  40. NSString *transitionName = transitionData[@"name"];//key
  41. id<ECSlidingViewControllerDelegate> transition = transitionData[@"transition"];//value
  42. self.slidingViewController.delegate =(transition == (id)[NSNull null]) ? nil :transition;
  43. if ([transitionName isEqualToString:METransitionNameDynamic]) {
  44. self.slidingViewController.topViewAnchoredGesture = ECSlidingViewControllerAnchoredGestureTapping | ECSlidingViewControllerAnchoredGestureCustom;
  45. self.slidingViewController.customAnchoredGestures = @[self.dynamicTransitionPanGesture];
  46. [self.navigationController.view removeGestureRecognizer:self.slidingViewController.panGesture];
  47. [self.navigationController.view addGestureRecognizer:self.dynamicTransitionPanGesture];
  48. } else {
  49. self.slidingViewController.topViewAnchoredGesture = ECSlidingViewControllerAnchoredGestureTapping | ECSlidingViewControllerAnchoredGesturePanning;
  50. self.slidingViewController.customAnchoredGestures = @[];
  51. [self.navigationController.view removeGestureRecognizer:self.dynamicTransitionPanGesture];
  52. [self.navigationController.view addGestureRecognizer:self.slidingViewController.panGesture];
  53. }
  54. }
  55. //NO4 侧滑菜单:滑出右侧菜单
  56. - (void)anchorRight {
  57. [self.slidingViewController anchorTopViewToRightAnimated:YES];
  58. }
  59. //NO5 侧滑菜单:滑出左侧菜单
  60. - (void)anchorLeft {
  61. [self.slidingViewController anchorTopViewToLeftAnimated:YES];
  62. }
  63. //NO6 侧滑菜单:必须设置为成员变量
  64. - (METransitions *)transitions {
  65. if (_transitions) return _transitions;
  66. _transitions = [[METransitions alloc] init];
  67. return _transitions;
  68. }
  69. //NO7 侧滑菜单:必须设置为成员变量
  70. - (UIPanGestureRecognizer *)dynamicTransitionPanGesture {
  71. if (_dynamicTransitionPanGesture) return _dynamicTransitionPanGesture;
  72. _dynamicTransitionPanGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self.transitions.dynamicTransition action:@selector(handlePanGesture:)];
  73. return _dynamicTransitionPanGesture;
  74. }
  75. @end

Storyboard参数


等同于

  1. // configure sliding view controller
  2. UIViewController *topViewController = [[UIViewController alloc] init];
  3. UIViewController *underLeftViewController = [[UIViewController alloc] init];
  4. UIViewController *underRightViewController = [[UIViewController alloc] init];
  5. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:topViewController];
  6. self.slidingViewController = [ECSlidingViewController slidingWithTopViewController:navigationController];
  7. self.slidingViewController.underLeftViewController = underLeftViewController;
  8. self.slidingViewController.underRightViewController = underRightViewController;
  9. // enable swiping on the top view
  10. [navigationController.view addGestureRecognizer:self.slidingViewController.panGesture];
  11. self.window.rootViewController = self.slidingViewController;
  12. [self.window makeKeyAndVisible];

效果





附件列表

     

    以上是关于侧滑菜单的主要内容,如果未能解决你的问题,请参考以下文章

    侧滑菜单

    嗯嗯,一句代码就搞定 RecycleView 侧滑菜单添加头部底部加载更多

    一句代码搞定 RecycleView 侧滑菜单添加头部底部加载更多

    DrawerLayout侧滑菜单

    安卓抽屉菜单(侧滑菜单)全屏设置

    Android 使用DrawerLayout快速实现侧滑菜单