(原創博客,如將轉載煩請備注出處)
iOS版本的QQ左滑會出現‘置頂’、‘刪除’等多個按鈕,產生左劃動作cell會向左滑動,在cell已經是出現多個操作按鈕的情況下點擊或產生右滑動作cell都會復位。
以前我的第一想法和做法是:自定義cell,在cell上放一個scrollview,根據手勢來判別相應操作,加上一些動畫,可是這樣的做法很難達到很和諧,如系統動畫那般絲滑。
下面我要給大家介紹PTLestSlideTableView。PTLestSlideTableView是繼承了UITableView,并開放出按鈕相關配置接口。
直接定義一個成員屬性PTLeftSlideTableView *tableView
@property (nonatomic, strong) PTLeftSlideTableView *tableView;
在初始化的時候可以定義好相關屬性
- (PTLeftSlideTableView *)tableView{
?? ? ?? if (!_tableView) {
?? ? ? ? ? ? ?? _tableView = [[PTLeftSlideTableView alloc]initWithFrame:self.view.frame];
??? ? ? ? ? ? ? _tableView.PTdelegate = self;
??? ? ? ? ? ? ? _tableView.PTdataSource = self;
??? ? ? ? ? ? ? _tableView.btnFont = 11.0;
??? ? ? ? ? ? ? _tableView.btnWidth = 74.0;
??? ? ? ? ? ? ? _tableView.editingArr = @[@"置頂",@"刪除"];
??????????????? _tableView.editingBgColorArr = @[[UIColor yellowColor],[UIColor redColor]];??
?? ? ? ? ? ? ? _tableView.editingTColorArr = @[[UIColor blueColor],[UIColor blackColor]];
}
return _tableView;
}
然后剩下的就跟平時使用uitableview一樣了。
接下來來講講PTLeftSlideTableView的實現原理。
因為uitableviewcell的編輯操作其實就是cell的左平移,即cell.frame的改變,由此想到了KVO機制。PTLeftSlideTableView正是運用KVO,在cell的frame發生改變時平鋪一個view在顯現出來的UITableViewCellDeleteConfirmationView上方。
效果如下: