一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - IOS - ios仿側(cè)邊抽屜效果實(shí)現(xiàn)代碼

ios仿側(cè)邊抽屜效果實(shí)現(xiàn)代碼

2021-01-17 20:30菜鳥Alex IOS

這篇文章主要為大家詳細(xì)介紹了ios仿側(cè)邊抽屜效果實(shí)現(xiàn)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

效果圖如下

ios仿側(cè)邊抽屜效果實(shí)現(xiàn)代碼

代碼實(shí)現(xiàn)以及思路下面分析:
代碼創(chuàng)建導(dǎo)航控制器
appdelegate.m中

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#import "appdelegate.h"
#import "viewcontroller.h"
@interface appdelegate ()
 
@end
 
@implementation appdelegate
 
 
- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions {
 
  self.window = [[uiwindow alloc] initwithframe:[uiscreen mainscreen].bounds];
  viewcontroller * vc = [[viewcontroller alloc] init];
//必須要初始化導(dǎo)航控制器的根控制器
  uinavigationcontroller * nav = [[uinavigationcontroller alloc] initwithrootviewcontroller:vc];
  self.window.rootviewcontroller = nav;
  [self.window makekeyandvisible];
  return yes;
}

viewcontroller.m中

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//
// viewcontroller.m
// pbsliedmenu
//
// created by 裴波波 on 16/4/21.
// copyright © 2016年 裴波波. all rights reserved.
//
 
#import "viewcontroller.h"
#define kscreenh [uiscreen mainscreen].bounds.size.height
#define kscreenw [uiscreen mainscreen].bounds.size.width
#define knavw 64
@interface viewcontroller ()<uitableviewdelegate,uitableviewdatasource>
 
@property (nonatomic, strong) uitableview *tableview;
/** 記錄是否打開側(cè)邊欄 */
@property (nonatomic, assign) bool openslide;
/** 側(cè)欄按鈕 */
@property (nonatomic, strong) uibarbuttonitem *btnleft;
 
@end

用一個bool值來記錄左側(cè)view是打開還是關(guān)閉狀態(tài).每次點(diǎn)擊都要改變記錄tableview狀態(tài)的值
用屬性保存 側(cè)欄 按鈕,用來當(dāng)左側(cè)tableview正在彈出或者收回執(zhí)行動畫過程中禁用.

?
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
@implementation viewcontroller
 
