UIScrollview
import “MainViewController.h”
define WIDTH self.view.frame.size.width
define HEIGHT self.view.frame.size.height
@interface MainViewController ()
@end
@implementation MainViewController
(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor=[UIColor whiteColor];UIScrollView *scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scrollView.backgroundColor=[UIColor yellowColor];
[self.view addSubview:scrollView];
[scrollView release];// 重要的属性,这个属性可以让scrollView滚动起来
// contentSize设置scrollView的滚动范围
scrollView.contentSize=CGSizeMake(WIDTH*7,HEIGHT);
// 按页来进行滚动
scrollView.pagingEnabled=YES;for (NSInteger i=1; i<8; i++) {
NSString *picName=[NSString stringWithFormat:@”h%ld.jpg”,i];
// 通过图片名来创建UIImageView
UIImageView *imageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:picName]];
imageView.frame=CGRectMake(WIDTH*(i-1), 0, WIDTH, HEIGHT);
// 把imageView放到scrollView上
[scrollView addSubview:imageView];
// 释放imageView
[imageView release];
}
// 偏移量
scrollView.contentOffset=CGPointMake(WIDTH*2,0);// 关掉边界回弹的效果,默认是YES
scrollView.bounces=NO;// 水平和垂直的滚动条会作为两个字视图添加到scrollView的子视图里,如果把滚动调效果关闭,这两个视图就不会添加到scrollView的子视图里
NSLog(@”%ld”,scrollView.subviews.count);// 关闭滚动条
scrollView.showsHorizontalScrollIndicator=NO; // 水平滚动条
scrollView.showsVerticalScrollIndicator=NO; // 垂直滚动条// 设置代理人
scrollView.delegate=self;
// 设置tag值
scrollView.tag=1000;
}
pragma mark 只要滚动就能触发的协议方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
NSLog(@”翻滚吧,牛宝宝”);
}
pragma mark 当scrollView减速停止的时候会触发的协议方法
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
NSLog(@”停止停止”);
}