淘先锋技术网

首页 1 2 3 4 5 6 7

UIScrollView最重要的功能之一是支持分页。实现分页之前要设置滚动试图的pagingEnabled属性为YES。
下面实现三张图片的分页显示功能:

-(void) viewDidLoad{
    [super viewDidLoad];

    UIImage *iphone = [UIImage imageNamed: @"iphone"];
    UIImage *ipad = [UIImage imageNamed: @"ipad"];
    UIImage *macbook = [UIImage imageNamed: @"macbook"];

    CGRect scrollViewRect = self.view.bounds;

    self.myScrollView = [[UIScrollView alloc] initWithFrame: scrollViewRect];
    self.myScrollView.pagingEnabled = YES;
    self.myScrollView.contentSize = CGSizeMake(scrollViewRect.size.width*f, scrollViewRect.size.height);
    [self.view addSubView: self.myScrollView];

    CGRect imageViewRect = self.view.bounds;

    UIIMageView *iphoneImageView = [self newImageViewWithImage: ipone withFrame: imageViewRect];
    [self.myScrollView addSubView: iphoneImageView];

    //设置图片视图的x增加原来的视图宽度
    imageViewRect.orgin.x += iphoneImageView.size.width;
    UIImageView *ipadImageView = [self newImageViewWithImage: ipad withFrame: imageViewRect];
    [self.myScrollView addSubView: ipadImageView];

    imageViewRect.orgin.x += ipadImageView.size.width;
    UIImageView *macbookImageView = [self newImageViewWithImage: macbook withFrame: imageViewRect];
    [self.myScrollView addSubView: macbookImageView];
}

-(UIImageView *) newImageViewWithImage: (UIImage * )paramImage withFrame:(CGRect) paramFrame{
    UIImageView *myView = [[UIImageView alloc] initWithFrame: frame];
    myView.contentMode = UIViewContentModeScaleAspectFit;
    myView.image = paramImage;
    return myView;
}