UIScrollview delaysContentTouches issue

2019-01-21 23:25发布

I have UIScrollView loaded with UIButtons and on UIButton action I have highlighted UIImage of each UIButton.

If I don't set delaysContentTouches as NO then highlighted UIImage of UIButton will not shown if I touch up UIButton very fast. After I set delaysContentTouches property as NO then only UIButton highlighted UIImage is shown.

Now after setting delaysContentTouches property as NO for UIScrollView. I can not scroll my UIScrollView by dragging on the UIButtons. Now how can I resolve this issue.

Please give me an advise.

Thanks in advance.

5条回答
Emotional °昔
2楼-- · 2019-01-22 00:03

Here's what works for me. Subclass UIScrollView, and implement only this method:

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

Then set delaysContentTouches = NO;

Voila! Works just like the home screen: Highlights buttons immediately, but still allows scrolling :)

查看更多
劫难
3楼-- · 2019-01-22 00:03

OK I have resolved by implementing below method :

- (BOOL)touchesShouldCancelInContentView:(UIView *)view
{
    NSLog(@"touchesShouldCancelInContentView");

    if ([view isKindOfClass:[UIButton class]])
        return NO;
    else
        return YES;
}
查看更多
相关推荐>>
4楼-- · 2019-01-22 00:15

Unable to find a satisfactory solution online so far (and it seems to be that Apple is ignoring the issue). Found a thread on Apple's developer forum with some suggestions in there that may help: UIScrollView: 'delaysContentTouches' ignored

I was able to use the workaround from this link. To summarize the workaround (I'm para-quoting here):

UIEvent objects contain a time stamp.

You can record the time stamp at the time of touchesBegan on your embedded subview.

In touchesMoved of scrollView's subview, look at the time stamp and location again.

If the touch has not moved very far and more than, say, 0.1 seconds have passed, you can assume the user touched the subview and then delayed movement.

In this case, the UIScrollView will have decided, independently, that this is NOT a scrolling action even though it will never tell you that.

So, you can have a local state variable to flag that this condition of delayed movement occurred and process events received by the subview.

Here's my code:

-(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
}
查看更多
劫难
5楼-- · 2019-01-22 00:19

I had same issue & same hierarchy of the views, With latest sdk , just use it :

Setting delaysContentTouches to NO for UIButton in the same UITableViewCell.

self.scrollview.delaysContentTouches = NO
查看更多
Melony?
6楼-- · 2019-01-22 00:21

I found that in iOS 8, the UIScrollView's underlying UIPanGestureRecognizer is not respecting the UIScrollView's delaysContentTouches property. I consider this an iOS 8 bug. Here's my workaround:

theScrollView.panGestureRecognizer.delaysTouchesBegan = theScrollView.delaysContentTouches
查看更多
登录 后发表回答