原理:在ScrollView上面使用捏合手势时,ScrollView会给代理发送一条消息,询问要缩放的是哪个控件,即
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
代理方法,我们在这个代理方法里面返回要缩放的视图即可
@interface ViewController ()<UIScrollViewDelegate>//签代理
{
UIScrollView *scrolleView;
UIImageView *imgV;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//初始化滚动视图
scrolleView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
[self.view addSubview:scrolleView];
//初始化imageview,设置图片
imgV = [[UIImageView alloc]init];
imgV.image = [UIImage imageNamed:@"高圆圆"];
imgV.frame = CGRectMake(0, 0, imgV.image.size.width, imgV.image.size.height);
[scrolleView addSubview:imgV];
//设置代理,设置最大缩放和虽小缩放
scrolleView.delegate = self;
scrolleView.maximumZoomScale = 5;
scrolleView.minimumZoomScale = 1;
//设置UIScrollView的滚动范围和图片的真实尺寸一致
scrolleView.contentSize = imgV.image.size;
}
//代理方法,告诉ScrollView要缩放的是哪个视图
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return imgV;
}
效果:
原图片.png 放大.png 缩小.png
网友评论