2011-03-07 19 views
1

好吧,我一直在努力這個功能一段時間了,這讓我瘋狂。我查看了網絡上的所有內容,似乎無法找到任何答案。我想要一個有兩個UIScrollviews的視圖,它們都包含一個UIImageView。所以我可以在我的視圖上顯示的圖像是獨立的scroll/zoomable。我想在同一個視圖上有兩個可縮放的UIImageViews,但我無法使它工作!

這似乎很簡單,但我不能得到它的工作。我在IB中設置了ScrollView/ImageView對,並將它們連接起來。現在我的viewDidLoad代碼看起來像這樣:

-(void) viewDidLoad 
{ 
// set the image to be displayed, pic your own image here 

imageView = [[MyImage alloc] initWithImage: [UIImage imageNamed: @"newBackground.png"]]; 

// yes we want to allow user interaction 

[imageView setUserInteractionEnabled:YES]; 

// set the instance of our myScrollView to use the main screen 

scrollView = [[MyScroll alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; 

// turn on scrolling 

[scrollView setScrollEnabled: YES]; 

// set the content size to the size of the image 

[scrollView setContentSize: imageView.image.size]; 

// add the instance of our myImageView class to the content view 

[scrollView addSubview: imageView]; 

// flush the item 

[imageView release]; 

// set max zoom to what suits you 

[scrollView setMaximumZoomScale:1.0f]; 

// set min zoom to what suits you 

[scrollView setMinimumZoomScale:0.25f]; 

// set the delegate 

[scrollView setDelegate: self]; 

// scroll a portion of image into view (my image is very big) :) 

//[scrollView scrollRectToVisible:CGRectMake(100, 100, 320, 440) animated:NO]; 

// yes to autoresize 

scrollView.autoresizesSubviews = YES; 

// set the mask 

scrollView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight); 

// set the view 

//self.view =scrollView; 

[scrollView setFrame:CGRectMake(0, 0, 320, 240)]; 

self.view =scrollView; 

imageView1 = [[MyImage alloc] initWithImage: [UIImage imageNamed: @"newBackground.png"]]; 

// yes we want to allow user interaction 

[imageView1 setUserInteractionEnabled:YES]; 

// set the instance of our myScrollView to use the main screen 

scrollView1 = [[MyScroll alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; 

// turn on scrolling 

[scrollView1 setScrollEnabled: YES]; 

// set the content size to the size of the image 

[scrollView1 setContentSize: imageView1.image.size]; 

// add the instance of our myImageView class to the content view 

[scrollView1 addSubview: imageView1]; 

// flush the item 

[imageView1 release]; 

// set max zoom to what suits you 

[scrollView1 setMaximumZoomScale:1.0f]; 

// set min zoom to what suits you 

[scrollView1 setMinimumZoomScale:0.25f]; 

// set the delegate 

[scrollView1 setDelegate: self]; 

// scroll a portion of image into view (my image is very big) :) 

//[scrollView scrollRectToVisible:CGRectMake(100, 100, 320, 440) animated:NO]; 

// yes to autoresize 

scrollView1.autoresizesSubviews = YES; 

// set the mask 

scrollView1.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight); 

// set the view 

//self.view =scrollView; 

[scrollView1 setFrame:CGRectMake(0, 300, 320, 220)]; 

//I do this only because I don't know what else to do. This effectively adds the scrollview to the scrollview... 
self.view =scrollView1; 

而第一個scrollView加載,但第二個嵌套在那個!我試着做[self.view addSubView:scrollView1];,但這只是讓應用程序崩潰。我盡我最大的努力去挖掘並盡力找出scrollViews,但我只是在這裏擁有。

請幫忙!

感謝

**爲了進一步澄清,我要的是有一個滾動視圖上的圖像的上半部分,可以平移和與它內部的圖像放大,然後又是滾動視圖從firt分離可以平移和縮放屏幕的下半部分,獨立於第**

回答

1

必須同時滾動視圖添加爲視圖控制器的視圖(self.view)的子視圖:

[self.view addSubview:scrollView]; 
[self.view addSubview:scrollView1]; 

然後,您需要手動定位這些視圖以保證它們的正確性e定位就像你想要的那樣(上面的代碼將兩個滾動視圖放在彼此的頂部,有一些任意的默認大小,可能會或可能不是你想要的)。

+0

非常感謝您的回答。我得到了兩個scrollViews在正確的地方顯示正確的內容!但是,現在,縮放存在問題。如果我捏在第一個scrollView的內部,第一個圖像放大正常。如果我捏住第二個scrollView,第一個圖像放大!而第二個scrollView變得更大......好像圖像的大小正在增加,但它不是......任何想法? –

相關問題