0

在描述我的问题一个非常基本的问题之前,有没有办法可以UIScrollView从这个方法返回对象

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView

现在我的问题是,我有一个父母,里面scrollview有多个孩子(所有水平和分页都启用)。scrollview

每个孩子scrollview都有一个ImageView。我zoomScale从服务器获取因素,基于image,我必须缩放它,问题是当我尝试缩放里面scrollView有一个孩子image时,它只缩放最后一个孩子scrollview

所以我认为,我需要找到一种方法,我们可以scrollview将上述书面委托的形式返回给我们。

有人知道如何解决这个问题吗?


这是 UIView 类,我在此添加了滚动视图和图像,并返回了滚动视图代表的图像视图

示例代码

     - (id)initWithFrame:(CGRect)frame image:(UIImage *)image
{
    self = [super initWithFrame:frame];
    if (self) {

        self.backgroundColor=[UIColor yellowColor];      
        self.imageView = [[UIImageView alloc] initWithImage:image];
        self.imageView.tag = VIEW_FOR_ZOOM_TAG;
        self.imageView.frame=frame;//CGRectMake(8,8 ,305, 400);
        [self.imageView setContentMode:UIViewContentModeScaleAspectFit];


        self.scrollView = [[UIScrollView alloc] initWithFrame:frame];
        self.scrollView.minimumZoomScale = 0.4f;
        self.scrollView.maximumZoomScale = 2.0f;
        self.scrollView.backgroundColor=[UIColor redColor];
        self.scrollView.zoomScale = 1.0f;
        self.scrollView.contentSize = self.imageView.bounds.size;
        self.scrollView.delegate = self;
        self.scrollView.showsHorizontalScrollIndicator = NO;
        self.scrollView.showsVerticalScrollIndicator = NO;
        [self.scrollView setCanCancelContentTouches:NO];
        [self.scrollView addSubview:self.imageView];
        [self addSubview:self.scrollView];

        NSLog(@"ScrollViewPostion %f",self.scrollView.frame.origin.x);
    }
    return self;
}

#pragma -mark
#pragma -mark ScrollViewDelegates
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return [scrollView viewWithTag:VIEW_FOR_ZOOM_TAG];
}

在我的主 viewController 类中,我正在这样做,但它没有在滚动视图中正确添加所有图像

 NSLog(@"Inner scrollFrame and content size %f %f",innerScrollFrame.origin.x,mainScrollView.contentSize.width);
    JoinSeeSubView *subView=[[JoinSeeSubView alloc] initWithFrame:innerScrollFrame image:img];
    [self.mainScrollView addSubview:subView];


        if (i < [self.allUrlArray count]-1) {
            innerScrollFrame.origin.x += innerScrollFrame.size.width;
    }
    i++;
    mainScrollView.contentSize = CGSizeMake(innerScrollFrame.origin.x + innerScrollFrame.size.width, mainScrollView.bounds.size.height);
4

2 回答 2

1

我不完全确定问题出在哪里,但是您正在循环浏览多个图像并重新编写imageViewandpageScrollView属性,因此在添加最后一个图像之后,这就是您的指针指向的位置,这就是为什么它只会缩放您的最后一个图片。

最好的方法是创建一个UIView处理每个图像及其父滚动视图的子类,然后将该类添加到您的主视图中UIScrollView

如果您不想这样做,则必须找到另一种方法来访问每个单独UIScrollView的 ,可能使用标签,但不建议这样做


编辑 - 一些基本代码

@interface A : UIView {
    UIScrollView *scrollView
    UIImage View *imageView;

}
// initialization method that gets the image and the CGRect
@end
@implementation A
// implement initialization that loads the UIImage into the imageView, and set scrollView.delegate = self
// implement scroll view delegate method viewForZooming and return the imageView
@end

然后,在您现在正在使用的任何课程上:

for (int i = 0; i < imagesCount; i++) {
    UIImage *image = ...; // image i
    CGRect rect = ...; // find rect
    A *subview = [[A alloc] initWithFrame:rect image:image]
    [self.mainScroll addSubview:subview];
}
于 2012-12-26T12:36:06.523 回答
0

如果上述方法对真正有需要的人没有帮助,我以不同的方式解决了这个问题,但不确定这是否是正确的方法。

因此,我没有创建一个扩展 UIView 的新类,而是创建并排列并将内部滚动视图的所有对象存储在该数组中。所以我的代码看起来像

       UIScrollView *pageScrollView = [[UIScrollView alloc] initWithFrame:innerScrollFrame];
        pageScrollView.minimumZoomScale = 0.4f;
        pageScrollView.maximumZoomScale = 2.0f;
        pageScrollView.zoomScale = 1.0f;
        pageScrollView.backgroundColor=[UIColor clearColor];
        pageScrollView.delegate = self;
        [pageScrollView addSubview:self.imageview];
        [mainScrollView addSubview:pageScrollView];
        [self.pageScrollViewObjArray addObject:pageScrollView];

每当我需要访问包含在滚动视图中的特定图像视图时,我都会这样做:-

UIScrollView *sv=(UIScrollView*)[self.pageScrollViewObjArray objectAtIndex:swipeCounter];
sv.zoomScale=1.0f;

但正如我之前所说,我不知道这段代码的效率有多高,但是应用程序在商店上架并进行了正确测试已经有一个多月了,但没有任何问题。

于 2013-01-23T16:41:37.717 回答