ios UIScrollView 創建多圖像分頁滾動

滾動視圖涉及的不僅僅是縮放 。通過UIScrollView 地分頁屬性,我們可將圖像放在滾動視圖中,並對它們一次移動一個視圖地寬度,

關鍵是要確保加載地每幅圖像水平方向上與滾動視圖框架的寬度精確匹配,而在垂直方向上與其高度精確匹配。

將pagngEnabled 屬性設爲YES ,這樣就可以了。


下面是代碼:

#define COOKBOOK_PURPLE_COLOR    [UIColor colorWithRed:0.20392f green:0.19607f blue:0.61176f alpha:1.0f]

#define BASEHEIGHT    284.0f
#define NPAGES        3

- (void) viewDidLoad
{
    self.navigationController.navigationBar.tintColor = COOKBOOK_PURPLE_COLOR;
    self.title = @"Image Scroller";
    
    // Create the scroll view and set its content size and delegate
    UIScrollView *sv = [[[UIScrollView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, BASEHEIGHT)] autorelease];
    sv.contentSize = CGSizeMake(NPAGES * 320.0f, sv.frame.size.height);
    sv.pagingEnabled = YES;
    sv.delegate = self;
   
    // Load in all the pages
    int i=0;
    for(i; i < NPAGES; i++)
    {
        NSString *filename = [NSString stringWithFormat:@"image%d.png", i+1];
        UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:filename]];
        iv.frame = CGRectMake(i * 320.0f, 0.0f, 320.0f, BASEHEIGHT);
        [sv addSubview:iv];
        [iv release];
    }
    
    [self.view addSubview:sv];
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章