ios UITableView 自定義右滑刪除的實現(xiàn)代碼
公司有個奇葩需求。刪除按鈕帶點圓角 不止如此,還有cell之間有間隔,cell圓角,cell左右有間隔。如下圖?。。。?!
內(nèi)心奔潰的我想了想了很多方法。(獲取系統(tǒng)自帶按鈕改圓角也試過,自定義手勢也試過)最后決定全部自定義。個人感覺這樣最合適。下面是效果圖
今天有時間,稍微說下實現(xiàn)方式:
這個項目工程只是提供一種思路,應(yīng)對場景是 需要自定義左滑刪除按鈕的樣式。
因為項目本身并不是修改系統(tǒng)的左滑刪除,而是自定義實現(xiàn),所以任何樣式都算使用。
下面先說下項目的結(jié)構(gòu)類型
最底下自然是uitableviewCell 然后放入一個scrollview 填滿整個cell (若想有左右間隔,也可以不填滿)
scrollview 中放入一個uiview 和scrollview寬高相等 作為內(nèi)容視圖 。界面的所有控件視圖都添加到這個uiview中!?。?右邊就是自定義的刪除按鈕 也添加到scrollview中。這樣就能實現(xiàn)滑動效果了。(你也可以加2個按鈕,3個按鈕,隨你開心)
下面講下代碼
//設(shè)置代理 - (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停止?jié)L動時,根據(jù)最后的位置判斷是否顯示刪除按鈕。
這樣已經(jīng)實現(xiàn)了左右拖拽,彈出關(guān)系效果了。接下來就有一些細節(jié)部分需要注意。
1.我們觀察到,uitableviewcell只會出現(xiàn)一個刪除,當tableView滾動,或另一個cell左滑刪除時,前一個cell需要關(guān)閉。下面是我的解決方案
首先,當tableviewcell里的scrollview開始拖拽時,將當前的cell和tableview關(guān)聯(lián)起來。并關(guān)閉之前關(guān)聯(lián)的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準備滾動,就直接關(guān)閉掉他關(guān)聯(lián)的cell。
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{ MyTableViewCell *currentCell = objc_getAssociatedObject(self.tableView, @"currentCell"); if (currentCell != nil) { [currentCell hideButtonsWithAnimation]; } }
代碼修正過一版,之前那版有點小bug。
2.當cell點擊時,如果處于編輯狀態(tài),就先關(guān)閉編輯狀態(tài)。 我的做法是直接在內(nèi)容view中添加點擊手勢(同時完成點擊事件的代理),然后內(nèi)部屬性判斷是否處于編輯狀態(tài)。具體代碼時間問題沒有整理到demo中。各位見諒。
先寫這么多了。感覺你們也碰不到這么奇葩的產(chǎn)品和美工。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS利用AFNetworking實現(xiàn)文件上傳的示例代碼
本篇文章主要介紹了iOS利用AFNetworking實現(xiàn)文件上傳的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02iOS開發(fā)藍牙技術(shù)應(yīng)用增加無線連接功能
這篇文章主要為大家介紹了iOS開發(fā)藍牙技術(shù)應(yīng)用增加無線連接功能示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02分享一個關(guān)于Storyboard 跳轉(zhuǎn)與傳值
近日不忙,給大家分享一個關(guān)于storyboard跳轉(zhuǎn)傳值的相關(guān)知識,感興趣的朋友一起看看吧2015-12-12iOS應(yīng)用中發(fā)送HTTP的get請求以及HTTP異步請求的方法
這篇文章主要介紹了iOS應(yīng)用中發(fā)送HTTP的get請求以及HTTP異步請求的方法,代碼為傳統(tǒng)的Objective-C語言,說明都簡單地融入于注釋之中,需要的朋友可以參考下2016-02-02兼容iOS 10 升級xcode8出現(xiàn)的問題及一些適配問題的解決方案
這篇文章主要介紹了兼容iOS 10 升級xcode8出現(xiàn)的問題及一些適配問題的解決方案,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09