简体   繁体   中英

How to Zoom Image on single tap in an iCarousel View?

I am trying hard to zoom image on single tap, but no success yet. I have a carousel view with 6 images which works great but i want the particular image to zoom/pop-up when tapped on the image in carousel view. Any help will be highly appreciated.

First Declare the TapGestureRegcognizer then addSubview in your UIImageView . use this code in your viewDidLoad :

UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];

[doubleTap setNumberOfTapsRequired:1];

[imageview addGestureRecognizer:doubleTap];

then if you click the UIImageView single tap, call the below methods are,

    - (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer {
        // zoom in
        float newScale = [myscrollview zoomScale] * 2;

        if (newScale > self.myscrollview.maximumZoomScale){
            newScale = self.myscrollview.minimumZoomScale;
            CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];

            [myscrollview zoomToRect:zoomRect animated:YES];

        }
        else{

            newScale = self.myscrollview.maximumZoomScale;
            CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];

            [myscrollview zoomToRect:zoomRect animated:YES];
        }
    }

- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center {

    CGRect zoomRect;

    // the zoom rect is in the content view's coordinates.
    //    At a zoom scale of 1.0, it would be the size of the imageScrollView's bounds.
    //    As the zoom scale decreases, so more content is visible, the size of the rect grows.
    zoomRect.size.height = [myscrollview frame].size.height / scale;
    zoomRect.size.width  = [myscrollview frame].size.width  / scale;

    // choose an origin so as to get the right center.
    zoomRect.origin.x    = center.x - (zoomRect.size.width  / 2.0);
    zoomRect.origin.y    = center.y - (zoomRect.size.height / 2.0);

    return zoomRect;
}

its working for me, hope its helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM