导入一张图片
<UIScrollViewDelegate>
@property (nonatomic,strong)UIScrollView * scrollView;
@property (nonatomic,strong)UIImageView *imageView;
//===============
- (void)viewDidLoad {
[super viewDidLoad];
[self createView];
}
//===================
-(void)createView
{
_scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height - 64)];
[self.view addSubview:_scrollView];
_imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 64)];
_imageView.image = [UIImage imageNamed:@"05-022927_250.jpg"];
_imageView.contentMode = UIViewContentModeScaleAspectFit;
[_scrollView addSubview:_imageView];
//设置内容大小
_scrollView.contentSize = _imageView.frame.size;
//设置代理为控制器
_scrollView.delegate = self;
//设置最小缩放比例
_scrollView.minimumZoomScale = 1;
//设置最大缩放比例
_scrollView.maximumZoomScale = 2;
UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapGesture:)];
//设置手势点击数,双击:点2下
tapGesture.numberOfTapsRequired=2;
// self.imageView.userInteractionEnabled = YES;
[_scrollView addGestureRecognizer:tapGesture];
// [self.imageView addGestureRecognizer:tapGesture];
}
// Do any additional setup after loading the view, typically from a nib.
//放大缩小
-(void)handleTapGesture:(UIGestureRecognizer*)sender
{
if(_scrollView.zoomScale > 1.0){
[_scrollView setZoomScale:1.0 animated:YES];
}else{
[_scrollView setZoomScale:2.0 animated:YES];
}
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return _imageView;
}