Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

hi i have actually multiple images in uiscrollview. actually i want to implement image scrolling like in iphone image gallary. am downloading images from url to imagearray then i can successfully display and scroll these images from imagearray in uiscrollview. but my problem is i want to enable double tap zoom in/out and two fingure zoom in/out in uiscrollviiew like in iphone gallary . like when user double tap an image will zoom in . and when user again double tap that image that image will zoom . and also same case with twofingur gesture. i tried to implement it reading tutorials but cant succeeded actually the problem is when i double tap an image doubletap image method called succcessfully but can do effect on image size this is the code which i have been implement for scrolling and gesture recognition for gesture recognition i used the code one from the apple site

    if (imagesArray.count < 1)
    {


        msgView = [[UIView alloc]initWithFrame:CGRectMake(35, 190, 250, 40)];
        [msgView setBackgroundColor:[UIColor blackColor]];
        [msgView setAlpha:0.5];

        UILabel *mgtLbl = [[UILabel alloc]initWithFrame:CGRectMake(25, 8, 250, 25)];
        [mgtLbl setText:@"Sorry no image available for this post"];
        [mgtLbl setFont:[UIFont systemFontOfSize:13.0]];
        [mgtLbl setBackgroundColor:[UIColor clearColor]];
        [mgtLbl setTextColor:[UIColor blueColor]];
        [msgView addSubview:mgtLbl];
        [self.view addSubview:msgView];
    }
  else  {

      [msgView removeFromSuperview];
    [self.view setBackgroundColor:[UIColor blackColor]];
    srcVw = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
    srcVw.scrollEnabled = YES;
    srcVw.delegate = self;
    [srcVw setZoomScale:1.0];
    srcVw.showsHorizontalScrollIndicator = YES;
    srcVw.showsVerticalScrollIndicator = NO;
    srcVw.pagingEnabled = NO;

    //add the scrollview to the view
    srcVw = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width,self.view.frame.size.height)];
    srcVw.pagingEnabled = YES;
    [srcVw setAlwaysBounceVertical:NO];
    //setup internal views
    NSInteger numberOfViews = imagesArray.count;
    for (int i = 0; i < numberOfViews; i++) {
        CGFloat xOrigin = i * self.view.frame.size.width;
        image = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0,self.view.frame.size.width,400)];
        /*
        [image setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat: @"http://www.junkremoval.co.uk/bedspace/%@",[imagesArray objectAtIndex:i]]]];
        */
        image.image = [imagesArray objectAtIndex:i];

        image.contentMode = UIViewContentModeScaleAspectFit;
      //  image.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image.image.size};




        [srcVw addSubview:image];

    }
    //set the scroll view content size
    srcVw.contentSize = CGSizeMake(self.view.frame.size.width *numberOfViews,
                                   self.view.frame.size.height);
    [self.view addSubview:srcVw];

    [image setTag:ZOOM_VIEW_TAG];

    [image setUserInteractionEnabled:YES];


    // add gesture recognizers to the image view
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
    UITapGestureRecognizer *twoFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTwoFingerTap:)];

    [doubleTap setNumberOfTapsRequired:2];
    [twoFingerTap setNumberOfTouchesRequired:2];

    [image addGestureRecognizer:singleTap];
    [image addGestureRecognizer:doubleTap];
    [image addGestureRecognizer:twoFingerTap];




    // calculate minimum scale to perfectly fit image width, and begin at that scale
    float minimumScale = [srcVw frame].size.width  / [image frame].size.width;
    [srcVw setMinimumZoomScale:minimumScale];
    [srcVw setZoomScale:minimumScale];


    }



/************************************** NOTE **************************************/
/* The following delegate method works around a known bug in zoomToRect:animated: */
/* In the next release after 3.0 this workaround will no longer be necessary      */
/**********************************************************************************/
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
    [scrollView setZoomScale:scale+0.01 animated:NO];
    [scrollView setZoomScale:scale animated:NO];
}

#pragma mark TapDetectingImageViewDelegate methods

- (void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer {
    // single tap does nothing for now
}

- (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer {
    // double tap zooms in
    float newScale = [srcVw zoomScale] * ZOOM_STEP;
    CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];
    [srcVw zoomToRect:zoomRect animated:YES];
}

- (void)handleTwoFingerTap:(UIGestureRecognizer *)gestureRecognizer {
    // two-finger tap zooms out
    float newScale = [srcVw zoomScale] / ZOOM_STEP;
    CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];
    [srcVw zoomToRect:zoomRect animated:YES];
}

#pragma mark Utility methods

- (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 = [srcVw frame].size.height / scale;
    zoomRect.size.width  = [srcVw 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;
}
share|improve this question

1 Answer 1

I guess you have all you need here

share|improve this answer
    
Please read this meta post: meta.stackexchange.com/questions/8231/… –  PeeHaa Jun 12 '13 at 12:09
1  
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. –  bensiu Jun 12 '13 at 12:13
    
As others have said, you need to add the relevant parts otherwise this is not an acceptable answer. –  griotspeak Jul 23 '14 at 21:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.