iOS 纯代码写个侧滑栏功能

所属分类: 软件编程 / IOS 阅读数: 1956
收藏 0 赞 0 分享

代码原理就是使用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程序性能的25个方法

本篇文章主要介绍了优化iOS程序性能的25个方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解iOS开发中解析JSON中的boolean类型的数据遇到的问题

这篇文章主要介绍了详解iOS开发中解析JSON中的boolean类型的数据遇到的问题,具有一定的参考价值,有兴趣的可以了解一下。
收藏 0 赞 0 分享

iOS中模态Model视图跳转和Push视图跳转的需求实现方法

这篇文章主要介绍了iOS中模态Model视图跳转和Push视图跳转的需求实现,非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

iOS+PHP注册登录系统 iOS部分(下)

这篇文章主要介绍了iOS+PHP注册登录系统的iOS部分,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

详解iOS - ASIHTTPRequest 网络请求

本篇文章主要介绍了iOS - ASIHTTPRequest 网络请求 ,详细的介绍了 ASIHTTPRequest的使用,具有一定的参考价值,有兴趣的可以了解一下。
收藏 0 赞 0 分享

零基础学习iOS直播之采集

直播的采集由采集的设备(摄像头、话筒)不同分为视频采集和音频采集,本篇文章会分别介绍,需要的朋友一起来看下吧
收藏 0 赞 0 分享

iOS自带动画效果的实例代码

本文给大家分享ios自带动画效果的实现代码,非常不错,具有参考借鉴价值,需要的朋友参考下吧
收藏 0 赞 0 分享

零基础学习iOS直播之播放

对于直播来说,客户端主要做两件事情,推流和播放。本篇主要对播放进行详细介绍,需要的朋友一起来看下吧
收藏 0 赞 0 分享

详解iOS中集成ijkplayer视频直播框架

ijkplayer 是一款做视频直播的框架, 基于ffmpeg, 支持Android和iOS,本文将详细的讲一下在iOS中如何集成ijkplayer, 即便以前从没有接触过,按着下面做也可以集成成功!下面跟着小编一起来看下吧
收藏 0 赞 0 分享

iOS 将系统自带的button改装成上图片下文字的样子

这篇文章主要介绍了 iOS 将系统自带的button改装成上图片下文字的样子,代码是通过继承UIButton,然后再重写layoutSubviews方法,对自带的图片和titleLabel进行重新的layout。下面通过本文给大家分享下实现代码
收藏 0 赞 0 分享
查看更多