0

so I know this has been posted numerous times (I have extensively searched this site along with many other resources for answers to no avail :C )

I am relatively new to iOS programming and am trying to make an image viewer with zooming and panning capabilities. Based on just looking around for answers, I have concluded that I should build a structure something like this:

- UIViewController
- - UIView
- - - UIScrollView (paginated)
- - - - UIView
- - - - - UIScrollView
- - - - - - UIImageView
- - - - - UIScrollView
- - - - - - UIImageView
- - - - - UIScrollView
- - - - - - UIImageView
- - - - - ...

I've meddled with my code but I am unable to get the zooming and panning correct, either nothing will happen, or the zoom will cause the whole gallery to jump. What I've built is a simple gallery like thing so far with the following code. You can easily test this by creating a new Single-View Application and replacing the code in ViewController.m with the one below.

In the file below:, the structure is as follow:

- UIViewController
- - UIView
- - - UIScrollView (Scroller)
- - - - UIView (mainView)
- - - - - UIScrollView (SubScroller)
- - - - - - UIImageView (ImgView)
- - - - - UIScrollView (SubScroller)
- - - - - - UIImageView (ImgView)
- - - - - UIScrollView (SubScroller)
- - - - - - UIImageView (ImgView)
- - - - - ...

ViewController.m

#import "ViewController.h"

@interface ViewController () {
    BOOL statusBarHidden ;
    UIView *mainView;
}

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    statusBarHidden = NO;

    NSMutableArray *images = [[NSMutableArray alloc] init];
    [images addObject:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://img.mangastream.to/manga/one-piece/001/OP%20Cover%2001.jpg"]]]];
    [images addObject:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://img.mangastream.to/manga/one-piece/001/OP001-01.jpg"]]]];
    [images addObject:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://img.mangastream.to/manga/one-piece/001/OP001-01.jpg"]]]];
    [images addObject:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://img.mangastream.to/manga/one-piece/001/OP001-01.jpg"]]]];

    //How many pages do we want?
    int PageCount = images.count;

    //Setup scroll view
    UIScrollView *Scroller = [[UIScrollView alloc] initWithFrame:[self.view frame]];

    Scroller.backgroundColor = [UIColor purpleColor];
    Scroller.pagingEnabled = YES;
    Scroller.userInteractionEnabled = YES;
    Scroller.contentSize = CGSizeMake(PageCount * Scroller.bounds.size.width, Scroller.bounds.size.height);

    Scroller.delegate = self;
    Scroller.maximumZoomScale = 4.0;
    Scroller.minimumZoomScale = 0.75;

    //Setup Each View Size
    CGRect ViewSize = Scroller.bounds;

    mainView = [[UIView alloc]initWithFrame:ViewSize];
    CGRect mainViewSize = mainView.bounds;

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doThis:)];
    [Scroller addGestureRecognizer:tap];

    for(id image in images) {
        UIScrollView *SubScroller = [[UIScrollView alloc] initWithFrame:mainViewSize];

        CGRect SubViewSize = SubScroller.bounds;

        UIImageView *ImgView = [[UIImageView alloc] initWithFrame:SubViewSize];
        [ImgView setImage:image];
        [ImgView setContentMode: UIViewContentModeScaleAspectFit];

        [SubScroller addSubview:ImgView];
        [mainView addSubview:SubScroller];

        mainViewSize = CGRectOffset(mainViewSize, Scroller.bounds.size.width, 0);
}
    [Scroller addSubview:mainView];
    [self.view addSubview:Scroller];
    [Scroller setContentOffset:CGPointMake(Scroller.frame.size.width * (PageCount - 1), 0.0f) animated:NO];
}

- (void)doThis:(UIGestureRecognizer*)tap {
    if(statusBarHidden == NO) {
        [self.navigationController setNavigationBarHidden:YES animated:YES];
        [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
        statusBarHidden = YES;
    } else {
        [self.navigationController setNavigationBarHidden:NO animated:YES];
        [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
        statusBarHidden = NO;
    }

}

-(BOOL)prefersStatusBarHidden {
    return YES;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

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

@end

Thank you so much in advance!

nine7ySix
  • 486
  • 2
  • 13

0 Answers0