- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//1.滚动视图的使用
//读取图片内容,显示到图片视图上
NSString *path = [[NSBundle mainBundle] pathForResource:@"3" ofType:@"png"];
UIImage *image = [UIImage imageWithContentsOfFile:path];
//图片视图
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
imageView.image = image;
//滚动视图是在视图大小超过它的高度时使用
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(50, 80, 300, 300)];
//设置背景颜色
scrollView.backgroundColor = [UIColor redColor];
//将图片放到滚动视图上面
[scrollView addSubview:imageView];
//添加到父视图
[self.view addSubview:scrollView];
//设置滚动的范围
//滚动视图在横向和纵向显示的最大值
scrollView.contentSize = CGSizeMake(image.size.width, image.size.height);
//设置横向滚动条的显示
scrollView.showsHorizontalScrollIndicator = NO;
//设置纵向滚动条的显示
scrollView.showsVerticalScrollIndicator = NO;
//设置回弹效果
scrollView.bounces = NO;
//设置偏移量
scrollView.contentOffset = CGPointMake(0, 0);
//设置放到和缩小的倍数
//放到倍数
scrollView.maximumZoomScale = 2;
//设置缩小的倍数
scrollView.minimumZoomScale = 0.5;
//代理属性
scrollView.delegate = self;
//分页显示
scrollView.pagingEnabled = YES;
}
pragma mark - UIScrollView代理
//返回滚动视图中可以放大或缩小的视图对象
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return [scrollView.subviews lastObject];
}
//开始滑动时调用
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
NSLog(@"%s",__func__);
}
//将要结束滑动时调用
-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
NSLog(@"%s",__func__);
}
//已经结束滑动时调用
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
NSLog(@"%s",__func__);
}
//在滚动的时候调用
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
NSLog(@"%s",__func__);
}
//减速停止时调用这个方法
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSLog(@"%s",__func__);
}