代码改变世界

iOS - UIScrollView实现抽屉菜单效果

2015-08-18 20:46  HermitCarb  阅读(561)  评论(0编辑  收藏  举报

抽屉菜单看起来比较高端的样子,但实现起来并不难。

简单效果图如下(为了简单,颜色部分使用的是纯色):

图1

 

图2

 

实现原理(本例):

    初始化一个刚好能水平容纳两个视图的UIScrollView,左侧视图为屏幕大小,右侧略窄与屏幕大小。

    初始化各种事件,在处理程序中修改UIScrollView的contentOffset。

    但是实际应用时一般还要在显示右侧视图时禁用左侧视图的内容以免出现误操作。

 

简单实现如下(只保留了关键代码):

 1 - (void)viewDidLoad
 2 {
 3     [super viewDidLoad];
 4 
 5     //右侧导航按钮
 6     UIBarButtonItem *switchButton =[[UIBarButtonItem alloc]initWithTitle:@"打开" style:UIBarButtonItemStyleDone target:self action:@selector(switchView)];
 7     self.navigationItem.rightBarButtonItem = switchButton;
 8 
 9     //左侧视图(蓝色)
10     UIView *viewLeft = [[UIView alloc] initWithFrame:self.view.frame];
11     [viewLeft setBackgroundColor:[UIColor blueColor]];
12     //当右侧视图显示时,点击左侧视图的手势
13     UITapGestureRecognizer *viewLeftTapBack = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewLeftBack)];
14     [viewLeft addGestureRecognizer:viewLeftTapBack];
15 
16     //右侧视图
17     UIView *viewRight = [[UIView alloc] initWithFrame:CGRectMake(viewLeft.frame.size.width, 0, 200, self.view.frame.size.height)];
18     [viewRight setBackgroundColor:[UIColor greenColor]];
19    
20 
21     //关键部分。左右侧视图都放在这里
22     scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
23 
24     //设置scrollview的内容大小
25     [scrollView setContentSize:CGSizeMake(viewLeft.frame.size.width + viewRight.frame.size.width, self.view.frame.size.height)];
26 
27     //设置为不可以滑动
28     [scrollView setScrollEnabled:NO];
29 
30     //设置scrollview的两个滚动条为不显示,否则可能给人误解
31     [scrollView setShowsHorizontalScrollIndicator:NO];
32     [scrollView setShowsVerticalScrollIndicator:NO];
33 
34     //设置为不反弹
35     [scrollView setBounces:NO];
36 
37     //添加左右子视图
38     [scrollView addSubview:viewLeft];
39     [scrollView addSubview:viewRight];
40     self.view = scrollView;
41 
42     //整个scrollView范围内的滑动手势,控制向左或右滑动时左右视图的滚动
43     UISwipeGestureRecognizer *viewSwipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(viewSwipeSwitch:)];
44     [viewSwipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
45     UISwipeGestureRecognizer *viewSwipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(viewSwipeSwitch:)];
46     [viewSwipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
47     [self.view addGestureRecognizer:viewSwipeLeft];
48     [self.view addGestureRecognizer:viewSwipeRight];
49 }
50 
51  
52 //显示右视图时,左侧视图点击返回事件处理
53 - (void)viewLeftBack
54 
55 {
56     float offsetX = scrollView.contentOffset.x;
57     if (offsetX == 200) {
58         [UIView animateWithDuration:0.6f animations:^{scrollView.contentOffset = CGPointMake(0, 0);}];
59         self.navigationItem.rightBarButtonItem.title = @"打开";
60     }
61 }
62 
63 
64 //左右滑动事件处理
65 - (void)viewSwipeSwitch:(UISwipeGestureRecognizer*)sender
66 {
67     if (sender.direction == UISwipeGestureRecognizerDirectionLeft)
68     {
69         [UIView animateWithDuration:0.6f animations:^{scrollView.contentOffset = CGPointMake(200, 0);}];
70         self.navigationItem.rightBarButtonItem.title = @"关闭";
71     }
72     else if(sender.direction == UISwipeGestureRecognizerDirectionRight)
73     {
74         [UIView animateWithDuration:0.6f animations:^{scrollView.contentOffset = CGPointMake(0, 0);}];
75         self.navigationItem.rightBarButtonItem.title = @"打开";
76     }
77 }
78 
79 
80 //导航栏右侧按钮的事件处理
81 - (void)switchView
82 {
83     float offsetX = scrollView.contentOffset.x;
84     if (offsetX == 0)
85     {
86         //动画,滑动显示,后同
87         [UIView animateWithDuration:0.6f animations:^{scrollView.contentOffset = CGPointMake(200, 0);}];
88         self.navigationItem.rightBarButtonItem.title = @"关闭";
89     }
90     else
91     {
92         [UIView animateWithDuration:0.6f animations:^{scrollView.contentOffset = CGPointMake(0, 0);}];
93         self.navigationItem.rightBarButtonItem.title = @"打开";
94     }
95 }
96 
97