图像的阵列中的UIScrollView和寻呼成功显示,但无法正常放大,滚动型图像(Array of

2019-08-02 02:02发布

我的工作的UIScrollView与ImageArray。 滚动和翻页的工作,但我不能够放大每个图像。 我的形象滚动型的代码如下: -

#define IMAGE_WIDTH 320

#define IMAGE_HEIGHT 360

 - (void)viewDidLoad
 {
[super viewDidLoad];

// TODO – fill with your photos
NSArray *photos = [[NSArray arrayWithObjects:
                    [UIImage imageNamed:@"photo1m.jpg"],
                    [UIImage imageNamed:@"photo2m.jpg"],
                    [UIImage imageNamed:@"photo3m.jpg"],
                    [UIImage imageNamed:@"photo4m.jpg"],
                    nil] retain];
// note that the view contains a UIScrollView in aScrollView

    int i=0;
    for ( NSString *image in photos )
    {
       UIImage *images = [photos objectAtIndex:i];
       imageView = [[UIImageView alloc] initWithImage:images];
       imageView.contentMode = UIViewContentModeScaleAspectFit;
       imageView.clipsToBounds = YES;

       imageView.frame = CGRectMake( IMAGE_WIDTH * i++, 0, IMAGE_WIDTH, IMAGE_HEIGHT);

       [self.scrollViewimages addSubview:imageView];
      [imageView release];
     }
      self.scrollViewimages.contentSize = CGSizeMake(IMAGE_WIDTH*i, IMAGE_HEIGHT);
      self.scrollViewimages.delegate = self; 
 }

需要帮助实现每个图像的双指缩放。 请帮忙 !

Answer 1:

for ( NSString *image in photos )
    {
       UIImage *images = [photos objectAtIndex:i];
       imageView = [[UIImageView alloc] initWithImage:images];
       imageView.contentMode = UIViewContentModeScaleAspectFit;
       imageView.clipsToBounds = YES;
       imageView.tag = 1;

       UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake( IMAGE_WIDTH * i++, 0, IMAGE_WIDTH, IMAGE_HEIGHT)];
       scrollView.delegate = self;
       scrollView.maximumZoomScale = 3.0f
       imageView.frame = scrollView.bounds;
       [scrollView addSubview:imageView];
       [imageView release];

       [self.scrollViewimages addSubview:scrollView];

     }

和实施UIScrollViewDelegate委托方法

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return [scrollView viewWithTag:1];
}


Answer 2:

#define IMAGE_FOR_ZOOM_TAG (1)

呼叫以下自定义方法

-(void) setupProductImageViewContainerUI
{
    UIWindow *window = [[[UIApplication sharedApplication] delegate] window];

    if(self.dimView)
    {
        for(UIView *subView in self.dimView.subviews)
            [subView removeFromSuperview];
        [self.dimView removeFromSuperview]; self.dimView = nil;
    }

    self.dimView = [[UIView alloc] initWithFrame:window.bounds];
    self.dimView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:.5];
    self.dimView.userInteractionEnabled = YES;
    [window addSubview:self.dimView];

    self.imgContainerView = [[UIView alloc] init];
    self.imgContainerView.frame = CGRectMake(0, 0, window.frame.size.width, window.frame.size.height);
    self.imgContainerView.backgroundColor = [UIColor whiteColor];
    [self.dimView addSubview:self.imgContainerView];

    UIScrollView *mainScrollView = [[UIScrollView alloc] initWithFrame:window.bounds];
    mainScrollView.pagingEnabled = YES;
    mainScrollView.delegate = self;
    mainScrollView.showsHorizontalScrollIndicator = NO;
    mainScrollView.showsVerticalScrollIndicator = NO;
    [self.imgContainerView addSubview:mainScrollView];
    CGRect innerScrollFrame = mainScrollView.bounds;

    IndexOfSlidingPhoto = 0;
    for(int i = 0 ; i < listOfImages.count; i++)
    {
        UIImage *imgProduct = [GeneralClass getImageForSreenFromScreenTable:@"" orCustomTable:@"" OrDefaultImga:[listOfImages objectAtIndex:i]];
        UIImageView *imageForZooming = [[UIImageView alloc] initWithImage:imgProduct];
        imageForZooming.frame = CGRectMake(0, 0, window.frame.size.width, window.frame.size.height);
        imageForZooming.tag = IMAGE_FOR_ZOOM_TAG;
        imageForZooming.contentMode = UIViewContentModeScaleAspectFit;

        UIScrollView *pageScrollView = [[UIScrollView alloc] initWithFrame:innerScrollFrame];
        pageScrollView.minimumZoomScale = 1.0f;
        pageScrollView.maximumZoomScale = 20;
        pageScrollView.zoomScale = 1.0f;
        pageScrollView.contentSize = imageForZooming.bounds.size;
        pageScrollView.delegate = self;
        pageScrollView.showsHorizontalScrollIndicator = NO;
        pageScrollView.showsVerticalScrollIndicator = NO;
        [pageScrollView addSubview:imageForZooming];        
        [mainScrollView addSubview:pageScrollView];

        if (i < listOfImages.count -1)
            innerScrollFrame.origin.x += innerScrollFrame.size.width;

    mainScrollView.contentSize = CGSizeMake(innerScrollFrame.origin.x + innerScrollFrame.size.width, mainScrollView.bounds.size.height);


    [window bringSubviewToFront:self.dimView];
}

#pragma Mark -
#pragma Mark - UIScrollView Delegate Methods

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return [scrollView viewWithTag:IMAGE_FOR_ZOOM_TAG];
}


文章来源: Array of Images display in UIScrollview and Paging successfully but cannot properly zoom-in images in scrollview