2018-07-20 13:25:15 4686瀏覽
今天扣丁學(xué)堂給大家主要介紹了iOS培訓(xùn)之UITableView自定義右滑刪除的代碼實(shí)現(xiàn),首先這個(gè)項(xiàng)目工程只是提供一種思路,應(yīng)對場景是需要自定義左滑刪除按鈕的樣式,因?yàn)轫?xiàng)目本身并不是修改系統(tǒng)的左滑刪除,而是自定義實(shí)現(xiàn),所以任何樣式都算使用。下面我們一起來看一下吧。
下面先說下項(xiàng)目的結(jié)構(gòu)類型
//設(shè)置代理 -(void)awakeFromNib{ [superawakeFromNib]; self.myScrollView.delegate=self; } -(void)scrollViewWillBeginDragging:(UIScrollView*)scrollView{ [selfdidBeginMove]; } -(void)scrollViewDidEndDragging:(UIScrollView*)scrollViewwillDecelerate:(BOOL)decelerate{ [scrollViewsetContentOffset:scrollView.contentOffsetanimated:YES]; [selfscrollViewDidEnd:scrollView]; } -(void)scrollViewDidScroll:(UIScrollView*)scrollView{ CGPointoffset=scrollView.contentOffset; //左邊不彈性 if(offset.x<0){ offset.x=0; [scrollViewsetContentOffset:offsetanimated:NO]; } } -(void)scrollViewWillBeginDecelerating:(UIScrollView*)scrollView{ NSLog(@"beginbegin"); [scrollViewsetContentOffset:scrollView.contentOffsetanimated:NO]; [selfscrollViewDidEnd:scrollView]; } -(void)scrollViewDidEnd:(UIScrollView*)scrollView{ [scrollViewsetContentOffset:scrollView.contentOffsetanimated:YES]; CGPointpoint=scrollView.contentOffset; if(point.x>DELETEWIDTH/2){ self.deleteLeftLayout.constant=-3; [UIViewanimateWithDuration:0.3animations:^{ [selflayoutIfNeeded]; }]; [scrollViewsetContentOffset:CGPointMake(DELETEWIDTH-3,0)animated:YES]; self.detailView.layer.cornerRadius=0; }else{ self.deleteLeftLayout.constant=0; [selflayoutIfNeeded]; [scrollViewsetContentOffset: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&¤tCell!=nil){ [currentCellhideButtonsWithAnimation]; } objc_setAssociatedObject(self.tableview,@"currentCell",self,OBJC_ASSOCIATION_ASSIGN); } } -(void)hideButtonsWithAnimation{ [self.myScrollViewsetContentOffset:CGPointMake(0,0)animated:YES]; self.detailView.layer.cornerRadius=5; self.deleteLeftLayout.constant=0; [selflayoutIfNeeded]; }
-(void)didBeginMove{ if(self.tableview){ MyTableViewCell*currentCell=objc_getAssociatedObject(self.tableview,@"currentCell"); if(currentCell!=self&¤tCell!=nil){ [currentCellhideButtonsWithAnimation]; } objc_setAssociatedObject(self.tableview,@"currentCell",self,OBJC_ASSOCIATION_ASSIGN); } }
-(void)scrollViewWillBeginDragging:(UIScrollView*)scrollView{ MyTableViewCell*currentCell=objc_getAssociatedObject(self.tableView,@"currentCell"); if(currentCell!=nil){ [currentCellhideButtonsWithAnimation]; } }