2014-02-23 96 views
1

我正在嘗試使用滾動視圖對可在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"; 

回答

1

它已經相當長一段時間,你發佈你的問題。我敢打賭,你已經自己修復了它,但我想確保其他人可以使用你的代碼。

但是我下載了你的小GitHub項目,發現你得到了崩潰,因爲你沒有在[ScrollerViewController viewDidLoad]中保留ImageViewController的page1和page2。他們自己的觀點並不保留他們的控制器,因爲控制器在viewDidLoad之後被釋放。然後當你捏住圖像滾動視圖時,它會調用它的委託,但它已經被釋放。

爲了解決這個問題,我在ScrollerViewController類中添加了兩個ImageViewController屬性,並將控制器對象存儲在那裏。

@interface ScrollerViewController() 

@property (strong) ImageViewController *page1; 
@property (strong) ImageViewController *page2; 

@end 

在[ScrollerViewController viewDidLoad中]我說在最後:

self.page1 = page1; 
self.page2 = page2; 

我希望有人可能會覺得這個信息有用。也許你想更新你的GitHub項目,以便它能夠編譯和運行。

+0

感謝您的回答。我其實從來沒有想過並放棄。 –