如何在uiscrollview中双击缩放和两个fingure zoom uiimage

时间:2013-06-12 09:43:29

标签: iphone objective-c ios6

嗨,我实际上在uiscrollview中有多个图像。实际上我想实现图像滚动像在iPhone图像gallary。我正在从url下载图像到imagearray然后我可以在uiscrollview中成功显示和滚动这些图像来自imagearray。但我的问题是我想启用双击放大/缩小和两个fingure放大/缩小uiscrollviiew像在iPhone的gallary。比如当用户双击图像时会放大。当用户再次双击那个图像将缩放的图像。并且与twofingur手势相同的情况。我试图实现它阅读教程但不能成功实际上问题是当我双击图像doubletap图像方法称为成功但可以影响图像大小这是我已经实现的滚动和手势识别的代码 对于手势识别,我使用了苹果网站上的代码

    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;
}

1 个答案:

答案 0 :(得分:-1)

我猜你所需要的只是here