国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

ios UITableView 自定義右滑刪除的實現代碼

2019-10-21 18:40:18
字體:
來源:轉載
供稿:網友

公司有個奇葩需求。刪除按鈕帶點圓角 不止如此,還有cell之間有間隔,cell圓角,cell左右有間隔。如下圖!!!!!

ios,UITableView,右滑刪除,代碼

內心奔潰的我想了想了很多方法。(獲取系統自帶按鈕改圓角也試過,自定義手勢也試過)最后決定全部自定義。個人感覺這樣最合適。下面是效果圖

ios,UITableView,右滑刪除,代碼

今天有時間,稍微說下實現方式:

這個項目工程只是提供一種思路,應對場景是 需要自定義左滑刪除按鈕的樣式。

因為項目本身并不是修改系統的左滑刪除,而是自定義實現,所以任何樣式都算使用。

下面先說下項目的結構類型

ios,UITableView,右滑刪除,代碼

最底下自然是uitableviewCell 然后放入一個scrollview 填滿整個cell (若想有左右間隔,也可以不填滿)

scrollview 中放入一個uiview 和scrollview寬高相等 作為內容視圖 。界面的所有控件視圖都添加到這個uiview中!!! 右邊就是自定義的刪除按鈕 也添加到scrollview中。這樣就能實現滑動效果了。(你也可以加2個按鈕,3個按鈕,隨你開心)

下面講下代碼

//設置代理- (void)awakeFromNib {  [super awakeFromNib];  self.myScrollView.delegate = self;}-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{  [self didBeginMove];}-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{  [scrollView setContentOffset:scrollView.contentOffset animated:YES];  [self scrollViewDidEnd:scrollView];}-(void)scrollViewDidScroll:(UIScrollView *)scrollView{  CGPoint offset = scrollView.contentOffset;  //左邊不彈性  if (offset.x < 0 ) {    offset.x = 0;    [scrollView setContentOffset:offset animated:NO];  }}-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{  NSLog(@"beginbegin");  [scrollView setContentOffset:scrollView.contentOffset animated:NO];  [self scrollViewDidEnd:scrollView];}-(void)scrollViewDidEnd:(UIScrollView *)scrollView{  [scrollView setContentOffset:scrollView.contentOffset animated:YES];  CGPoint point = scrollView.contentOffset;  if (point.x > DELETEWIDTH / 2) {    self.deleteLeftLayout.constant = -3;    [UIView animateWithDuration:0.3 animations:^{      [self layoutIfNeeded];    }];        [scrollView setContentOffset:CGPointMake(DELETEWIDTH -3 , 0) animated:YES];    self.detailView.layer.cornerRadius = 0;  }else{    self.deleteLeftLayout.constant = 0;    [self layoutIfNeeded];    [scrollView setContentOffset:CGPointMake(0, 0) animated:YES];    self.detailView.layer.cornerRadius = 5;  }}-(void)didBeginMove{  if (self.tableview) {    MyTableViewCell *currentCell = objc_getAssociatedObject(self.tableview, @"currentCell");        if (currentCell != self && currentCell != nil) {      [currentCell hideButtonsWithAnimation];    }    objc_setAssociatedObject(self.tableview, @"currentCell", self, OBJC_ASSOCIATION_ASSIGN);  }}-(void)hideButtonsWithAnimation{  [self.myScrollView setContentOffset:CGPointMake(0, 0) animated:YES];  self.detailView.layer.cornerRadius = 5;  self.deleteLeftLayout.constant = 0;  [self layoutIfNeeded];}

代碼意思大致是,scrollview停止滾動時,根據最后的位置判斷是否顯示刪除按鈕。

這樣已經實現了左右拖拽,彈出關系效果了。接下來就有一些細節部分需要注意。

1.我們觀察到,uitableviewcell只會出現一個刪除,當tableView滾動,或另一個cell左滑刪除時,前一個cell需要關閉。下面是我的解決方案

首先,當tableviewcell里的scrollview開始拖拽時,將當前的cell和tableview關聯起來。并關閉之前關聯的cell

-(void)didBeginMove{  if (self.tableview) {    MyTableViewCell *currentCell = objc_getAssociatedObject(self.tableview, @"currentCell");        if (currentCell != self && currentCell != nil) {      [currentCell hideButtonsWithAnimation];    }    objc_setAssociatedObject(self.tableview, @"currentCell", self, OBJC_ASSOCIATION_ASSIGN);  }}

然后到tableview的代理中(注意是tableview,不是cell中的scrollview)當tableview準備滾動,就直接關閉掉他關聯的cell。

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{  MyTableViewCell *currentCell = objc_getAssociatedObject(self.tableView, @"currentCell");  if (currentCell != nil) {    [currentCell hideButtonsWithAnimation];  }}

代碼修正過一版,之前那版有點小bug。

2.當cell點擊時,如果處于編輯狀態,就先關閉編輯狀態。 我的做法是直接在內容view中添加點擊手勢(同時完成點擊事件的代理),然后內部屬性判斷是否處于編輯狀態。具體代碼時間問題沒有整理到demo中。各位見諒。

先寫這么多了。感覺你們也碰不到這么奇葩的產品和美工。

下載地址:nextTableDelete.rar

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 舟山市| 阿瓦提县| 如东县| 广平县| 南京市| 台北市| 香河县| 温宿县| 建德市| 博爱县| 公主岭市| 延吉市| 郴州市| 二连浩特市| 兴化市| 瑞安市| 法库县| 文昌市| 南丹县| 白玉县| 桑日县| 克山县| 砀山县| 思茅市| 奉新县| 葫芦岛市| 汉中市| 玛沁县| 鄂温| 泊头市| 盐池县| 垣曲县| 胶州市| 昭苏县| 游戏| 宁安市| 马山县| 西青区| 乐山市| 朝阳区| 清涧县|