//创建一个滚动视图对象
self.scrollView = [[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds];
[self addSubview:_scrollView];
_scrollView.backgroundColor = [UIColor cyanColor];
// NSLog(@"%@",NSStringFromCGRect(_scrollView.frame));
// 创建一个图片对象
//将该图片加在滚动视图上
UIImageView *imageView = [[UIImageView alloc ]initWithImage:[UIImage imageNamed:@"1.png"]];
[_scrollView addSubview:imageView];
imageView.tag = 100;
//设置内容视图大小(通常都是设置为图片大小)
_scrollView.contentSize = imageView.frame.size;
//_scrollView.contentSize = CGSizeMake(200, 100);
//设置偏移量(左正右负,上正下负)
//_scrollView.contentOffset = CGPointMake(100, 0);
//点击状态栏,是否回到顶部
_scrollView.scrollsToTop = YES;
//设置是否整页滑动
_scrollView.pagingEnabled = YES;
//是否开启边界回弹效果
//_scrollView.bounces = NO;
//设置滚动视图是否可以滚动
//_scrollView.scrollEnabled = NO;
//滚动设置被关闭后,可以用代码改变视图的位置
_scrollView.contentOffset=CGPointMake(300, 100);
//设置指示器的格式
_scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
//设置是否显示横向和纵向指示器
_scrollView.showsHorizontalScrollIndicator = YES;
_scrollView.showsVerticalScrollIndicator = YES;
//如果contentSize不大于本身的frame,设置下面两个属性,也可以进行滑动
_scrollView.alwaysBounceHorizontal = YES;
_scrollView.alwaysBounceVertical = YES;
//给滚动视图添加缩放比例
//注意:要想可以缩放视图,需要设置最小缩放比例和最大缩放比例
_scrollView.minimumZoomScale = 0.2;
_scrollView.maximumZoomScale = 2;
//设置插入条
// _scrollView.contentInset = UIEdgeInsetsMake(50,50, 20, 20);
//实现缩放的协议方法
//设置在滚动视图上要缩放的视图
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
//获得图片的对象:方法一:使用tag值
UIImageView *image = (UIImageView *)[self.rootView.scrollView viewWithTag:100];
return image;
}
//缩放结束时调用该方法
-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{
//由于缩放是即修改了缩放视图的大小,也修改了contentSize的大小,所以不能单独的使用center
//获取缩放视图的位置和大小
CGRect viewRect = view.frame;
//获取滚动视图的大小
CGSize scrollViewSize = scrollView.frame.size;
//如果内容视图的高度小于scrollView的高度,需要进行计算,否则,设置为0
if (view.frame.size.width < scrollViewSize.width) {
viewRect.origin.x = (scrollViewSize.width-view.frame.size.width)/2;
}
else{
viewRect.origin.x = 0;
}
if (view.frame.size.height<scrollViewSize.height) {
viewRect.origin.y = (scrollViewSize.height-view.frame.size.height)/2;
}
else{
viewRect.origin.y = 0;
}
//将变化的frame赋值给视图
view.frame = viewRect;
}
self.scrollView = [[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds];
[self addSubview:_scrollView];
_scrollView.backgroundColor = [UIColor cyanColor];
// NSLog(@"%@",NSStringFromCGRect(_scrollView.frame));
// 创建一个图片对象
//将该图片加在滚动视图上
UIImageView *imageView = [[UIImageView alloc ]initWithImage:[UIImage imageNamed:@"1.png"]];
[_scrollView addSubview:imageView];
imageView.tag = 100;
//设置内容视图大小(通常都是设置为图片大小)
_scrollView.contentSize = imageView.frame.size;
//_scrollView.contentSize = CGSizeMake(200, 100);
//设置偏移量(左正右负,上正下负)
//_scrollView.contentOffset = CGPointMake(100, 0);
//点击状态栏,是否回到顶部
_scrollView.scrollsToTop = YES;
//设置是否整页滑动
_scrollView.pagingEnabled = YES;
//是否开启边界回弹效果
//_scrollView.bounces = NO;
//设置滚动视图是否可以滚动
//_scrollView.scrollEnabled = NO;
//滚动设置被关闭后,可以用代码改变视图的位置
_scrollView.contentOffset=CGPointMake(300, 100);
//设置指示器的格式
_scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
//设置是否显示横向和纵向指示器
_scrollView.showsHorizontalScrollIndicator = YES;
_scrollView.showsVerticalScrollIndicator = YES;
//如果contentSize不大于本身的frame,设置下面两个属性,也可以进行滑动
_scrollView.alwaysBounceHorizontal = YES;
_scrollView.alwaysBounceVertical = YES;
//给滚动视图添加缩放比例
//注意:要想可以缩放视图,需要设置最小缩放比例和最大缩放比例
_scrollView.minimumZoomScale = 0.2;
_scrollView.maximumZoomScale = 2;
//设置插入条
// _scrollView.contentInset = UIEdgeInsetsMake(50,50, 20, 20);
//实现缩放的协议方法
//设置在滚动视图上要缩放的视图
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
//获得图片的对象:方法一:使用tag值
UIImageView *image = (UIImageView *)[self.rootView.scrollView viewWithTag:100];
return image;
}
//缩放结束时调用该方法
-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{
//由于缩放是即修改了缩放视图的大小,也修改了contentSize的大小,所以不能单独的使用center
//获取缩放视图的位置和大小
CGRect viewRect = view.frame;
//获取滚动视图的大小
CGSize scrollViewSize = scrollView.frame.size;
//如果内容视图的高度小于scrollView的高度,需要进行计算,否则,设置为0
if (view.frame.size.width < scrollViewSize.width) {
viewRect.origin.x = (scrollViewSize.width-view.frame.size.width)/2;
}
else{
viewRect.origin.x = 0;
}
if (view.frame.size.height<scrollViewSize.height) {
viewRect.origin.y = (scrollViewSize.height-view.frame.size.height)/2;
}
else{
viewRect.origin.y = 0;
}
//将变化的frame赋值给视图
view.frame = viewRect;
}