UITableView 常用的UI控件,但是对于IOS提供的方法而言,不够简明,为了实现如下图所示的,通过左滑手势,自动删除cell而言,可以减少一步用户操作,增强用户体验。
如何设计出此效果呢?
重点在于如何针对性的改造UITableViewCell。
- 禁止UITableViewCell相应点击、滑动事件:在cell初始化中添加代码:
self.selectionStyle = UITableViewCellSelectionStyleNone;
- 为UITableViewCell添加自定义手势动作
_deleteGestrue =
[[UIPanGestureRecognizer alloc] initWithTarget:self
action:@selector(handleDeleteGesture:)];
_deleteGestrue.delegate = self;
[self.contentView addGestureRecognizer:_deleteGestrue];
_tapGestrue = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapGesture:)];
[self.contentView addGestureRecognizer:_tapGestrue];
在该例子中仅为了实现左滑删除的效果,因此在手势处理中,限制滑动方向,额外添加点击手势是因为第一步中禁止cell相应交互后,需要自行维护点击事件,添加手势处理事件:
-(void)handleDeleteGesture:(UIScreenEdgePanGestureRecognizer *)gesture{
if(UIGestureRecognizerStateBegan == gesture.state ||
UIGestureRecognizerStateChanged == gesture.state)
{
CGPoint translation = [gesture translationInView:gesture.view];
if ( translation.x<0) {
//手势向左滑动 删除
[UIView animateWithDuration:0.5 animations:^{
self.center = CGPointMake(WIDTH(self)/2 + translation.x, self.center.y);
CGFloat redFloat = 255+translation.x/WIDTH(self)*255;
self.backgroundColor = RGBACOLOR(255, redFloat, redFloat, 1);
}];
}
}
else if (UIGestureRecognizerStateEnded== gesture.state){
CGPoint translation = [gesture translationInView:gesture.view];
if (translation.x+WIDTH(self)<WIDTH(self)/3) {
[UIView animateWithDuration:0.3 animations:^{
self.center = CGPointMake(WIDTH(self)*-1, self.center.y);
} completion:^(BOOL finished){
if ([self.delegate respondsToSelector:@selector(tableViewCell:didEndTapGestureWith:)]) {
[self.delegate tableViewCell:self didEndTapGestureWith:TableViewCellDeletion];
}
[_separateLayer removeFromSuperlayer];
[_lineLayer removeFromSuperlayer];
}];
}else{
[UIView animateWithDuration:0.5 animations:^{
self.center = CGPointMake(WIDTH(self)/2, self.center.y);
self.backgroundColor = [UIColor whiteColor];
}];
}
}else{//取消,失败,结束的时候返回原处
[UIView animateWithDuration:0.5 animations:^{
self.center = CGPointMake(WIDTH(self)/2, self.center.y);
self.backgroundColor = [UIColor whiteColor];
}];
}
}
手势滑动开始后,将vell背景颜色渐变为红色,同时调整center坐标,实现滑动,在滑动到左侧1/3的位置抬起手势后,自动删除。取消、或者不满足删除条件时,则恢复cell显示。
-(void)handleTapGesture:(UITapGestureRecognizer *)gesture{
if ([self.delegate respondsToSelector:@selector(tableViewCell:didEndTapGestureWith:)]) {
[self.delegate tableViewCell:self didEndTapGestureWith:TableViewCellSelection];
}
}
点击事件的处理。注意到在两个手势处理中分别都添加了delegate事件,是因为cell本身不响应用户交互,UItableView也就无法实时获取到cell 的状态,因此此时需要我们为cell添加delegate:
typedef NS_ENUM(NSInteger, TableViewCellActionType) {
TableViewCellSelection,
TableViewCellDeletion
};
@class TableViewCell ;
@protocol TableViewCellDelegate <NSObject>
-(void)tableViewCell:(TableViewCell *)Cell didEndTapGestureWith:(TableViewCellActionType)type;
@interface TableViewCell : UITableViewCell<UIGestureRecognizerDelegate>
@property (assign,nonatomic) id<TableViewCellDelegate> delegate;
@end
完成以上设置后,在UITableView添加cell中,为cell设置好delegate,就可以完整的实现对cell的自定义手势操作了。
3. 除了该例子中使用到的滑动删除的手势之外,还可以针对滑动的方向、滑动的距离等,实现更多充分利用手势完成的操作,相比button点击事件而言,能够增加APP相应的灵气。
4. (完)