如何实现在UITableView Cell中左右滑动,与iOS Mail类似

我试图复制苹果公司在其邮件应用程序中用于将邮件标记为未读或在邮箱内从左向右滑动时“标记为未读”的相同技术。

Mail里面的iOS 8.1邮件应用程序的屏幕截图

我find了类似的 解决scheme,但只是从右向左滑动的手势。 我希望这个相同的解决scheme可以作为Apple SDK的一部分用于相反的方向。

我怎么能像iOS的邮件应用一样实现相同的从左到右的手势效果?

我find了类似的解决scheme,但只是 从右向左 滑动的手势

SWTableViewCell有你可能想要的所有选项。

在离开单元格时,只需根据需要设置左/右button组。

cell.leftUtilityButtons = [self leftButtons]; cell.rightUtilityButtons = [self rightButtons]; cell.delegate = self; 

通过将视图控制器设置为其代表,您可以听button点击。 关于如何实现的全部细节都在这个链接中

例1:

在这里输入图像说明

例2: 在这里输入图像说明

如果你正在寻找垂直堆叠的button检查了这一点 。

我通常在表级执行它。

 - (void)viewDidLoad { [super viewDidLoad]; UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipe:)]; [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)]; [self.tableView addGestureRecognizer:recognizer]; recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipe:)]; recognizer.delegate = self; [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)]; [self.tableView addGestureRecognizer:recognizer]; } 

