Jak stuknąć, aby powiększyć i stuknąć dwukrotnie, aby pomniejszyć system iOS?

Rozwijam aplikację do wyświetlania galerii UIImages za pomocą UIScrollView, moje pytanie brzmi, jak stuknąć do zoom i dwukrotnie stuknąć do zoom out, Jak to działa podczas obsługi z UIScrollView.

Author: Larme, 2012-01-25

2 answers

Musisz zaimplementować UITapGestureRecognizer-docs tutaj - w twoim 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];

}

Zasadniczo ten kod mówi, że gdy {[2] } jest zarejestrowany w self.view metoda tapOnce lub taptvice zostanie wywołana w self w zależności od tego, czy jest to pojedynczy czy podwójny kraniec. Dlatego musisz dodać te metody tap do swojego 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];
}

Hope that helps

 39
Author: Gaz_Edge,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-03 20:44:36

Wersja Swift 4.0 , która powiększa dwukrotnie przy podwójnym dotknięciu.

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

Gdzieś (zwykle w viewDidLoad):

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

:

@objc 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))
    }
}
 2
Author: Avt,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-06-22 07:15:48