2012-11-06 59 views
1

我有一個UIScrollView - 590 X 650 我已經在滾動視圖2個UIImages與尺寸:620 X 450 所以,我設置contentsize和屬性從而:的UIScrollView contentsize屬性

self.scroller.minimumZoomScale=0.5; 
    self.scroller.maximumZoomScale=6.0; 
    self.scroller.contentSize=CGSizeMake(1350, 950);// EDIT : 1350 because 620 + 620 
    //950 because 450 + 450 and 50 extra 

    self.scroller.contentInset=UIEdgeInsetsMake(64.0,0.0,44.0,0.0); 
    self.scroller.scrollIndicatorInsets=UIEdgeInsetsMake(64.0,0.0,44.0,0.0); 

    self.scroller.delegate=self; 

但當我嘗試運行並滾動時,即使在完全拉動滾動條以顯示第二張圖像的末尾時,也只顯示第二張圖像的一半/部分,並且滾動條會反彈回來。

我在做什麼錯在這裏?
我的內容尺寸設置/假設是否正確?

如果這個問題已經解答,請點我,因爲我找不到正確的解釋。

EDITS: * h文件

@interface ViewController : UIViewController<UIScrollViewDelegate> 
{ 
    UIScrollView *scroller; 
    UIImageView *varImage1; 
    UILabel *lblStyle; 
    BOOL isAlpha; 
    UITextView *textView; 
} 

@property (retain, nonatomic) IBOutlet UIScrollView *scroller; 
@property (retain, nonatomic) IBOutlet UIImageView *varImage1; 
@property (retain, nonatomic) IBOutlet UILabel *lblStyle; 

- (IBAction)notesClick:(id)sender; 
@property (retain, nonatomic) IBOutlet UIButton *notesClick; 

@property (retain, nonatomic) IBOutlet UITextView *textView; 


@end 

* .m文件

// calculate minimum scale to perfectly fit image width, and begin at that scale 
    float minimumScale = [scroller frame].size.width/[varImage1 frame].size.width; 
    [scroller setMinimumZoomScale:minimumScale]; 
    [scroller setZoomScale:minimumScale]; 
    scroller.contentSize= CGSizeMake(960,1170); 

    scroller.delegate=self; 

圖像尺寸1170x960 UIImage的視圖尺寸是:1170x960(寬x高)圖片集合形式IB和屬性設置爲適合比例。

滾動視圖尺寸爲590×650mm的(寬×高)

對象分層結構:

enter image description here

上負載初始視圖:*注意:即使想圖像被部分地顯示時,我不能滾動到右側並查看圖像的其餘部分。 enter image description here

後,我縮小圖像,我可以滾動即使只是部分所顯示的圖像 enter image description here

+0

您的圖片寬度爲620所以620 + 620 = 1240不是1200 所以請設置內容大小 self.scroller.contentSize = CGSizeMake(1350,950); – kb920

+0

你說的是正確的。但是,這無助於回答查詢。當我垂直滾動並且不完全顯示第二個圖像時,UIImage仍然會彈回。 – Vinayaka

+0

@Vinu,你有沒有檢查UIScrollView的框架?它在任何機會之外都是超級觀點嗎? – iDev

回答

0

你會告訴我們的代碼,用於創建和添加圖片?

也是一個安全和一般的方法是如下:

CGSize contentSize = CGSizeZero; 
for(UIView *view in self.scroller.subviews) { 
    contentSize.height = MAX(contentSize.height, view.frame.origin.y + view.frame.size.height); 
    contentSize.width = MAX(contentSize.width, view.frame.origin.x + view.frame.size.width); 
} 
CGFloat border = 50; 
contentSize.height += border; 
contentSize.width += border; 
self.scroller.contentSize = contentSize; 

它只是將發現在滾動視圖的任何視圖的最低和最合適的位置,並用它作爲contentSize。

+0

我添加了來自IB的圖像。 – Vinayaka

+0

你確定,imageViews是scrollView的子視圖?什麼是確切的imageView框架?如果您使用的是interfaceBuilder,那麼爲什麼您需要在代碼中設置所有其他屬性。你可以在那裏做。 IBOutlet「self.scroller」連接是否正確? – calimarkus

+0

contentSize是否可以從IB設置?我讀到只有這個必須在代碼中設置,因此我使用代碼來設置值。我做了一個滾輪的插座和正確連接,我相信它的工作正常,因爲我已經編寫了縮放代碼並且工作正常,所以插座也被正確分配和連接。圖像視圖,我已經在IB的滾動視圖中添加了,所以我認爲它是正確的。我們可以用其他方法檢查它嗎?/ – Vinayaka

相關問題