公司有個奇葩需求。刪除按鈕帶點圓角 不止如此,還有cell之間有間隔,cell圓角,cell左右有間隔。如下圖?。。。?!
內心奔潰的我想了想了很多方法。(獲取系統自帶按鈕改圓角也試過,自定義手勢也試過)最后決定全部自定義。個人感覺這樣最合適。下面是效果圖
今天有時間,稍微說下實現方式:
這個項目工程只是提供一種思路,應對場景是 需要自定義左滑刪除按鈕的樣式。
因為項目本身并不是修改系統的左滑刪除,而是自定義實現,所以任何樣式都算使用。
下面先說下項目的結構類型
最底下自然是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_jb51.rar
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。