#pragma mark - 選中某個cell代理方法
-(void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath{
 
  uitableviewcell * cell = [tableview cellforrowatindexpath:indexpath];
  nslog(@"%@",cell.textlabel.text);
  //選中cell后立即取消選中
  [tableview deselectrowatindexpath:indexpath animated:yes];
}
 
 
#pragma mark - tableview數(shù)據(jù)源
-(nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section{
  
  return 20;
}
 
-(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath{
  static nsstring * id = @"cell";
  uitableviewcell * cell = [tableview dequeuereusablecellwithidentifier:id forindexpath:indexpath];
  cell.textlabel.text = [nsstring stringwithformat:@"我是%zd",indexpath.row];
  cell.backgroundcolor = [uicolor orangecolor];
  return cell;
}
 
- (void)viewdidload {
  
  [super viewdidload];
  self.view.backgroundcolor = [uicolor whitecolor];
  [self initleftbarbutton];
  //注冊cell
  [self.tableview registerclass:[uitableviewcell class] forcellreuseidentifier:@"cell"];
}

注意:注冊cell的同時調(diào)用了 self.tableview 則調(diào)用了懶加載,此時tableview已經(jīng)創(chuàng)建了.必須要先創(chuàng)建,否則有一個小bug就是,當(dāng)tableview第一次彈出的時候會從屏幕的(0,0)點(diǎn)彈出,而不是整個tableview從左側(cè)彈出.

?
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
#pragma mark - 初始化側(cè)欄按鈕
-(void)initleftbarbutton{
  
  uibutton * btnleft = [[uibutton alloc] init];
  btnleft.frame = cgrectmake(0, 0, 90, 40);
  [btnleft settitle:@"側(cè)欄" forstate:uicontrolstatenormal];
  [btnleft settitlecolor:[uicolor blackcolor] forstate:uicontrolstatenormal];
  [btnleft addtarget:self action:@selector(didleftbtn) forcontrolevents:uicontroleventtouchupinside];
  self.navigationitem.leftbarbuttonitem = [[uibarbuttonitem alloc] initwithcustomview:btnleft];
  self.btnleft = self.navigationitem.leftbarbuttonitem;
}
 
#pragma mark - 懶加載tableview
-(uitableview *)tableview{
  
  if (_tableview == nil) {
    _tableview = [[uitableview alloc] init];
    _tableview.delegate = self;
    _tableview.datasource = self;
    _tableview.backgroundcolor = [uicolor orangecolor];
    //第一次點(diǎn)擊tableview從左上角彈出,優(yōu)化方案--先創(chuàng)建出tableview
    cgfloat hight = kscreenh;
    cgfloat x = 0;
    cgfloat y = knavw;
    cgfloat width = 0;
    _tableview.frame = cgrectmake(x, y, width, hight);
    //取消顯示豎直滾動條
    _tableview.showsverticalscrollindicator = no;
  }
  return _tableview;
}

懶加載的時候直接創(chuàng)建tableview,讓其寬度 == 0 即可.

?
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
#pragma mark - 點(diǎn)擊側(cè)欄按鈕彈出tableview
-(void)didleftbtn{
  
  //禁用button等待動畫執(zhí)行完畢再啟用button
  self.btnleft.enabled = no;
  cgfloat hight = kscreenh;
  cgfloat x = 0;
  cgfloat y = knavw;
  if (!self.openslide) {
    //添加動畫
    [uiview animatewithduration:0.3 animations:^{
      cgfloat width = kscreenw / 3;
      self.tableview.frame = cgrectmake(x, y, width, hight);
    }];
    [self.view addsubview:self.tableview];
  } else {
    [uiview animatewithduration:0.3 animations:^{
      cgfloat width = 0;
      self.tableview.frame = cgrectmake(x, y, width, hight);
    }];
  }
  //執(zhí)行完畢動畫 取消禁用button
  [self performselector:@selector(setbtnleftenabled) withobject:nil afterdelay:0.3];
  //監(jiān)視側(cè)欄是否打開
  if (self.openslide == yes) {
    self.openslide = no;
  } else {
    self.openslide = yes;
  }
}

點(diǎn)擊 側(cè)欄 按鈕彈出tableview,此過程中讓其動畫執(zhí)行,不會顯得生硬.讓tableview的寬度從0---> 屏幕寬度的三分之一
記錄tableview打開的狀態(tài).
執(zhí)行動畫的過程中禁用 側(cè)欄 按鈕,由于代碼執(zhí)行時間的瞬間完成的,動畫執(zhí)行時間是0.3s,則延遲0.3s取消禁用 側(cè)欄 按鈕.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//不用反復(fù)創(chuàng)建tableview
//#pragma mark - 移除tableview
//-(void)removesliedview{
//
//  [self.tableview removefromsuperview];
//  self.btnleft.enabled = yes;
//}
#pragma mark - 動畫執(zhí)行完畢啟用"側(cè)欄"按鈕
-(void)setbtnleftenabled{
  
  self.btnleft.enabled = yes;
  //動畫執(zhí)行完畢讓第一個cell顯示在最頂端
  self.tableview.contentoffset = cgpointmake(0, 0);
}
 
 
- (void)didreceivememorywarning {
  [super didreceivememorywarning];
  // dispose of any resources that can be recreated.
}
 
@end

之前犯過一個錯誤就是點(diǎn)擊 側(cè)欄 按鈕創(chuàng)建tableview,再點(diǎn)擊 銷毀 tableview,這樣比較耗性能.通過懶加載先創(chuàng)建tableview,收回tableview的時候讓其寬度 == 0 即可.
上圖演示的可以看出,當(dāng)滑動tableview的時候,再次點(diǎn)擊進(jìn)去tableview還是滑動的位置,不會恢復(fù)到開始 下標(biāo)為 0 的cell為最上面顯示的cell.優(yōu)化方案:讓tableview的偏移contentoffset等于 0即可.代碼不能寫在 彈出tableview 與 收回 tableview的動畫代碼中,因?yàn)檫@樣會讓人看出來.寫在動畫執(zhí)行完畢后的代碼中.

源代碼地址:https://git.oschina.net/alexpei/pbsliedmenu.git

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 东北美女野外bbwbbw免费 | julianann办公室 | 天天做天天爱天天一爽一毛片 | 91免费在线| 国亚洲欧美日韩精品 | 国产伦精品一区二区三区免费观看 | 贤妻良母电影日本 | 日本护士厕所xxx | 精品在线91| 国产成人精品高清在线 | 久久精品国产在热亚洲 | 亚洲欧美影院 | 奇米777四色精品综合影院 | 久久99精品久久久久久园产越南 | 国产精品林美惠子在线观看 | 亚洲成人免费观看 | 人妖欧美一区二区三区四区 | 99er视频| 国产在线观看精品 | a级免费观看| 国产精品一二三 | 国产主播99 | 午夜伦伦电影理论片费看 | 青青草综合网 | 非洲黑女人性xxxx | 午夜宅男网 | 大香焦在线 | 第一福利在线视频 | 青草福利视频 | 亚洲第五色综合网啪啪 | juy799大岛优香在线观看 | 免费在线观看网址入口 | 2022日韩理论片在线观看 | 福利入口在线观看 | 高清视频在线播放 | 亚洲日本在线观看网址 | 国产成人在线综合 | 射逼网 | 国产一级黄毛片 | 91短视频在线观看2019 | 免费看成年视频网页 |