然后您可以控制方向,并可以随意自定义

 - (void)leftSwipe:(UISwipeGestureRecognizer *)gestureRecognizer { //do you left swipe stuff here. } - (void)rightSwipe:(UISwipeGestureRecognizer *)gestureRecognizer { //do you right swipe stuff here. Something usually using theindexPath that you get that way CGPoint location = [gestureRecognizer locationInView:self.tableView]; NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location]; } 

信用转到玉心

您提供的链接中接受的答案是两个滑动方向。
注意,对于UISwipeGestureRecognizerDirectionLeftUISwipeGestureRecognizerDirectionRightgestureRecognizer.direction返回YES

你只需要修改几件事情:
改变被调用的select器,所以它会调用你的方法,而不是在post的例子中,
并且将滑动的方向只从左到右改变,而不是像现在这样改变两个方向,因为据我所知,你正试图设置一个方向的滑动。

所以你的代码应该是这样的:

 // In cellForRowAtIndexPath:, where you create your custom cell cell.tableView=tableView; cell.indexPath=indexPath; UISwipeGestureRecognizer *swipeGestureRecognizer=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(YOUR_METHOD_GOES_HERE)]; [cell addGestureRecognizer:swipeGestureRecognizer]; 

 -(BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { if([[gestureRecognizer view] isKindOfClass:[UITableViewCell class]] && ((UISwipeGestureRecognizer*)gestureRecognizer.direction==UISwipeGestureRecognizerDirectionRight) return YES; } 

请注意,您也可以在接受的答案下方使用答案,只需将手势识别器direction属性修改为UISwipeGestureRecognizerDirectionRight ,而不是示例中的当前方向,即UISwipeGestureRecognizerDirectionLeft

如果你select实现这个,你的viewController必须实现手势识别器委托,你的代码应该是这样的:

 // Call this method in viewDidLoad - (void)setUpLeftSwipe { UISwipeGestureRecognizer *recognizer; recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRightt:)]; [recognizer setDirection:UISwipeGestureRecognizerDirectionRight]; [self.tableView addGestureRecognizer:recognizer]; recognizer.delegate = self; } - (void)swipeRight:(UISwipeGestureRecognizer *)gestureRecognizer { CGPoint location = [gestureRecognizer locationInView:self.tableView]; NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location]; ... do something with cell now that i have the indexpath, maybe save the world? ... } 

请注意,如果我没有弄错,那么您需要自己创build单元格滑动animation,因为我相信Xcode的默认单元格animation只有在向左滑动时才会生效。

信用从您提供的链接到MadhavanRPJulian 。 我只是修改了自己的答案,以更好地满足您的需求。
我还没有尝试过,并且自己实现了这个。

使用您的自定义TableViewCell滚动视图如下所示:

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 80; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 120; } -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { return header_view; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [array_field1 count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Schedule_cell"; Custom_Schedule_Cell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = (Custom_Schedule_Cell *)[Custom_Schedule_Cell cellFromNibNamed:@"Custom_Schedule_Cell"]; } x = 0; UILabel *lbl_title =[[UILabel alloc] initWithFrame:CGRectMake(x,0,cell.scroll_view.frame.size.width,cell.scroll_view.frame.size.height)]; lbl_title.text=@"Title Array"; [lbl_title setTextAlignment:NSTextAlignmentCenter]; [lbl_title setFont:[UIFont fontWithName:@"Raleway" size:18.0f]]; x += lbl_title.frame.size.width+10; UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x,0,110,cell.scroll_view.frame.size.height)]; [button setTitle:@"Button1" forState:UIControlStateNormal]; [button setBackgroundColor:[UIColor grayColor]]; [button addTarget:self action:@selector(button1:) forControlEvents:UIControlEventTouchDown]; button.tag = indexPath.row; [button.titleLabel setTextAlignment:NSTextAlignmentCenter]; [button.titleLabel setFont:[UIFont fontWithName:@"Raleway" size:14.0f]]; x += button.frame.size.width+5; UIButton *button2 = [[UIButton alloc] initWithFrame:CGRectMake(x,0,110,cell.scroll_view.frame.size.height)]; [button2 setTitle:@"Button2" forState:UIControlStateNormal]; [button2 setBackgroundColor:[UIColor grayColor]]; [button2 addTarget:self action:@selector(button2:) forControlEvents:UIControlEventTouchDown]; button2.tag = indexPath.row; [button2.titleLabel setTextAlignment:NSTextAlignmentCenter]; [button2.titleLabel setFont:[UIFont fontWithName:@"Raleway" size:14.0f]]; x += button2.frame.size.width+5; UIButton *button3 = [[UIButton alloc] initWithFrame:CGRectMake(x,0,110,cell.scroll_view.frame.size.height)]; [button3 setTitle:@"Button3" forState:UIControlStateNormal]; [button3 setBackgroundColor:[UIColor grayColor]]; [button3 addTarget:self action:@selector(button3:) forControlEvents:UIControlEventTouchDown]; button3.tag = indexPath.row; [button3.titleLabel setTextAlignment:NSTextAlignmentCenter]; [button3.titleLabel setFont:[UIFont fontWithName:@"Raleway" size:14.0f]]; [cell.scroll_view addSubview:lbl_title]; [cell.scroll_view addSubview:button]; [cell.scroll_view addSubview:button2]; [cell.scroll_view addSubview:button3]; x += button3.frame.size.width+5; cell.scroll_view.contentSize = CGSizeMake(x,cell.scroll_view.frame.size.height); cell.scroll_view.showsHorizontalScrollIndicator = NO; cell.scroll_view.showsVerticalScrollIndicator = NO; [cell.scroll_view setContentOffset:CGPointMake(0,0) animated:NO]; [cell.scroll_view setPagingEnabled:true]; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // Custom_Schedule_Cell *selectedCell = [tableView cellForRowAtIndexPath:indexPath]; // [selectedCell.scroll_view setContentOffset:CGPointMake(x2,0) animated:NO]; } -(void)button1:(UIButton *)button { NSLog(@“button1 Click "); [button setBackgroundColor:[UIColor redColor]]; } -(void)button2:(UIButton *)button { NSLog(@“button2 Click"); [button setBackgroundColor:[UIColor greenColor]]; } -(void)button3:(UIButton *)button { NSLog(@“button Click"); [button setBackgroundColor:[UIColor redColor]]; }