服务器之家

服务器之家 > 正文

iOS 纯代码写个侧滑栏功能

时间:2021-04-15 16:04     来源/作者:键盘舞者113

代码原理就是使用UIView并对其移动来完成,一个twoView作为侧滑栏,一个oneView作为主界面,需要弹出侧滑栏时对twoView向右移动200,当隐藏侧滑栏时,向左移动200就行了,twoVIew初始的x地址为-200。

?
1
2
3
4
5
6
7
8
9
10
#import <UIKit/UIKit.h>
 
@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
 
@property (strong, nonatomic) NSArray
*list;
 
 
 
@end
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//
// ViewController.m
// First
//
// Created by shanreal-iOS on 17/10/16.
// Copyright © 2017年 shanreal.LongZhenHao. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property(nonatomic,strong)UIView* oneView;
@property(nonatomic,strong)UIView* twoView;
@property(nonatomic,assign)Boolean isShow;
@property(nonatomic,strong)UIButton* btn_back;
@property(nonatomic,strong)UIButton* btn_show;
@end
@implementation ViewController
- (void)viewDidLoad {
 [super viewDidLoad];
 // Do any additional setup after loading the view.
 [self initLeftMenu];
}
-(void)initLeftMenu{
 //self.view.backgroundColor = [UIColor whiteColor];
 _isShow = NO;
 _oneView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width+200, self.view.frame.size.height)];
 _oneView.backgroundColor = [UIColor whiteColor];
 
 _twoView=[[UIView alloc]initWithFrame:CGRectMake(-200, 0, 200, self.view.frame.size.height)];
 _twoView.backgroundColor = [UIColor lightGrayColor];
 
 [self.view addSubview:_oneView];
 [self.view addSubview:_twoView];
 
 _oneView.userInteractionEnabled=YES;
 
 UITapGestureRecognizer *tapGesture1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(backClick)];
 [_oneView addGestureRecognizer:tapGesture1];
 
 _btn_show = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2-75, self.view.frame.size.height/2-15, 150, 30)];
 _btn_show.backgroundColor = [UIColor whiteColor];
 [_btn_show setTitle:@"弹出侧滑栏" forState:UIControlStateNormal];
 [_btn_show setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
 [_btn_show addTarget:self action:@selector(oneClick) forControlEvents:UIControlEventTouchUpInside];
 [self.oneView addSubview:_btn_show];
 
 _btn_back = [[UIButton alloc]initWithFrame:CGRectMake(20, 100, 150, 30)];
 _btn_back.backgroundColor = [UIColor whiteColor];
 [_btn_back setTitle:@"返回" forState:UIControlStateNormal];
 [_btn_back setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
 [_btn_back addTarget:self action:@selector(twoClick) forControlEvents:UIControlEventTouchUpInside];
 [self.twoView addSubview:_btn_back];
 
 
 
}
-(void)oneClick{
 [UIView animateWithDuration:0.7 animations:^{
  //[_oneView setTransform:CGAffineTransformMakeTranslation(200, 0)];
  [_twoView setTransform:CGAffineTransformMakeTranslation(200, 0)];
 }];
 _isShow = YES;
}
-(void)twoClick{
 [UIView animateWithDuration:0.7 animations:^{
  //[_oneView setTransform:CGAffineTransformMakeTranslation(-200, 0)];
  [_twoView setTransform:CGAffineTransformMakeTranslation(-200, 0)];
 }];
 _isShow = NO;
}
-(void)backClick{
 if(_isShow == YES)
  [self performSelector:@selector(twoClick)];
}
@end

以上这篇iOS 纯代码写个侧滑栏功能就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/z979451341/article/details/79045191

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部