問(wèn)題
如何自定義可以編輯選中Cell還有簡(jiǎn)單的動(dòng)畫(huà)效果的方式(不用系統(tǒng)原始的編輯方式)
代碼地址
效果演示
GIF 耐心看完,一般的編輯功能也就這些東西了
EditCell效果圖.gif
查看目錄結(jié)構(gòu)
編輯cell 目錄結(jié)構(gòu).jpeg
實(shí)現(xiàn)思路贱纠,關(guān)鍵代碼
- 首先自定一個(gè)cell
自定義cell 這個(gè)就不做參數(shù)重父,不明白的可以直接下載demo 看耻矮。 - 計(jì)算cell 的frame
# addChildView 里面最關(guān)鍵的代碼: [self.contentView layoutIfNeeded];一定要有,這個(gè)是動(dòng)畫(huà)效果實(shí)現(xiàn)的關(guān)鍵侥涵,有了這句代碼眉抬,初始的cell 里面空間才會(huì)有frame ,這樣動(dòng)畫(huà)效果才是最逼真的暖途。
- (void)addChildView{
[self.contentView addSubview:self.selectBtn];
[self.selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.mas_equalTo(self.contentView.mas_leading).mas_offset(20);
make.top.mas_equalTo(self.contentView.mas_top).mas_offset(15);
make.size.mas_equalTo(CGSizeMake(20, 20));
}];
[self.contentView addSubview:self.contentLabel];
[self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.mas_equalTo(self.selectBtn.mas_trailing).mas_offset(2);
make.top.mas_equalTo(self.contentView.mas_top).mas_offset(15);
make.trailing.mas_equalTo(self.contentView.mas_trailing).mas_offset(-15);
make.bottom.mas_equalTo(self.contentView.mas_bottom).mas_offset(-15);
}];
[self.contentView addSubview:self.bottomLineView];
[self.bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.mas_equalTo(self.contentView.mas_leading).mas_offset(20);
make.trailing.mas_equalTo(self.contentView.mas_trailing).mas_offset(-20);
make.bottom.mas_equalTo(self.contentView);
make.height.mas_equalTo(0.5);
}];
[self.contentView layoutIfNeeded];
}
- 添加動(dòng)畫(huà)效果
# 關(guān)鍵代碼看 setModel 方法里面的 對(duì) 選中按鈕的隱藏,內(nèi)容的布局膏执,還最簡(jiǎn)單的addAnimation 動(dòng)畫(huà)函數(shù)代碼
- (void)setModel:(FanEditCellModel *)model{
_model = model;
if (model.isEditStatu) {
self.selectBtn.hidden = NO;
[self.contentLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
make.leading.mas_equalTo(self.selectBtn.mas_trailing).mas_offset(4);
make.top.mas_equalTo(self.contentView.mas_top).mas_offset(15);
make.trailing.mas_equalTo(self.contentView.mas_trailing).mas_offset(-15);
make.bottom.mas_equalTo(self.contentView.mas_bottom).mas_offset(-15);
}];
} else{
self.selectBtn.hidden = YES;
[self.contentLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
make.leading.mas_equalTo(self.contentView.mas_leading).mas_offset(20);
make.top.mas_equalTo(self.contentView.mas_top).mas_offset(15);
make.trailing.mas_equalTo(self.contentView.mas_trailing).mas_offset(-15);
make.bottom.mas_equalTo(self.contentView.mas_bottom).mas_offset(-15);
}];
}
self.selectBtn.selected = model.isSelect;
self.contentLabel.text = model.content;
[self addAnimation];
}
- (void)setIsSelect:(BOOL)isSelect{
_isSelect = isSelect;
self.selectBtn.selected = isSelect;
}
- (void)addAnimation{
[UIView animateWithDuration:0.25 animations:^{
[self.contentView layoutIfNeeded];
}];
}
- 至于選中和刪除這個(gè)邏輯處理驻售,這個(gè)簡(jiǎn)單不做敘述,給你一個(gè)Model
# FanBaseModel 只是一個(gè)基礎(chǔ)model 你就當(dāng)做NSObject
@interface FanEditCellModel : FanBaseModel
@property (nonatomic,assign)BOOL isSelect;
@property (nonatomic,strong)NSString *content;
/// YES :表示現(xiàn)在顯示管理 NO:表示現(xiàn)在顯示完成
@property (nonatomic,assign)BOOL isEditStatu;
@end