把 UITableView 直接放進 UIScrollView,是為了讓超寬的列表能左右滑動:透過設定 UIScrollView 的 contentSize 撐出水平滾動空間。實作上沒問題,但只要再加上「左滑刪除」,手勢就會開始打架。
使用者體驗上通常希望表格緊貼螢幕邊框、不要露出旁邊多餘的內容,所以會把 UIScrollView 的 bounces 關掉(設為 NO),取消彈簧效果。
但接著做左滑刪除時會發現:往左滑,刪除按鈕時有跑出來、時出不來。原因是滑動手勢被外層的 UIScrollView 搶走並消耗掉了,根本沒傳到 tableView 上——這就是典型的手勢衝突。
最乾淨的做法是繼承 UIScrollView,覆寫「是否允許同時辨識」的 delegate 方法。
MyScrollview.h
#import <UIKit/UIKit.h>
@interface MyScrollview : UIScrollView<UIGestureRecognizerDelegate>
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
@end
MyScrollview.m
#import "MyScrollview.h"
@implementation MyScrollview
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
if (gestureRecognizer.state != 0) {
return YES;
} else {
return NO;
}
}
@end
接著在 ViewController 引入 MyScrollview.h,把原本的 UIScrollView 換成 MyScrollview 即可。
#import "ViewController.h"
#import "sideslipTableViewCell.h"
#import "MyScrollview.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UIGestureRecognizerDelegate>{
UITableView *sideslipTableView;
NSMutableArray *dataArray;
MyScrollview *mainScrollView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self initUI];
dataArray = [NSMutableArray arrayWithArray: @[@"1111",@"2222",@"3333",@"4444",@"5555",@"6666",@"7777",@"8888",@"9999"]];
}
#pragma mark - 初始化UI
- (void)initUI{
self.view.backgroundColor = RGB(242, 242, 247);
self.automaticallyAdjustsScrollViewInsets = NO;
mainScrollView = [[MyScrollview alloc] initWithFrame:CGRectMake(0, 44 + 10, kScreenWidth, kScreenHeight - 44 - 10)];
mainScrollView.bounces = NO;
mainScrollView.delegate = self;
[mainScrollView setDelaysContentTouches:NO];
[mainScrollView setCanCancelContentTouches:NO];
[self.view addSubview:mainScrollView];
sideslipTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 60, 440, kScreenHeight - 60) style:UITableViewStylePlain];
sideslipTableView.backgroundColor = [UIColor clearColor];
sideslipTableView.delegate = self;
sideslipTableView.dataSource = self;
sideslipTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[mainScrollView addSubview:sideslipTableView];
mainScrollView.contentSize = CGSizeMake(440, 0);
}
#pragma mark - 行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return dataArray.count;
}
#pragma mark - 行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 46;
}
#pragma mark - cell內容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *indefier = @"cell";
sideslipTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indefier];
if (!cell) {
cell = [[sideslipTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indefier];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.lable.text = dataArray[indexPath.row];
return cell;
}
//先要設Cell可編輯
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
//定義編輯樣式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
//進入編輯模式,按下出現的編輯按鈕後,進行刪除操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[dataArray removeObjectAtIndex:indexPath.row];
[sideslipTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
//修改編輯按鈕文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return @"刪除";
}
@end
把上面那個 titleForDeleteConfirmationButtonForRowAtIndexPath 註解掉,改實作下面這個方法就好:
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewRowAction *deleteRoWAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"刪除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"點擊刪除");
}];
deleteRoWAction.backgroundColor = RGB(210, 207, 209);
UITableViewRowAction *editRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"關閉" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"點擊關閉");
}];
editRowAction.backgroundColor = [UIColor redColor];
return @[editRowAction, deleteRoWAction];
}
以下兩行是整個方案能生效的關鍵,少了它們手勢一樣會被吃掉:
[mainScrollView setDelaysContentTouches:NO];
[mainScrollView setCanCancelContentTouches:NO];
另外常用到的螢幕尺寸巨集也一併附上:
// 當前屏幕寬度
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
// 當前屏幕高度
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
歸納一下:外層 scrollView 負責水平滾動,內層 tableView 負責垂直列表與 cell 側滑,兩者之所以能和平共處,靠的就是「允許同時辨識」加上關閉 touch 延遲與取消。