1

它是一个熟悉的话题我正在使用 UIScrollview 创建一本图画小说。计划是能够使用顶级 uiscrollview 沿着小说翻页。其中有一系列嵌套的 uiscrollviews,其中每个都嵌入了一个 uimageview。这个想法是大图像可以平移和放大,但仍然可以翻页。我有大部分工作,但我无法让变焦工作。我已经实现了缩放委托方法,但无济于事,缩放只是平移图像。我怀疑这是由于嵌套滚动视图的嵌入性质造成的,但我看不出我缺少什么。我查看了 Apple 滚动视图套件示例,但找不到答案。

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

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

1 回答 1

4

解决了这个问题,对于其他试图这样做的人。

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

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

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

return [scrollView.subviews objectAtIndex:0];  

这使您可以缩放和平移图像,还可以很好地沿着一组图像翻页。只是要弄清楚如何在移动到下一个图像时自动缩放回原始大小,因为它在分页时保持缩放..

于 2011-05-12T17:31:38.597 回答