How to Zoom In/Out Photo on double Tap in the iPhone WWDC 2010 - 104 PhotoScroller

前端 未结 14 1738
野性不改
野性不改 2020-12-12 13:28

I am going through the Sample code of iPhone WWDC 2010 - 104 PhotoScroller App. It\'s working great with my project related images (PDF Page Images)

but I am struggl

相关标签:
14条回答
  • 2020-12-12 14:02

    This appears to work using convertRect:fromView: and zoomToRect:animated::

    -(void)tap:(UITapGestureRecognizer *)tapGestureRecognizer
    {
        CGFloat zoomeScaleMultiplier = 0.5;
        UIScrollView *scrollView = (UIScrollView *) tapGestureRecognizer.view;
        UIView *zoomableView = [scrollView.delegate viewForZoomingInScrollView:scrollView];
    
        CGRect rect = scrollView.bounds;
        rect.origin = CGPointZero;
    
        CGAffineTransform transform = CGAffineTransformMakeScale(zoomeScaleMultiplier, zoomeScaleMultiplier);
        rect = CGRectApplyAffineTransform(rect, transform);
    
        rect.origin = [tapGestureRecognizer locationInView:scrollView];
    
        rect = CGRectOffset(rect, CGRectGetWidth(rect)/-2., CGRectGetHeight(rect)/-2.);
    
        rect = [zoomableView convertRect:rect fromView:scrollView];
    
        [scrollView zoomToRect:rect animated:YES];
    }
    
    0 讨论(0)
  • 2020-12-12 14:03

    here my code work for me it's simple :

    // Listen for Double Tap Zoom
    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
    
    [doubleTap setNumberOfTapsRequired:2];
    
    [self addGestureRecognizer:doubleTap];
    

    you need

    BOOL checkZoomImage;
    

    Here tab method

    - (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer {
    
        if (self.zoomScale > self.minimumZoomScale && self.zoomScale == self.maximumZoomScale){
            checkZoomImage = YES;
        }
        if (self.zoomScale < self.maximumZoomScale && self.zoomScale == self.minimumZoomScale) {
            checkZoomImage = NO;
        }
    
    
        if (checkZoomImage) {
            [self setZoomScale:self.zoomScale *0.5 animated:YES];
        }else{
            [self setZoomScale:self.zoomScale *1.5 animated:YES];
        }
    
    }
    
    0 讨论(0)
  • 2020-12-12 14:06

    use this code so it will zoom where you click.......

    - (void)handleDoubleTap:(UIGestureRecognizer *)recognizer {  
            if(zoomCheck){
                CGPoint Pointview=[recognizer locationInView:self];
                CGFloat newZoomscal=3.0;
    
                newZoomscal=MIN(newZoomscal, self.maximumZoomScale);
    
                CGSize scrollViewSize=self.bounds.size;
    
                CGFloat w=scrollViewSize.width/newZoomscal;
                CGFloat h=scrollViewSize.height /newZoomscal;
                CGFloat x= Pointview.x-(w/2.0);
                CGFloat y = Pointview.y-(h/2.0);
    
                CGRect rectTozoom=CGRectMake(x, y, w, h);
                [self zoomToRect:rectTozoom animated:YES]; 
    
                [self setZoomScale:3.0 animated:YES]; 
                zoomCheck=NO;
            }
            else{ 
                [self setZoomScale:1.0 animated:YES]; 
                zoomCheck=YES;
            }
        } 
    

    use this code so it will zoom where you click....... zoomChech is bool variable for checking zoom

    0 讨论(0)
  • 2020-12-12 14:09

    Swift 3

    Normally image zoom functionality is required with both double tap and pinch gesture, so I am providing complete solution to achieve the same. Double tap zoom is inspired by above answers and pinch zoom was taken from here.

    import UIKit
    
    class ViewController: UIViewController, UIScrollViewDelegate {
    
        var imageView: UIImageView!
        var scrollImg: UIScrollView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            let vWidth = self.view.frame.width
            let vHeight = self.view.frame.height
    
            scrollImg = UIScrollView()
            scrollImg.delegate = self
            scrollImg.frame = CGRect(x: 0, y: 0, width: vWidth, height: vHeight)
            scrollImg.backgroundColor = UIColor(red: 90, green: 90, blue: 90, alpha: 0.90)
            scrollImg.alwaysBounceVertical = false
            scrollImg.alwaysBounceHorizontal = false
            scrollImg.showsVerticalScrollIndicator = true
            scrollImg.flashScrollIndicators()
    
            scrollImg.minimumZoomScale = 1.0
            scrollImg.maximumZoomScale = 10.0
    
            let doubleTapGest = UITapGestureRecognizer(target: self, action: #selector(handleDoubleTapScrollView(recognizer:)))
            doubleTapGest.numberOfTapsRequired = 2
            scrollImg.addGestureRecognizer(doubleTapGest)
    
            self.view.addSubview(scrollImg)
    
            imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: vWidth, height: vHeight))
            imageView.image = UIImage(named: "cat")
            imageView!.layer.cornerRadius = 11.0
            imageView!.clipsToBounds = false
            scrollImg.addSubview(imageView!)
        }
    
        func handleDoubleTapScrollView(recognizer: UITapGestureRecognizer) {
            if scrollImg.zoomScale == 1 {
                scrollImg.zoom(to: zoomRectForScale(scale: scrollImg.maximumZoomScale, center: recognizer.location(in: recognizer.view)), animated: true)
            } else {
                scrollImg.setZoomScale(1, animated: true)
            }
        }
    
        func zoomRectForScale(scale: CGFloat, center: CGPoint) -> CGRect {
            var zoomRect = CGRect.zero
            zoomRect.size.height = imageView.frame.size.height / scale
            zoomRect.size.width  = imageView.frame.size.width  / scale
            let newCenter = imageView.convert(center, from: scrollImg)
            zoomRect.origin.x = newCenter.x - (zoomRect.size.width / 2.0)
            zoomRect.origin.y = newCenter.y - (zoomRect.size.height / 2.0)
            return zoomRect
        }
    
        func viewForZooming(in scrollView: UIScrollView) -> UIView? {
            return self.imageView
        }
    
    }
    
    0 讨论(0)
  • 2020-12-12 14:12

    Previous answer could be simpler with BlocksKit, like below.

    #import "BlocksKit.h"
    ...
    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithHandler:^(UIGestureRecognizer *sender, UIGestureRecognizerState state, CGPoint location) {
        if(scrollView.zoomScale != 1.0){
            [scrollView setZoomScale:1.0 animated:YES];
        }else{
            [scrollView setZoomScale:2.0 animated:YES];
        }
    }];
    [doubleTap setNumberOfTapsRequired:2];
    [scrollView addGestureRecognizer:doubleTap];
    
    0 讨论(0)
  • 2020-12-12 14:13

    How about adding a UITapGestureRecognizer to the ImageScrollView?

    As long as you are looking at sample code, you can check out how UIGestureRecognizers are used in the ScrollViewSuite sample code.

    0 讨论(0)
提交回复
热议问题