//optional双指键
//创建滑动视图
UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
//导入代理协议UIScrollViewDelegate
scrollView.delegate=self;
//scrollView.contentOffset偏移量
//scrollView.contentInset内边距
//最小缩放系数
scrollView.minimumZoomScale= 0.5f;
//最大缩放系数
scrollView.maximumZoomScale= 2.0f;
[self.viewaddSubview:scrollView];
//创建图片视频添加到滑动视图上
_zoomView= [[UIImageView alloc] initWithFrame:CGRectMake(0,0,scrollView.frame.size.width,scrollView.frame.size.height)];
_zoomView.image= [UIImage imageNamed:@"7.jpg"];
[scrollView addSubview:_zoomView];
}
#pragma mark --- UIScrollViewDelegate
//返回一个缩放的视图这个视图是滑动视图的子视图
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return _zoomView;
}
//缩放一般用这个代码量少好实现
- (void)scrollViewDidZoom:(UIScrollView*)scrollView {
/*
//第一种方法:
//如果不进行判断放大之后会回缩如果视图不是整屏幕的大小需要设置设置图片的中心位置不然会跑动
if (scrollView.zoomScale < 1) {//缩小比例>1;
//内边距设置视图边缘跟设备边缘的距离设置上下左右四个边距
//UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>)
scrollView.contentInset = UIEdgeInsetsMake((scrollView.frame.size.height - scrollView.contentSize.height)*0.5, (scrollView.frame.size.width - scrollView.contentSize.width)*0.5, (scrollView.frame.size.height - scrollView.contentSize.height)*0.5, (scrollView.frame.size.width - scrollView.contentSize.width)*0.5);
}else {//放大
scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
}
*/
//第二种方式:通过计算偏移量
//contentSize滚动区域现视图的占的高宽大小
CGFloatoffsetX = scrollView.bounds.size.width> scrollView.contentSize.width? (scrollView.bounds.size.width- scrollView.contentSize.width) * 0.5 : 0.0;
CGFloatoffsetY = scrollView.bounds.size.height> scrollView.contentSize.height? (scrollView.bounds.size.height- scrollView.contentSize.height) * 0.5 : 0.0;
_zoomView.center=CGPointMake(scrollView.contentSize.width/2 + offsetX, scrollView.contentSize.height/2 + offsetY);
}
网友评论