2011-01-19 12 views
11

UIWebView पर पर स्पर्श का पता लगाने के लिए कैसे मैं एक स्पर्श का पता लगाने कर सकते हैं?कैसे UIWebView

लेकिन नहीं उपयोगकर्ता कुछ यूआरएल या एक नियंत्रण को छू क्लिक करता है।

इसे संभाल करने के लिए संभव है?

उत्तर

31

उपयोग UIGestureRecognizerDelegate विधि:

घोषणा फ़ाइल में UIGestureRecognizerDelegate जोड़ें (यानी आपके ज फाइल)

चरण 1: बस gestureRecognizer के प्रतिनिधि सेट: (मीटर फ़ाइल viewDidLoad)

UITapGestureRecognizer *webViewTapped = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)]; 
webViewTapped.numberOfTapsRequired = 1; 
webViewTapped.delegate = self; 
[offScreenWebView addGestureRecognizer:webViewTapped]; 
[webViewTapped release]; 

चरण 2 में: इस समारोह ओवरराइड: (में .m फ़ाइल)

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

चरण 3: अब tapAction समारोह को लागू: के बीच में एक खींचें बिना

- (void)tapAction:(UITapGestureRecognizer *)sender 
{  
    NSLog(@"touched"); 

    // Get the specific point that was touched 
    CGPoint point = [sender locationInView:self.view]; 
} 
0

सब कुछ है कि UIResponder से विरासत छूता है संभाल कर सकते हैं (ताकि UIWebView करता है)। दस्तावेज़ पढ़ें: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIResponder_Class/Reference/Reference.html

आप का उपयोग करना होगा:

touchesBegan:withEvent: 

संपादित करें: के लिए clarity-

मेरा मानना ​​है कि फिर वहाँ यह करने का कोई साफ रास्ता है यहाँ टिप्पणी जोड़ने से, आप या तो this जैसे इवेंट विधि के साथ सबसे ज्यादा ओवरराइड कर सकते हैं या इस तरह एक हैक करें: overriding UIView

+0

मैं इसके बारे में पता कर रहा हूँ।लेकिन जब मैं उप-वर्गीकृत यूईवेब्यूव और कार्यान्वित स्पर्श करता हूं, तोगन और स्पर्शों को समाप्त किया जाता है, उन्हें बिल्कुल नहीं कहा जा रहा है ..... – Satyam

+0

उस स्थिति में, आप वेबदृश्य पर एक पारदर्शी UIView बना सकते हैं और उस पर स्पर्श विधि का उपयोग कर सकते हैं। – Ankit

+0

यदि मैं इसके ऊपर पारदर्शी UIView डालता हूं, तो यह लिंक पर क्लिक करने या नियंत्रण आदि को स्पर्श करने के लिए वेबव्यू की डिफ़ॉल्ट कार्यक्षमता को खो देगा ..... क्या मैं सही हूँ? – Satyam

-1

क्या आपका मतलब है कि आप ओवरराइड करना चाहते हैं विकल्प जो पॉपअप पर एक लिंक पर पकड़ते हैं? मैं इस ट्यूटोरियल/गाइड के साथ काम करने के लिए एक पाने में कामयाब रहे, लेकिन एक यहां पोस्ट अभी भी थोड़ा त्रुटिपूर्ण है और जरूरत है कि आप कुछ ठीक ट्यूनिंग करने के लिए: http://www.icab.de/blog/2010/07/11/customize-the-contextual-menu-of-uiwebview/

+0

नहीं। मेरे पास वेब व्यू पर एक करीबी बटन है। जब उपयोगकर्ता वेब दृश्य को छूता है, तो मैं इसे छिपाना चाहता हूं। जब फिर से छुआ, तो यह फिर से बंद बटन दिखाएगा। लेकिन मैं वेब दृश्य पर स्पर्श का पता लगाने में सक्षम नहीं हूं। – Satyam

+0

आप जिस ट्यूटोरियल को मॉनिटर करना चाहते हैं उसे पहचानने के लिए आप उस ट्यूटोरियल को संशोधित कर सकते हैं, बल्कि उदाहरण में टैप करके उस क्रिया को टैप करके रखें। –

0

ध्यान दें कि स्वीकार किए जाते हैं जवाब ऊपर केवल नल इशारों पर कब्जा (टचडाउन और touchup) , और स्वाइप जेश्चर को अनदेखा कर दिया जाएगा।

मेरी प्रयोजनों मैं जरूरत के लिए दोनों के बारे में सूचित किया जाना है, और इसलिए मैं कड़ी चोट इशारा recognizers उचित रूप से जोड़ दिया। (ध्यान दें कि एक सा क्षेत्र होने के बावजूद, आप OR एक साथ स्वाइप नहीं कर सकते इशारा recognizers 'direction संपत्ति है, तो 4 इशारा recognizers किसी भी कड़ी चोट का पता लगाने के लिए आवश्यक हैं)।

// Note that despite being a bit field, you can't `OR` together swipe gesture 
// recognizers' `direction` property, so 4 gesture recognizers are required 
// to detect any swipe 
for (NSNumber * swipeDirection in @[@(UISwipeGestureRecognizerDirectionUp), @(UISwipeGestureRecognizerDirectionDown), @(UISwipeGestureRecognizerDirectionLeft), @(UISwipeGestureRecognizerDirectionRight)]) { 

    UISwipeGestureRecognizer * swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(timerReset:)]; 
    swipe.direction = [swipeDirection integerValue]; 
    swipe.delegate = self; 
    [rootWebView addGestureRecognizer:swipe]; 
} 
1

स्वीकार किए जाते हैं जवाब महान यदि आप केवल नल का पता लगाने की जरूरत है। यदि आपको सभी स्पर्शों का पता लगाने की आवश्यकता है, तो सबसे अच्छा तरीका एक नया UIView सबक्लास बनाना और इसे वेबदृश्य पर रखना है। उपवर्ग में आप hitTest का उपयोग कर छूता पता लगा सकते हैं:

TouchOverlay.h

@class TouchOverlay; 

@protocol TouchOverlayDelegate <NSObject> 

@optional 
- (void)touchOverlayTouched:(TV4TouchOverlay *)touchOverlay; 

@end 


@interface TouchOverlay : UIView 
@property (nonatomic, unsafe_unretained) id <TouchOverlayDelegate> delegate; 
@end 

Touchoverlay.m

@implementation TouchOverlay 

- (id)initWithFrame:(CGRect)frame { 
    self = [super initWithFrame:frame]; 
    return self; 
} 

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { 
    UIView *hitView = [super hitTest:point withEvent:event]; 
    if (hitView == self) { 
    if (self.delegate && [self.delegate respondsToSelector:@selector(touchOverlayTouched:)]) { 
     [self.delegate touchOverlayTouched:self]; 
    } 
    return nil;  // Tell the OS to keep looking for a responder 
    } 
    return hitView; 
} 

@end