2012-06-19 44 views
1

當我嘗試滾動我的UIScrollView時,出現主要的Exc_Bad_Access錯誤。這個程序很簡單。我有一個UIViewController在AppDelegate中添加。然後,這是ViewController中的代碼。這工作正常滾動,但只要我加tmpScrollView.delegate = self。當我嘗試縮放時,該程序將不會滾動或縮放,並會給我出現Exc_Bad_Access錯誤。UIScrollView縮放使用Exc_Bad_Access崩潰應用程序

@interface MainViewController : UIViewController &ltUIScrollViewDelegate> 
@property (nonatomic,retain) UIScrollView *tmpScrollView; 
@property (nonatomic,retain) UIView *containerView; 
@end 

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 

// Do any additional setup after loading the view. 
tmpScrollView = [[UIScrollView alloc] initWithFrame:self.view.frame]; 
tmpScrollView.delegate = self; 
[self.view addSubview:tmpScrollView]; 

// Set up the container view to hold our custom view hierarchy 
CGSize containerSize = CGSizeMake(640.0f, 640.0f); 
self.containerView = [[UIView alloc] initWithFrame:(CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=containerSize}]; 
[self.tmpScrollView addSubview:self.containerView]; 

// Set up custom view hierarchy 
UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 640.0f, 80.0f)]; 
redView.backgroundColor = [UIColor redColor]; 
[self.containerView addSubview:redView]; 

UIView *blueView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 560.0f, 640.0f, 80.0f)]; 
blueView.backgroundColor = [UIColor blueColor]; 
[self.containerView addSubview:blueView]; 

UIView *greenView = [[UIView alloc] initWithFrame:CGRectMake(160.0f, 160.0f, 320.0f, 320.0f)]; 
greenView.backgroundColor = [UIColor greenColor]; 
[self.containerView addSubview:greenView]; 

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"slow.png"]]; 
imageView.center = CGPointMake(320.0f, 320.0f); 
[self.containerView addSubview:imageView]; 

//Tell the scroll view the size of the contents 
self.tmpScrollView.contentSize = containerSize; 
} 

-(void)viewWillAppear:(BOOL)animated 
{ 
NSLog(@"Will Appear"); 
[super viewWillAppear:animated]; 

// Set up the minimum & maximum zoom scales 
CGRect scrollViewFrame = self.tmpScrollView.frame; 
CGFloat scaleWidth = scrollViewFrame.size.width/self.tmpScrollView.contentSize.width; 
CGFloat scaleHeight = scrollViewFrame.size.height/self.tmpScrollView.contentSize.height; 
CGFloat minScale = MIN(scaleWidth, scaleHeight); 

self.tmpScrollView.minimumZoomScale = minScale; 
self.tmpScrollView.maximumZoomScale = 1.0f; 
self.tmpScrollView.zoomScale = minScale; 

//***** Here is the line for setting the delegate 
self.tmpScrollView.delegate = self; 

[self centerScrollViewContents]; 
} 


-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale 
{ 
NSLog(@"End Zoom"); 
} 

-(UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView { 
// Return the view that we want to zoom 
return self.containerView; 
} 

-(void)scrollViewDidZoom:(UIScrollView *)scrollView { 
// The scroll view has zoomed, so we need to re-center the contents 
[self centerScrollViewContents]; 
} 
@end 

感謝您的任何幫助。這已經快把我逼瘋了,現在一天...

編輯:添加centerScrollViewContens太:

- (void)centerScrollViewContents { 
    CGSize boundsSize = self.tmpScrollView.bounds.size; 
    CGRect contentsFrame = self.containerView.frame; 

    if (contentsFrame.size.width < boundsSize.width) { 
     contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width)/2.0f; 
    } else { 
     contentsFrame.origin.x = 0.0f; 
    } 

    if (contentsFrame.size.height < boundsSize.height) { 
     contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height)/2.0f; 
    } else { 
     contentsFrame.origin.y = 0.0f; 
    } 

    self.containerView.frame = contentsFrame; 
} 
+0

一些尋求在第一線的錯誤,它應該是: 的UIViewController &lt; UIScrollViewDelegate> – Dhruv

+0

&lt是<的html代碼。當我使用<***>時,它最初寫入堆棧溢出時並未顯示出來。我的實際代碼是 Jomnipotent17

回答

0

任何代碼你提供工作對我end.May罰款是可能有某種問題在你的「centerScrollViewContents」方法中。你可以發佈這個方法的代碼嗎?

+0

好吧,增加centerScrollViewContents – Jomnipotent17

+0

它仍然在我的情況下工作,並提供一個輸出像上面的紅色視圖然後綠色視圖中間然後藍色視圖在最後和灰色的背景。嘗試「清理」你的項目並重置模擬器設置,然後再次運行它。或者用上述所有代碼創建一個新項目並運行它。它肯定會起作用。 –

+0

是的,我也可以看到意見。但嘗試用alt鍵放大。當我這樣做。它與Exc_Bad_Access – Jomnipotent17

0

您需要計算滾動視圖的最小縮放比例。縮放比例爲1意味着內容以正常大小顯示。小於1的縮放比例顯示縮小的內容,而大於1的縮放比例顯示放大的內容。要獲得最小的縮放比例,您需要計算需要縮小的距離,以便圖像可以貼合地放入您的滾動視圖的邊界基於其寬度。然後,根據圖像的高度進行相同的操作。這兩個縮放比例的最小值將是滾動視圖的最小縮放比例。這給你一個縮放比例,你可以在完全縮小時看到整個圖像。

斯威夫特:

let scrollViewFrame = scrollView.frame 
let scaleWidth = scrollViewFrame.size.width/scrollView.contentSize.width 
let scaleHeight = scrollViewFrame.size.height/scrollView.contentSize.height 
let minScale = min(scaleWidth, scaleHeight); 
scrollView.minimumZoomScale = minScale; 

Objective-C的

CGFloat scrollViewFrame = scrollView.frame 
CGFloat scaleWidth = scrollViewFrame.size.width/scrollView.contentSize.width 
CGFloat scaleHeight = scrollViewFrame.size.height/scrollView.contentSize.height 
CGFloat minScale = min(scaleWidth, scaleHeight); 
scrollView.minimumZoomScale = minScale; 

從其他項目和個人經驗做在iOS重GPU(無盡的滾動,最大位圖大小,等等),我注意到每個設備都有自己的閾值。對於過去的項目,我也硬了下面的值作爲minimumZoomScale防止崩潰:

scale : 
     { 
       iPad : { 
        min : 0.8 
       , max : 1.6 
      } 
      , iPhone : { 
        min : 0.25 
       , max : 1.6 
      } 
      , iPhone6 : { 
        min : 0.3333 
       , max : 1.6 
      } 
      , iPhoneResizable: { 
        min : 1 
       , max : 1.6 
      } 
     } 

初學者偉大的教程:http://www.raywenderlich.com/76436/use-uiscrollview-scroll-zoom-content-swift

相關問題