ios UIScrollView 创建多图像分页滚动

来源:互联网 发布:有约束条件的优化问题 编辑:程序博客网 时间:2024/06/05 16:25

滚动视图涉及的不仅仅是缩放 。通过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];
}

原创粉丝点击