简体   繁体   English

当UITableView仍然垂直滚动时,禁用UITableViewCell上的水平滚动

[英]Disable horizontal scrolling on UITableViewCell when UITableView is still scrolling vertically

I added a UIPanGestureRecognizer on my UITableViewCell subclass so that when the user swipes to the left, the buttons underneath are revealed (kind of like in the Mail app). 我在我的UITableViewCell子类上添加了一个UIPanGestureRecognizer ,这样当用户向左滑动时,下面的按钮就会显示出来(有点像在Mail应用程序中)。 I do this in awakeFromNib : 我在awakeFromNib这样做:

// Add a pan gesture recognizer to the pannable view.
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panScrollView:)];
panGesture.delegate = self;
[self.pannableView addGestureRecognizer:panGesture];

I want to allow the table view to scroll despite my custom gesture recognizer so I also have the following in the same UITableViewCell subclass: 我想允许表视图滚动,尽管我的自定义手势识别器,所以我在同一个UITableViewCell子类中也有以下内容:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

My problem now is I don't know how to allow only one gesture recognizer at a time, so that when the user is side-swiping, the table view doesn't scroll, and vice versa. 我现在的问题是我不知道如何一次只允许一个手势识别器,因此当用户侧扫时,表视图不会滚动,反之亦然。 Help? 救命?


What I've tried that doesn't work 我尝试过的东西不起作用

Method 1 方法1

Implement UIGestureRecognizerDelegate in the table view's view controller and require the failure of any other gesture recognizer besides the vertical one which is for scrolling the table view. 在表视图的视图控制器中实现UIGestureRecognizerDelegate,并且除了用于滚动表视图的垂直手势识别器之外,还要求任何其他手势识别器的失败。

  1. Set the table view's panGestureRecognizer 's delegate to the view controller that contains it (say it's ContainingViewController ). 将表视图的panGestureRecognizer的委托设置为包含它的视图控制器(比如它的ContainingViewController )。

     self.tableView.panGestureRecognizer.delegate = self; 
  2. Make ContainingViewController implement UIGestureRecognizerDelegate . Make ContainingViewController实现UIGestureRecognizerDelegate

  3. Implement shouldRequireFailureOfGestureRecognizer: and return YES (I'm assuming that the otherGestureRecognizer will be the horizontal panning gesture). 实现shouldRequireFailureOfGestureRecognizer:并返回YES(我假设otherGestureRecognizer将是水平平移手势)。

Error: 'UIScrollView's built-in pan gesture recognizer must have its scroll view as its delegate.' 错误:'UIScrollView的内置平移手势识别器必须将其滚动视图作为其委托。

Create a bool that allows your panGesture to action inside panScrollView 创建一个bool,允许你的panGesture在panScrollView

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    self.blockPanGesture = YES;
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    self.blockPanGesture = NO;
}

Then 然后

-(void)panScrollView:(UIPanGestureRecognizer *)panGestureRecognizer
{
    if(self.blockPanGesture == NO)
    {
       // do the stuff
    }
}

If you are panning the entire tableview personally I'd put the gesture recogniser on the tableView itself... Otherwise there's also 如果您正在平移整个tableview,我会将手势识别器放在tableView本身上......否则也会

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {

    // Return YES if you want the specified item to be editable.
    return YES;
}

which handles all this for you... but I'm assuming there's a reason you don't want to use this. 它为你处理所有这些...但我假设有一个你不想使用它的原因。 You can also build on this logic, for example you might want to tweak when the scrollView can drag as well, by putting similar checks in -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView etc. 您也可以在此逻辑的基础上进行构建,例如,您可能希望在scrollView也可以进行调整时进行调整,方法是将相似的检查放入-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView等。

Using Magoo solution, I did something simillar which works for me. 使用Magoo解决方案,我做了一些适合我的simillar。 Hope it helps someone. 希望它可以帮助某人。

In Controller class 在Controller类中

#pragma mark - UIScrollViewDelegate methods

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

{
    ApplicationDelegate.blockPanGesture = YES;
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    ApplicationDelegate.blockPanGesture = NO;
}

Inside Cell: 内部细胞:

- (IBAction)panGestureRecognizer:(UIPanGestureRecognizer *)panGesture
{
    if(ApplicationDelegate.blockPanGesture)
        return;

    // do your stuff

}

Just FYI: have dragged UIPanGestureRecognizer in Cell's xib file and created above IBOutletAction |panGestureRecognizer:| 仅供参考:在Cell的xib文件中拖动UIPanGestureRecognizer并在IBOutletAction上创建| panGestureRecognizer:|

您是否尝试将跳出属性设置为NO?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM