2018-07-20 13:25:15 1623浏览
今天扣丁学堂给大家主要介绍了iOS培训之UITableView自定义右滑删除的代码实现,首先这个项目工程只是提供一种思路,应对场景是需要自定义左滑删除按钮的样式,因为项目本身并不是修改系统的左滑删除,而是自定义实现,所以任何样式都算使用。下面我们一起来看一下吧。
下面先说下项目的结构类型
//设置代理
-(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];
}
}