欢迎来到代码驿站!

iOS代码

当前位置:首页 > 移动开发 > iOS代码

iOS 纯代码写个侧滑栏功能

时间:2021-01-29 09:32:19|栏目:iOS代码|点击:

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

#import <UIKit/UIKit.h> 
 
@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource> 
 
@property (strong, nonatomic) NSArray 
*list; 
 
 
 
@end 
//
// 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通过按钮点击异步加载图片

栏    目:iOS代码

下一篇:iOS Webview自适应实际内容高度的4种方法详解

本文标题:iOS 纯代码写个侧滑栏功能

本文地址:http://www.codeinn.net/misctech/53315.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有