Zooming within Nested UIScrollViews

2020-07-29 03:53发布

问题:

Its a familiar topic Im creating a graphic novel using UIScrollview. The plan is to be able to page along the novel with a top level uiscrollview. Within this are a series of nested uiscrollviews which have a uimageview embedded within each. The idea is that large images can be panned around and zoomed into but still paged along. I have most of this working but I cant get the zoom working. I have implemented the zoom delegate method but to no avail, zooming just pans the image around. I suspect this is something due to embedded nature of the nested scrollviews but I cant see what im missing. I had a look at the Apple scrollview suite example but couldnt find an answer.

This is what I have working so far:

  - (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:

Worked this out, for anyone else trying to do this.

One thing I missed a line of code out on the post above which is quite fundamental!, in the viewdidLoad method just above [self.view addSubview:scrollview]; there should be the following which calculates the width of all of the subviews

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

The code in the -(UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView { method should be:

return [scrollView.subviews objectAtIndex:0];  

This allows you to zoom and pan around images and also page along a set of images very nicely. Just got to work out how to zoom back to the original size automagically when moving on to the next image as its stays zoomed upon paging along..