2012-03-23 58 views
0

我有多個圖像的滾動視圖。圖像可滾動。如何更改UIScrollView上的圖像

我使用下面的代碼:

scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 

scrollView.pagingEnabled = YES; 

scrollView.delegate = self; 

scrollView.showsVerticalScrollIndicator = NO; 

scrollView.showsHorizontalScrollIndicator = NO; 


NSInteger numberOfViews = [arrayImage count]; 

for (int i = 0; i < numberOfViews; i++) 
{ 
    CGFloat yOrigin = i * scrollView.frame.size.width; 
    imageView = [[UIImageView alloc]initWithFrame:CGRectMake(yOrigin +30, 0, self.view.frame.size.width-60 , self.view.frame.size.height)]; 
    imageView.image = [UIImage imageNamed:[arrayImage objectAtIndex:i]]; 
    [scrollView addSubview:imageView]; 
    [imageView release]; 
} 
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * numberOfViews,0); 
[self.view addSubview:scrollView]; 
[scrollView release] 

應用程序將與iPad方位工作,但圖像無法正常在風景模式下滾動。

它在縱向模式下工作正常,但圖像在橫向模式下合併。

如果有人有任何想法,請分享....

+0

我有一個包含圖像的底部滑塊,我想從UISlider中選擇屏幕上顯示相應的圖像。該圖像將顯示在最後一個UIImageView中,但我想在當前的UIImageView上顯示它。請幫助.. – 2012-03-23 11:38:32

+0

檢查autiscize選項的uiscrollview禁用它。 – hchouhan02 2012-03-23 11:38:51

+0

我發佈了一個答案...這對於在圖像視圖中滑動圖像和顯示圖像很有用... – Krunal 2012-03-23 11:41:45

回答

0

您必須設置圖像幀爲此用於每次界面方向改變或您必須在方向改變後再次重新繪製圖像

+0

感謝您的回覆。 – 2012-03-29 12:11:43

+0

接受答案,如果它解決了你的問題,以便它可以幫助他人解決同樣的問題 – 2012-04-03 05:51:31

1

試試這個...

scrollView.delegate = self; 
scrollView.scrollEnabled = YES; 

int scrollWidth = 120; 
scrollView.contentSize = CGSizeMake(scrollWidth,80);  


int xOffset = 0; 
imageView.image = [UIImage imageNamed:[imagesName objectAtIndex:0]]; 

for(int index=0; index < [imagesName count]; index++) 
{  
    UIImageView *img = [[UIImageView alloc] init]; 
    img.bounds = CGRectMake(10, 10, 50, 50); 
    img.frame = CGRectMake(5+xOffset, 0, 50, 50); 
    NSLog(@"image: %@",[imagesName objectAtIndex:index]); 
    img.image = [UIImage imageNamed:[imagesName objectAtIndex:index]]; 
    [images insertObject:img atIndex:index];   
    scrollView.contentSize = CGSizeMake(scrollWidth+xOffset,110); 
    [scrollView addSubview:[images objectAtIndex:index]]; 

    xOffset += 70; 
} 

設置此...

imagesName = [[NSArray alloc]initWithObjects:@"image1.jpg",@"image2.jpg",@"image3.jpg",@"image4.jpg",@"image5.jpg",@"image6.png",@"image7.png",@"image9.png",nil]; 
images = [[NSMutableArray alloc]init]; 
+0

根據我的要求,它不能正常工作。 – 2012-03-23 11:46:46

+0

我想你想要幻燈片中的圖像,並在滾動視圖中選擇正確的? – Krunal 2012-03-23 11:53:59

+0

是的,我想要的一樣。 – 2012-03-26 06:58:20