UIScrollview delaysContentTouches issue

前端 未结 6 2092
無奈伤痛
無奈伤痛 2020-12-13 10:33

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

6条回答
  •  时光说笑
    2020-12-13 11:00

    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
    }
    

提交回复
热议问题