Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This sample uses regular zooming capabilities of UIScrollView, which contains UIImageView as subview. You can find the implementation of such zooming in MWPhotoBrowser library for example. _imageView, _doubleTapBeganPoint, _longPressBeganPoint, _minScale are your class (UIScrollView subclass) iVars. So begin with initialization:</p> <pre><code>UILongPressGestureRecognizer* lpgs = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(imageLongPressed:)]; lpgs.minimumPressDuration = .2; [self addGestureRecognizer:lpgs]; </code></pre> <p>Standard zoom handler:</p> <pre><code>- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView { return _imageView; } </code></pre> <p>Using touchesBegan to catch double tap (UITapGestureRecognizer doesn't want to work with UILongPressGestureRecognizer in some reason):</p> <pre><code>- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; NSUInteger tapCount = touch.tapCount; switch (tapCount) { case 2: [self handleDoubleTapBegan:[touch locationInView:self.superview]]; break; default: break; } [[self nextResponder] touchesEnded:touches withEvent:event]; } - (void)handleDoubleTapBegan:(CGPoint)touchPoint { _doubleTapBeganPoint = touchPoint; NSLog(@"image double tap began at location: %@", NSStringFromCGPoint(touchPoint)); } </code></pre> <p>Handle long press and using difference in Y coordinates to calculate zoom scale. _minScale stores your initial zoomScale so we can restore it.</p> <pre><code>- (void) imageLongPressed:(UIGestureRecognizer*)gesture { if (gesture.state == UIGestureRecognizerStateBegan) { self.maximumZoomScale = _maxScale * 2; self.minimumZoomScale = _minScale / 3; _longPressBeganPoint = [gesture locationInView:self.superview]; [self setZoomScale:_minScale animated:YES]; NSLog(@"image long press began at location: %@", NSStringFromCGPoint(_longPressBeganPoint)); } else if (gesture.state == UIGestureRecognizerStateChanged) { CGPoint p = [gesture locationInView:self.superview]; //NSLog(@"image long press changed at location: %@", NSStringFromCGPoint(p)); if (CGPointEqualToPoint(_longPressBeganPoint, _doubleTapBeganPoint)) { _zoom = _minScale + (p.y - _longPressBeganPoint.y) / 100.0; NSLog(@"zoom scale: %f", _zoom); [self setZoomScale:_zoom animated:NO]; } } else if (gesture.state == UIGestureRecognizerStateEnded) { NSLog(@"image long press ended at location: %@", NSStringFromCGPoint([gesture locationInView:gesture.view])); if (self.zoomScale &lt; _minScale) { [self setZoomScale:_minScale animated:YES]; NSLog(@"min zoom scale: %f", _minScale); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload