所以我知道这已被多次发布(我已经广泛搜索了这个网站以及许多其他资源以获得无效答案:C)
我对iOS编程比较陌生,我正在努力使图像查看器具有缩放和平移功能。基于只是寻找答案,我得出结论,我应该建立一个像这样的结构:
- UIViewController
- - UIView
- - - UIScrollView (paginated)
- - - - UIView
- - - - - UIScrollView
- - - - - - UIImageView
- - - - - UIScrollView
- - - - - - UIImageView
- - - - - UIScrollView
- - - - - - UIImageView
- - - - - ...
我已经干预了我的代码,但我无法让缩放和平移正确,或者什么都不会发生,或者缩放会导致整个画廊跳转。到目前为止,我所构建的是一个简单的图库,其代码如下。您可以通过创建一个新的单视图应用程序并将 ViewController.m 中的代码替换为下面的代码来轻松测试。
在下面的文件中:,结构如下:
- 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
提前非常感谢你!