[英]How to have images that pinch-zoom in a paginating scroll view in iOS?
I'm trying to use a scroll view to have pagination with pages of subviews that are images that can be pinched zoomed on iOS. 我正在尝试使用滚动视图来对子视图的页面进行分页,这些子视图的图像可以在iOS上进行缩放。 The pagination works, but as soon as an image is pinch-zoomed, the app crashes with EXEC_BAD_ACCESS(code=1,address=...) 分页有效,但是只要捏住图像,应用程序就会崩溃,并显示EXEC_BAD_ACCESS(code = 1,address = ...)
I'm aware that it's a bit odd to swipe a zoomed image to pan the image and also swipe to paginate, but in the real app, the pagination will be done with a page control. 我知道滑动缩放的图像以平移图像并滑动以进行分页有点奇怪,但是在实际应用中,分页将使用页面控件完成。 Also I think it could work like the preview app. 我也认为它可以像预览应用程序一样工作。 If an image is zoomed, panning will go down to the bottom of the image and then after that is reached, it goes to the next image. 如果缩放图像,则平移将下降到图像的底部,然后达到该水平后,它将转到下一张图像。
Is this possible? 这可能吗?
Here's an example: 这是一个例子:
AppDelegate.m AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
ScrollerViewController *viewController = [[ScrollerViewController alloc] init];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
return YES;
}
ScrollerViewController.m - the outer pagination view controller ScrollerViewController.m-外部分页视图控制器
- (void)viewDidLoad {
[super viewDidLoad];
// outer scroll view for paging with two pages
CGRect frame = CGRectMake(0,0,self.view.bounds.size.width,self.view.bounds.size.height);
UIScrollView *pagingScroller = [[UIScrollView alloc] initWithFrame:frame];
pagingScroller.pagingEnabled = YES;
pagingScroller.scrollsToTop = NO;
pagingScroller.userInteractionEnabled = YES;
pagingScroller.contentSize = CGSizeMake(self.view.bounds.size.width*2,self.view.bounds.size.height);
// first page
ImageViewController *page1 = [[ImageViewController alloc] init];
page1.filename = @"cat.jpg";
page1.view.frame = CGRectMake(0,0,self.view.bounds.size.width,self.view.bounds.size.height);
[pagingScroller addSubview:page1.view];
// second page
ImageViewController *page2 = [[ImageViewController alloc] init];
page2.filename = @"dog.jpg";
page2.view.frame = CGRectMake(self.view.bounds.size.width,0,self.view.bounds.size.width,self.view.bounds.size.height);
[pagingScroller addSubview:page2.view];
self.view = pagingScroller;
}
ImageViewController.m - the pinch-zoom image ImageViewController.m-缩小图像
- (void)viewDidLoad {
[super viewDidLoad];
// scroll view for pinch zooming
CGRect frame = CGRectMake(0,0,self.view.bounds.size.width,self.view.bounds.size.height);
UIScrollView *zoomScroller = [[UIScrollView alloc] initWithFrame:frame];
zoomScroller.minimumZoomScale = 1.0;
zoomScroller.maximumZoomScale = 5.0;
zoomScroller.userInteractionEnabled = YES;
zoomScroller.delegate = self;
imageView = [[UIImageView alloc] initWithFrame:frame];
imageView.userInteractionEnabled = YES;
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.image = [UIImage imageNamed:filename];
[zoomScroller addSubview:imageView];
self.view = zoomScroller;
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return imageView;
}
The full project is at https://github.com/tomkincaid/ZoomScrollTest 完整项目位于https://github.com/tomkincaid/ZoomScrollTest
I can test that the pinch zoom works by changing 我可以通过更改来测试捏缩放
ScrollerViewController *viewController = [[ScrollerViewController alloc] init];
to 至
ImageViewController *viewController = [[ImageViewController alloc] init];
viewController.filename = @"cat.jpg";
Its been quite a while that you posted your question. 您发布问题已有一段时间了。 I bet you fixed it already yourself but I want to make sure other people can use your code. 我敢打赌,您已经自己修复了它,但我想确保其他人可以使用您的代码。
However I downloaded your small GitHub project and found that you get the crash because you don't retain the ImageViewController's page1 and page2 in [ScrollerViewController viewDidLoad]. 但是,我下载了一个小型GitHub项目,发现崩溃了,因为您没有将ImageViewController的page1和page2保留在[ScrollerViewController viewDidLoad]中。 The views them selfs don't retain their controllers therefor the controllers get released after viewDidLoad in your case. 他们自己的视图不会保留其控制器,因此在您的情况下,控制器在viewDidLoad之后被释放。 Then when you pinch on the image scroll view it calls for its delegate but it is already deallocated. 然后,当您捏住图像滚动视图时,它会调用其委托,但已被释放。
To fix this I added two ImageViewController properties to the ScrollerViewController class and stored the controller objects there. 为了解决这个问题,我在ScrollerViewController类中添加了两个ImageViewController属性,并将控制器对象存储在那里。
@interface ScrollerViewController ()
@property (strong) ImageViewController *page1;
@property (strong) ImageViewController *page2;
@end
In [ScrollerViewController viewDidLoad] I added at the end: 在[ScrollerViewController viewDidLoad]中,我在末尾添加了:
self.page1 = page1;
self.page2 = page2;
I hope that someone may find this information useful. 我希望有人会发现此信息有用。 Maybe you want to update your GitHub project so that it will compile and run. 也许您想更新GitHub项目,以便它可以编译并运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.