Enable scaling in uiscrollview with paging and scrolling enabled

I have uiscrollview that I upload an array of images to scroll through images with an effect similar to the effect of photos on iOS. I am using this code:

  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *path = [paths objectAtIndex:0];
    NSString *imageFilePath = [path stringByAppendingPathComponent:@"newPics"];
    fileExists   = [[NSFileManager defaultManager] fileExistsAtPath:imageFilePath];
    if(fileExists){

        imgs=[[NSMutableArray  alloc]init];


    [imgs addObject:[UIImage imageWithContentsOfFile:[path stringByAppendingPathComponent:@"newPics/pic1.png"]]];
    [imgs addObject:[UIImage imageWithContentsOfFile:[path stringByAppendingPathComponent:@"newPics/pic2.png"]]];
    [imgs addObject:[UIImage imageWithContentsOfFile:[path stringByAppendingPathComponent:@"newPics/pic3.png"]]];
    [imgs addObject:[UIImage imageWithContentsOfFile:[path stringByAppendingPathComponent:@"newPics/pic4.png"]]];


    CGSize imageSize;
    imageSize.height = 648;
    imageSize.width = 927;
    self.scroll1.frame = CGRectMake(0,0,imageSize.width,imageSize.height);

    self.scroll1.contentSize = CGSizeMake(imageSize.width * imgs.count, imageSize.height );
    self.scroll1.minimumZoomScale=1.0;
    self.scroll1.maximumZoomScale=3.0;
    self.scroll1.pagingEnabled = YES;


    self.scroll1.delegate=self;

    CGFloat xPos = 0.0;

    for (UIImage *image in imgs) {
        imageView = [[UIImageView alloc] initWithImage:image];
        imageView.frame = CGRectMake(xPos, 0.0, imageSize.width, imageSize.height);
        [self.scroll1 addSubview:imageView];
        xPos += imageSize.width;

    }


    }

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    int positionScrollPrim = scrollView.contentOffset.x;

    int positionScroll = positionScrollPrim / 927 ;

    imageView = [[UIImageView alloc] initWithImage:[imgs objectAtIndex:positionScroll]];
    //UIImage *tmep =  [imgs objectAtIndex:1];

    return imageView;
}

I can’t get the right result, instead I get a bunch of weird behaviors like improper scaling and no longer scroll when I try to zoom. I assume that my problem is in the viewForZoomingInScrollView section, so I tried to solve it using the code above. But I just can't get it to work. What am I missing?

+4
source share
1 answer

: , , .

+5

All Articles