//初始化一个scrollView 设置为全屏
UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
scrollView.backgroundColor = [UIColor whiteColor];
//通过width,height,来确定显示的内容的区域的大小。只要其大小超过scrollView自身的大小,就能产生滑动
CGFloat width = CGRectGetWidth(self.view.bounds);
CGFloat height = CGRectGetHeight(self.view.bounds);
scrollView.contentSize = CGSizeMake(8*width, height);
//使用循环来创建内容
for (int i = 0; i < 8; i ++) {
//第一步,让label铺满整个scrollView显示的区域
UILabel *aLabel = [[UILabel alloc]initWithFrame:CGRectMake(i * CGRectGetWidth(scrollView.frame), 0, scrollView.frame.size.width, CGRectGetHeight(scrollView.frame))];
//设置随机的背景色
aLabel.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1];
aLabel.text = [NSString stringWithFormat:@"第%d个Label" , i + 1];
aLabel.font = [UIFont boldSystemFontOfSize:100];
//字体自适应
aLabel.adjustsFontSizeToFitWidth = YES;
[scrollView addSubview:aLabel];
[aLabel release];
}
//通过设置pagingEnabled这个属性,可以控制scrollView的子视图按整屏翻动,默认为NO
scrollView.pagingEnabled = YES;
//设置代理
scrollView.delegate = self;
//通过修改contenOffset这个属性,让区域内容(子视图)按照偏移量的大小来进行显示。
scrollView.contentOffset = CGPointMake(CGRectGetWidth(scrollView.bounds)*0, 0);
//滑动视图的边界回弹效果,默认为YES.表示开启动画,设置为NO时,当滑动到边缘就是无效果
scrollView.bounces = YES;
//设置横向滑动的指示器是否显示,默认也是为显示
scrollView.showsHorizontalScrollIndicator = NO;
//设置纵向滑动的指示器的显示,默认也是为显示
scrollView.showsVerticalScrollIndicator = NO;
//滑动方向的锁定,默认为NO,不锁定的
scrollView.directionalLockEnabled = YES;
//默认是NO,当设置为YES时,可以运行content小于scrollView边界的回弹效果
scrollView.alwaysBounceHorizontal = YES;
scrollView.alwaysBounceVertical = YES;
//设置tag值 作用是如果和UIPageControl搭配使用 用Tag值来相互关联
scrollView.tag = 222;
//最后
[self.view addSubview:scrollView];
[scrollView release];
//效果