滚动视图
- 滚动视图的基本使用
实例: 浏览大尺寸图片
// 实例: 如何使用滚动实现显示大图片
UIImage *image = [UIImage imageNamed:@"1001.jpg"];
// 11000x5600
NSLog(@"%f %f",image.size.width,image.size.height);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 11000, 5600)];
imageView.image = image;
//[self.view addSubview:imageView];
//创建UIScrollView滚动视图
//注意: 滚动视图其实应该有两个大小, 本身大小, 内容大小
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
//再去设置内容的大小
scrollView.contentSize = CGSizeMake(11000, 5600);
[scrollView addSubview:imageView];
[self.view addSubview:scrollView];
//常用的设置
//是否显示水平方向滚动提示条
scrollView.showsHorizontalScrollIndicator = NO;
//是否显示竖直方向滚动提示条
scrollView.showsVerticalScrollIndicator = NO;
//去掉反弹效果
scrollView.bounces = NO;
2.滚动视图的基本原理 + 滚动视图的事件处理
3.滚动视图的缩放
4.项目常用: 新手引导界面(UIPageControl)
详细实战:
[self createScrollView];
[self createPageControl];
}
-(void)createPageControl
{
CGSize size = self.view.frame.size;
_pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
_pageControl.center = CGPointMake(size.width/2, size.height - 40);
_pageControl.numberOfPages = 4;
[self.view addSubview:_pageControl];
//注意事件类型
[_pageControl addTarget:self action:@selector(dealPage:) forControlEvents:UIControlEventValueChanged];
}
-(void)dealPage:(UIPageControl *)pc
{
//把序号转化为滚动视图x的偏移
double x = _scrollView.frame.size.width * pc.currentPage;
[_scrollView setContentOffset:CGPointMake(x, 0) animated:YES];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
int index = scrollView.contentOffset.x / scrollView.frame.size.width;
_pageControl.currentPage = index;
}
-(void)createScrollView
{
_scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
//分页滚动
_scrollView.pagingEnabled = YES;
_scrollView.bounces = NO;
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.delegate = self;
[self.view addSubview:_scrollView];
double w = _scrollView.frame.size.width;
double h = _scrollView.frame.size.height;
for (int i=0; i<4; i++) {
UIImageView *back = [[UIImageView alloc] initWithFrame:CGRectMake(w*i, 0, w, h)];
back.image = [UIImage imageNamed:@"guide_bg.png"];
[_scrollView addSubview:back];
NSString *name = [NSString stringWithFormat:@"guideView_%d.png",i+1];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(w*i, 0, w, h)];
imageView.image = [UIImage imageNamed:name];
[_scrollView addSubview:imageView];
}
//设置内容大小
_scrollView.contentSize = CGSizeMake(w * 4, h);
}