在嵌套的UIScrollViews中缩放

时间:2011-05-11 21:10:37

标签: iphone objective-c uiscrollview uiscrollviewdelegate

这是一个熟悉的主题我使用UIScrollview创建一个图形小说。该计划是能够通过顶级uiscrollview在小说中翻页。其中包含一系列嵌套的uiscrollviews,其中嵌入了uimageview。我们的想法是,大型图像可以平移并放大,但​​仍然可以分页。我有大部分工作,但我不能让变焦工作。我已经实现了缩放委托方法,但无济于事,缩放只是平移图像。我怀疑这是由于嵌套滚动视图的嵌入性质,但我无法看到我失踪了什么。我看了一下Apple的scrollview套件示例,但无法找到答案。

这是我到目前为止所做的工作:

  - (void)viewDidLoad

{

[super viewDidLoad];


UIScrollView *scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
scrollview.pagingEnabled = YES;
scrollview.scrollEnabled =YES;
scrollview.clipsToBounds = NO;
scrollview.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollview.showsHorizontalScrollIndicator = YES;
scrollview.backgroundColor = [UIColor blackColor];

NSInteger viewcount=4;
NSArray *images = [NSArray arrayWithObjects:[UIImage imageNamed:@"mars.png"],[UIImage imageNamed:@"mercury.png"],[UIImage imageNamed:@"neptune.png"],[UIImage imageNamed:@"venus.png"],nil];

for (int i = 0; i <viewcount; i++)
{
   CGFloat x = i * self.view.frame.size.width;

    subView = [[UIScrollView alloc]initWithFrame:CGRectMake(x, 0, self.view.frame.size.width, self.view.frame.size.height)];


    [subView setBackgroundColor:[UIColor blackColor]];
    [subView setCanCancelContentTouches:NO];
    subView.clipsToBounds = NO;    // default is NO, we want to restrict drawing within our scrollview
    subView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    aImageView = [[UIImageView alloc ] initWithImage:[images objectAtIndex:i]];
    [self.aImageView setTag:ZOOM_VIEW_TAG];
    [subView addSubview:aImageView];
    [subView setContentSize:CGSizeMake(aImageView.frame.size.width, subView.frame.size.height)];
    subView.minimumZoomScale = 1;
    subView.maximumZoomScale = 3;
    subView.delegate = self;
    [subView setScrollEnabled:YES];
     subView.contentSize = aImageView.frame.size;
    [scrollview addSubview:subView];
    [subView release];
}



[self.view addSubview:scrollview];

}



-(UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView {
NSLog (@"test");
UIView * view = nil;
view = [subView viewWithTag:ZOOM_VIEW_TAG];

return view;
}

1 个答案:

答案 0 :(得分:4)

为其他任何试图这样做的人解决了这个问题。

有一件事我在上面的帖子上错过了一行代码,这在viewdidLoad方法中非常基础! 就在上面[self.view addSubview:scrollview];应该有以下计算所有子视图的宽度

 scrollview.contentSize = CGSizeMake(self.view.frame.size.width*viewcount,self.view.frame.size.height);

- (UIView *)viewForZoomingInScrollView中的代码:(UIScrollView *)scrollView { 方法应该是:

return [scrollView.subviews objectAtIndex:0];  

这使您可以缩放和平移图像,也可以非常好地沿着一组图像进行翻页。刚刚开始研究如何在转移到下一个图像时自动缩放回到原始大小,因为它在分页时保持缩放。