繁体   English   中英

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

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

我在我的UITableViewCell子类上添加了一个UIPanGestureRecognizer ,这样当用户向左滑动时,下面的按钮就会显示出来(有点像在Mail应用程序中)。 我在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];

我想允许表视图滚动,尽管我的自定义手势识别器,所以我在同一个UITableViewCell子类中也有以下内容:

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

我现在的问题是我不知道如何一次只允许一个手势识别器,因此当用户侧扫时,表视图不会滚动,反之亦然。 救命?


我尝试过的东西不起作用

方法1

在表视图的视图控制器中实现UIGestureRecognizerDelegate,并且除了用于滚动表视图的垂直手势识别器之外,还要求任何其他手势识别器的失败。

  1. 将表视图的panGestureRecognizer的委托设置为包含它的视图控制器(比如它的ContainingViewController )。

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

  3. 实现shouldRequireFailureOfGestureRecognizer:并返回YES(我假设otherGestureRecognizer将是水平平移手势)。

错误:'UIScrollView的内置平移手势识别器必须将其滚动视图作为其委托。

创建一个bool,允许你的panGesture在panScrollView

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

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

然后

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

如果您正在平移整个tableview,我会将手势识别器放在tableView本身上......否则也会

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

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

它为你处理所有这些...但我假设有一个你不想使用它的原因。 您也可以在此逻辑的基础上进行构建,例如,您可能希望在scrollView也可以进行调整时进行调整,方法是将相似的检查放入-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView等。

使用Magoo解决方案,我做了一些适合我的simillar。 希望它可以帮助某人。

在Controller类中

#pragma mark - UIScrollViewDelegate methods

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

{
    ApplicationDelegate.blockPanGesture = YES;
}

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

内部细胞:

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

    // do your stuff

}

仅供参考:在Cell的xib文件中拖动UIPanGestureRecognizer并在IBOutletAction上创建| panGestureRecognizer:|

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

暂无
暂无

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

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