我正在嘗試使用滾動視圖對可在iOS上縮放圖像的子視圖頁面進行分頁。分頁工作,但一旦圖像捏縮放,應用程序崩潰與EXEC_BAD_ACCESS(代碼= 1,地址= ...)如何讓iOS中的分頁滾動視圖縮小圖像?
我知道這是有點奇怪的一個縮放圖像平移圖像,並滑動到分頁,但在真正的應用程序中,分頁將使用頁面控件完成。此外,我認爲它可以像預覽應用程序一樣工作。如果圖像被縮放,則平移將下降到圖像的底部,然後到達後,它會轉到下一個圖像。
這可能嗎?
下面是一個例子:
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 - 外分頁視圖控制器
- (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 - 夾變焦圖像
- (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;
}
整個項目是在https://github.com/tomkincaid/ZoomScrollTest
我可以測試的雙指縮放的工作原理是改變
ScrollerViewController *viewController = [[ScrollerViewController alloc] init];
到
ImageViewController *viewController = [[ImageViewController alloc] init];
viewController.filename = @"cat.jpg";
感謝您的回答。我其實從來沒有想過並放棄。 –