在启用分页并启用滚动的情况下启用放大uiscrollview

时间:2014-10-09 08:44:34

标签: ios objective-c uiscrollview

我有一个uiscrollview,我加载了一系列图像,以便我可以滚动浏览图片,效果类似于IOS中的照片效果。我正在使用此代码:

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

我无法获得正确的结果,相反,当我尝试缩放时,我会得到一些奇怪的行为,例如不正确的缩放和不再滚动。我想我的问题出在viewForZoomingInScrollView部分,这就是我尝试使用上面的代码解决它的原因。但我无法让它发挥作用。我错过了什么?

1 个答案:

答案 0 :(得分:5)

常见的方法是使用两个滚动视图:外部滚动视图,每个图像都包含在另一个负责缩放的滚动视图中。