How to add a gesture recognizer to a UIWebView subclass?

后端 未结 4 2057
隐瞒了意图╮
隐瞒了意图╮ 2020-12-09 00:35

If I add a gesture recognizer to a subclassed UIWebView instance called webView, e.g.:

UILongPressGestureRecognizer *_longPressReco         


        
相关标签:
4条回答
  • 2020-12-09 01:03

    Just thought I would add the answer if anyone comes back to here. You have not assigned the delegate, therefore gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: is not called.

    _longPressRecognizer.delegate = self;
    

    Do this and it works well.

    0 讨论(0)
  • 2020-12-09 01:08
    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
    }
    
    0 讨论(0)
  • 2020-12-09 01:14

    as far as I know UIWebView shouldnt be subclassed as mentioned in Apple docs:

    http://developer.apple.com/iphone/library/documentation/uikit/reference/UIWebView_Class/Reference/Reference.html

    0 讨论(0)
  • 2020-12-09 01:15

    You should use javascript in the UIWebView to detect the gestures. (You can then communicate that back to the Objective-C if you need to.) These are Apple's docs on detecting gestures and touches in Javascript. I also found this article helpful, although he uses a javascript library to deal with event binding, etc.

    Here's a working example of pinching to zoom on a UIWebView that stands alone.

    Note that it is the body element is listening for the event. On a short page it appears not to catch it if you do the event in the vast un-rendered whitespace below. (If anyone knows more about it, please leave a comment.)

    <body id='body'>
        Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco 
    
    <script type="text/javascript" charset="utf-8">
    
        body = document.getElementById('body');
    
        // (in percents)
        zoom = 100;
        maxZoom = 200;
        minZoom = 50;
        zoomIncrement = 25;
    
        function gestureEnd(event) {
            var newZoom;
            if (event.scale > 1.0) {
                // increase size
                newZoom = zoom + zoomIncrement;
            } else {
                // decrease size
                newZoom = zoom - zoomIncrement;
            }
    
            // don't exceed zoom boundaries
            if (newZoom > maxZoom || newZoom < minZoom) {
                return;
            }
            zoom = newZoom;
            body.style.webkitTextSizeAdjust = zoom+"%";
        }
    
        body.addEventListener("gestureend", gestureEnd, false);
    
        </script>
    </body>
    
    0 讨论(0)
提交回复
热议问题