UIScrollview delayedContentTouches问题

我有UIScrollView加载UIButtonsUIButton操作我突出显示了每个UIButton UIImage

如果我没有将delaysContentTouches设置为NO则突出显示delaysContentTouches UIImage如果我非常快地触摸UIButton将不会显示。 在我将delaysContentTouches属性设置为NO后,只显示UIButton突出显示的UIImage

现在将delaysContentTouches属性设置为UIScrollView NO。 我无法通过拖动UIButtons来滚动我的UIScrollView 。 现在我该如何解决这个问题。

请给我一个建议。

提前致谢。

这对我有用。 子类UIScrollView,并只实现此方法:

 - (BOOL)touchesShouldCancelInContentView:(UIView *)view { return YES; } 

然后设置delaysContentTouches = NO;

瞧! 像主屏幕一样工作:立即突出显示按钮,但仍允许滚动:)

我发现在iOS 8中,UIScrollView的底层UIPanGestureRecognizer不尊重UIScrollView的delaysContentTouches属性。 我认为这是iOS 8的错误。 这是我的解决方法:

 theScrollView.panGestureRecognizer.delaysTouchesBegan = theScrollView.delaysContentTouches 

好的我通过实现以下方法解决了:

 - (BOOL)touchesShouldCancelInContentView:(UIView *)view { NSLog(@"touchesShouldCancelInContentView"); if ([view isKindOfClass:[UIButton class]]) return NO; else return YES; } 

到目前为止,无法在网上找到满意的解决方案(似乎苹果忽略了这个问题)。 在Apple的开发者论坛上找到了一个主题,其中有一些建议可能有所帮助: UIScrollView:’delaysContentTouches’被忽略

我能够使用此链接中的解决方法。 总结一下解决方法(我在这里引用):

UIEvent对象包含时间戳。

您可以在嵌入式子视图中记录touchesBegan时的时间戳。

在scrollView的子视图的touchesMoved中,再次查看时间戳和位置。

如果触摸没有移动很远且超过0.1秒,您可以假设用户触摸了子视图然后延迟了移动。

在这种情况下,UIScrollView将独立决定,这不是一个滚动操作,即使它永远不会告诉你。

因此,您可以使用本地状态变量来标记发生延迟移动的这种情况并处理子视图接收的事件。

这是我的代码:

 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ // store the timestamp _beginDragTimeStamp = event.timestamp; // your embedded subview's touches begin code } -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ // compare and ignore drag if time passed between tap and drag is less than 0.5s if(event.timestamp - _beginDragTimeStamp < 0.5) return; // your drag code } 

我有相同的问题和相同的视图层次结构,使用最新的sdk,只需使用它:

在同一个UITableViewCell中为UIButton设置delayedContentTouches为NO。

 self.scrollview.delaysContentTouches = NO