0

嗨,我实际上在 uiscrollview 中有多个图像。实际上我想像在 iphone 图片库中那样实现图像滚动。正在将图像从 url 下载到 imagearray,然后我可以在 uiscrollview 中成功显示和滚动 imagearray 中的这些图像。但我的问题是我想在 uiscrollviiew 中启用双击放大/缩小和两个手指放大/缩小,就像在 iphone 画廊中一样。就像用户双击图像会放大一样。当用户再次双击该图像时,该图像将缩放。以及双手指手势的相同情况。

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

1 回答 1

-1

我想你在这里有你需要的一切

于 2013-06-12T11:52:58.007 回答