欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS 纯代码写个侧滑栏功能

程序员文章站 2023-12-16 21:36:28
代码原理就是使用uiview并对其移动来完成,一个twoview作为侧滑栏,一个oneview作为主界面,需要弹出侧滑栏时对twoview向右移动200,当隐藏侧滑栏时,向...

代码原理就是使用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 纯代码写个侧滑栏功能就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

上一篇:

下一篇: