2010-08-12 194 views
0

我已經開始試圖瞭解蘋果的方式,通過照片看,從2009年WWDC和2010年約scrollViews和尋呼兩個視頻後實現對iPhone的photoviewer應用程序,我走的每一步非常緩慢地瞭解過程非常順利在我將其全部實施到我的應用程序之前。pagingScrollView幻燈片

我所遇到的那一刻幾個問題:

  1. 首先,我有它設置有尋呼滾動視圖刷卡左,右之間的照片有點之間的空間像在視頻中,但是當我在UIImageView中添加圖像時,圖像對於屏幕來說太大了,我嘗試添加UIViewContentModeScaleAspectFit;沒有效果。

  2. 當我通過for循環查看包含圖像的數組的每個元素時,UIView會重疊顯示一張照片,我想知道如何將它們分隔到下一部分分頁滾動視圖。


- (void)loadView{ 
    UIImage *img0 = [UIImage imageNamed:@"29.png"]; 
    UIImage *img1 = [UIImage imageNamed:@"33.png"];

NSMutableArray *imgArray = [[NSMutableArray alloc] initWithObjects:img0, img2, nil]; CGRect pagingScrollViewFrame = [[UIScreen mainScreen] bounds]; pagingScrollViewFrame.origin.x -= 10; pagingScrollViewFrame.size.width += 20; pagingScrollView = [[UIScrollView alloc] initWithFrame:pagingScrollViewFrame]; pagingScrollView.pagingEnabled = YES; pagingScrollView.backgroundColor = [UIColor blackColor]; pagingScrollView.contentSize = CGSizeMake(pagingScrollViewFrame.size.width * [imgArray count], pagingScrollViewFrame.size.height); self.view = pagingScrollView; for (int i=0; i < [imgArray count]; i++) { UIImageView *page = [[UIImageView alloc] initWithImage: [imgArray objectAtIndex:i]]; page.contentMode = UIViewContentModeScaleAspectFit; [pagingScrollView addSubview:page]; }}

正如我剛纔提到的我是相當新的編程爲iPhone和正在採取的東西慢慢地完全理解,最終這一計劃將模仿原生應用與捏和攻絲進行縮放。

回答

3

如果您使用的是UIImageViews,您應該確保您的視圖的clipsToBounds字段設置爲yes。嘗試添加:

UIImageView *page = [[UIImageView alloc] 
          initWithImage:[imgArray objectAtIndex:i]]; 
[page setContentMode:UIViewContentModeScaleAspectFit]; 
[page setClipsToBounds:YES]; 
[pagingScrollView addSubview:page]; 

然後,確保您將圖像的幀設置爲滾動視圖中正確的偏移量。幀的origin.x需要是幀的寬度乘以圖像索引。所以,你需要的東西是這樣的:

[page setFrame:CGRectMake(i*pagingScrollViewFrame.size.width, y, width, height)]; 

其中是你的指數從循環。

儘管在您參考的WWDC會話的示例代碼中,這是通過名爲-configurePage的方法完成的。你是否下載了示例代碼?

+0

是的,我有示例代碼,並試圖弄清楚什麼是從怎麼回事,但我決定,我會嘗試寫簡直是自己從他們在2010 WWDC顯示出與PhotoScroller的各個層的圖,所以尋呼層,縮放層,然後UIImage的視圖作爲子視圖,但隨後又拿下它的水平和剛剛獲得分頁工作第一然後從建立。從你的答案,我不能設置clipsToBounds工作話說頁面不是一個結構或聯合。 – SamRowley 2010-08-12 20:44:12

1

-[UIImageView initWithImage:]將幀設置爲圖像的大小。您需要使用page.frame = [[UIScreen mainScreen] bounds]或類似圖像將圖像縮放到正確的尺寸。

+0

謝謝我做了嘗試,但沒有與AspectFit一起和它的作品:d。只需要弄清楚如何在主scrollView中設置imageViews。 – SamRowley 2010-08-12 18:27:12