(原創(chuàng)博客,如將轉(zhuǎn)載煩請(qǐng)備注出處)
iOS版本的QQ左滑會(huì)出現(xiàn)‘置頂’、‘刪除’等多個(gè)按鈕,產(chǎn)生左劃動(dòng)作cell會(huì)向左滑動(dòng),在cell已經(jīng)是出現(xiàn)多個(gè)操作按鈕的情況下點(diǎn)擊或產(chǎn)生右滑動(dòng)作cell都會(huì)復(fù)位。
以前我的第一想法和做法是:自定義cell,在cell上放一個(gè)scrollview,根據(jù)手勢(shì)來(lái)判別相應(yīng)操作,加上一些動(dòng)畫,可是這樣的做法很難達(dá)到很和諧,如系統(tǒng)動(dòng)畫那般絲滑。
下面我要給大家介紹PTLestSlideTableView。PTLestSlideTableView是繼承了UITableView,并開(kāi)放出按鈕相關(guān)配置接口。
直接定義一個(gè)成員屬性PTLeftSlideTableView *tableView
@property (nonatomic, strong) PTLeftSlideTableView *tableView;
在初始化的時(shí)候可以定義好相關(guān)屬性
- (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;
}
然后剩下的就跟平時(shí)使用uitableview一樣了。
接下來(lái)來(lái)講講PTLeftSlideTableView的實(shí)現(xiàn)原理。


因?yàn)閡itableviewcell的編輯操作其實(shí)就是cell的左平移,即cell.frame的改變,由此想到了KVO機(jī)制。PTLeftSlideTableView正是運(yùn)用KVO,在cell的frame發(fā)生改變時(shí)平鋪一個(gè)view在顯現(xiàn)出來(lái)的UITableViewCellDeleteConfirmationView上方。
效果如下:
