UIScrollView 滚动视图
1 #import "ViewController.h" 2 3 #import "GlobalDefine.h" 4 5 @interface ViewController () 6 7 @end 8 9 @implementation ViewController 10 11 - (void)viewDidLoad 12 { 13 [super viewDidLoad]; 14 //滚动视图 15 UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT)]; 16 //图片内容的宽高 17 scrollView.contentSize = CGSizeMake(5 * SCREENWIDTH, SCREENHEIGHT); 18 //启用分页 19 scrollView.pagingEnabled = YES; 20 //弹动 21 scrollView.bounces = NO; 22 23 [self.view addSubview:scrollView]; 24 25 //添加5张图片 26 for (int i = 0; i < 5; ++i) 27 { 28 UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * SCREENWIDTH, 0, SCREENWIDTH, SCREENHEIGHT)]; 29 imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png", i]]; 30 [scrollView addSubview:imageView]; 31 } 32 }
UIScrollViewdDelegate协议
1 - (void)viewDidLoad 2 { 3 [super viewDidLoad]; 4 5 //写相关代码 6 7 //遵循协议代理 8 scrollView.delegate = self; 9 } 10 11 12 //常用方法 13 - (void)scrollViewDidScroll:(UIScrollView *)scrollView 14 { 15 NSLog(@"滑动时执行"); 16 } 17 18 - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate 19 { 20 NSLog(@"结束拖拽时执行"); 21 } 22 23 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 24 { 25 NSLog(@"单个滑动结束时执行"); 26 //scrollView.contentOffset.x和scrollView.contentOffser.y计算内容偏移的坐标 27 }