对于ios7中的多个图像,捏缩放在UIScrollview中不起作用

时间:2013-12-05 05:55:50

标签: iphone xcode uiscrollview ios7

我想捏缩放uiscrollview图像。我在uiscrollview中添加了多个图像 但是捏缩放不起作用我想捏缩放UIImageview图像。我已经提到了很多参考但仍然没有工作

提前致谢

我的代码: -

for(int i = 0;i<aryImage.count;i++)
            {

                 //Create a uiimageview

                imageView =[[UIImageView alloc]init ];

                imageView.frame = CGRectMake((280 *i),0.0, 280, 475);
                imageView.image = [UIImage imageNamed:@"default.png"];
                imageView.tag = i;
                imageView.userInteractionEnabled = YES;
                [scrollGallery addSubview:imageView];

                scrollGallery.minimumZoomScale = 1.0;

                scrollGallery.maximumZoomScale = 2.0;

                scrollGallery.delegate = self;

               [scrollGallery addSubview:imageView];

            }
            scrollGallery.contentSize = CGSizeMake(280 * aryImage.count, scrollGallery.frame.size.height);
            pageControl.numberOfPages = aryImage.count;
            pageControlBeingUsed = NO;
            pageControl.currentPage = 0;




- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return imageView;
}

2 个答案:

答案 0 :(得分:2)

试试这个:

- (void)scrollViewDidZoom:(UIScrollView *)sv
{
    UIView* zoomView = [sv.delegate viewForZoomingInScrollView:sv];
    CGRect zvf = zoomView.frame;
    if(zvf.size.width < sv.bounds.size.width)
    {
        zvf.origin.x = (sv.bounds.size.width - zvf.size.width) / 2.0;
    }
    else
    {
        zvf.origin.x = 0.0;
    }
    if(zvf.size.height < sv.bounds.size.height)
    {
        zvf.origin.y = (sv.bounds.size.height - zvf.size.height) / 2.0;
    }
    else
    {
        zvf.origin.y = 0.0;
    }
    zoomView.frame = zvf;
}

答案 1 :(得分:1)

在viewForZooming中只返回一个imageView,但添加了一个数组。这可能是您的问题的原因。尝试替换这样的代码:

UIView *containerView = [[UIView alloc] initWithFrame:scrollGallery.frame];
[scrollGallery addSubview:containerView];
scrollGallery.minimumZoomScale = 1.0;
scrollGallery.maximumZoomScale = 2.0;
scrollGallery.delegate = self;

for(int i = 0;i<aryImage.count;i++)
        {

             //Create a uiimageview

            imageView =[[UIImageView alloc]init ];

            imageView.frame = CGRectMake((280 *i),0.0, 280, 475);
            imageView.image = [UIImage imageNamed:@"default.png"];
            imageView.tag = i;
            imageView.userInteractionEnabled = YES;
            [containerView addSubview:imageView];
        }
        scrollGallery.contentSize = CGSizeMake(280 * aryImage.count, scrollGallery.frame.size.height);

        // then maybe you need to resize containerView's frame
        //..........

        pageControl.numberOfPages = aryImage.count;
        pageControlBeingUsed = NO;
        pageControl.currentPage = 0;


- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return containerView;
}

希望它会有所帮助。

UPD :还尝试禁用imageViews的userInteraction。他们可能会阻止与scrollGallery的互动。

UPD2 :将所有imageView存储在预定义的数组中。例如,

NSMutableArray *arr = [[NSMutableArray alloc] init];
for (int i=0; i<aryImage.count; i++) {
    [arr addObject:[[UIImageView alloc] init]];
}

然后在你的周期而不是

imageView =[[UIImageView alloc] init];

使用

UIImageView *imageView = [arr objectAtIndex:i];

不要忘记最后释放所有内容;)