2012-01-25 14 views
19

मैं का उपयोग कर UIImages की गैलरी प्रदर्शित करने के लिए एक एप्लिकेशन विकसित कर रहा हूं, मेरा प्रश्न है कि zoom पर टैप करने के लिए कैसे करें और zoom पर दो बार टैप करें, UIScrollView के साथ संभालने पर यह कैसे काम करता है। असल में इस कोड को कह रहा है कि जब एक UITapGestureself.view विधि tapOnce या tapTwice में पंजीकृत हैself में बुलाया जाएगा अपने ViewControllerआईओएस में ज़ूम आउट करने के लिए ज़ूम करने और दो बार टैप करने के लिए टैप करने के लिए कैसे टैप करें?

- (void)viewDidLoad 
{ 
    [super viewDidLoad];  

    // what object is going to handle the gesture when it gets recognised ? 
    // the argument for tap is the gesture that caused this message to be sent 
    UITapGestureRecognizer *tapOnce = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnce:)]; 
    UITapGestureRecognizer *tapTwice = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTwice:)]; 

    // set number of taps required 
    tapOnce.numberOfTapsRequired = 1; 
    tapTwice.numberOfTapsRequired = 2; 

    // stops tapOnce from overriding tapTwice 
    [tapOnce requireGestureRecognizerToFail:tapTwice]; 

    // now add the gesture recogniser to a view 
    // this will be the view that recognises the gesture 
    [self.view addGestureRecognizer:tapOnce]; 
    [self.view addGestureRecognizer:tapTwice]; 

} 

में - डॉक्स here -

उत्तर

38

आप UITapGestureRecognizer लागू करने की आवश्यकता इसके आधार पर यदि यह एक सिंगल या डबल टैप है। इसलिए आपके UIViewController करने के लिए इन नल तरीकों जोड़ने की जरूरत:

- (void)tapOnce:(UIGestureRecognizer *)gesture 
{ 
    //on a single tap, call zoomToRect in UIScrollView 
    [self.myScrollView zoomToRect:rectToZoomInTo animated:NO]; 
} 
- (void)tapTwice:(UIGestureRecognizer *)gesture 
{ 
    //on a double tap, call zoomToRect in UIScrollView 
    [self.myScrollView zoomToRect:rectToZoomOutTo animated:NO]; 
} 

आशा है कि मदद करता है

+0

आप अपने जवाब के लिए बहुत बहुत धन्यवाद, मैं इसे परीक्षण करने और वापस रिपोर्ट जल्द से जल्द किया है जाएगा। – Bruno

+0

क्या यह संभव है वेबव्यू पहचान पक्ष के वेबव्यू – Codesen

+2

में लिंक को स्पर्श करें [टैपऑन की आवश्यकता है GestureRecognizerToFail: tapTwice]; वही है जो मैं खोज रहा था। धन्यवाद! – Bastek

2

स्विफ्ट 3.0 संस्करण कि डबल नल पर दो बार ज़ूम करता है।

@IBOutlet weak var scrollView: UIScrollView! 
@IBOutlet weak var imageView: UIImageView! 

कहीं (आमतौर पर viewDidLoad में):

let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(onDoubleTap(gestureRecognizer:))) 
tapRecognizer.numberOfTapsRequired = 2 
scrollView.addGestureRecognizer(tapRecognizer) 

हैंडलर:

func onDoubleTap(gestureRecognizer: UITapGestureRecognizer) { 
    let scale = min(scrollView.zoomScale * 2, scrollView.maximumZoomScale) 

    if scale != scrollView.zoomScale { 
     let point = gestureRecognizer.location(in: imageView) 

     let scrollSize = scrollView.frame.size 
     let size = CGSize(width: scrollSize.width/scale, 
          height: scrollSize.height/scale) 
     let origin = CGPoint(x: point.x - size.width/2, 
          y: point.y - size.height/2) 
     scrollView.zoom(to:CGRect(origin: origin, size: size), animated: true) 
     print(CGRect(origin: origin, size: size)) 
    } 
} 
संबंधित मुद्दे