1.图像点击之后,全屏浏览
- (void)viewDidLoad {
[super viewDidLoad];
_myImage=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, klScreenWidth, klScreenHeight-270)];
_myImage.userInteractionEnabled=YES;
[self.scrollview addSubview:_myImage];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showZoomImageView:)];
tapGesture.numberOfTapsRequired=1;
[_myImage addGestureRecognizer:tapGesture];
}
-(void)showZoomImageView:(UITapGestureRecognizer *)tap
{
if (![(UIImageView *)tap.view image]) {
return;
}
//scrollView作为背景
UIScrollView *bgView = [[UIScrollView alloc] init];
bgView.frame = [UIScreen mainScreen].bounds;
bgView.backgroundColor = [UIColor blackColor];
UITapGestureRecognizer *tapBg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBgView:)];
[bgView addGestureRecognizer:tapBg];
UIImageView *picView = (UIImageView *)tap.view;
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = picView.image;
imageView.frame = [bgView convertRect:picView.frame fromView:self.view];
[bgView addSubview:imageView];
[[[UIApplication sharedApplication] keyWindow] addSubview:bgView];
[UIView animateWithDuration:0.5 animations:^{
CGRect frame = imageView.frame;
frame.size.width = bgView.frame.size.width;
frame.size.height = frame.size.width * (imageView.image.size.height / imageView.image.size.width);
frame.origin.x = 0;
frame.origin.y = (bgView.frame.size.height - frame.size.height) * 0.5;
imageView.frame = frame;
}];
}
2.图片的缩放
@interface ViewController () <UIScrollViewDelegate>{
UIScrollView *_scrollview;
UIImageView *_imageview;
}
- (void)viewDidLoad {
[super viewDidLoad];
_scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
_scrollview.delegate=self;
//设置最大伸缩比例
_scrollview.maximumZoomScale=5.0;
//设置最小伸缩比例
_scrollview.minimumZoomScale=1;
[self.view addSubview:_scrollview];
UIImage *image=[UIImage imageNamed:@"1"];
_imageview=[[UIImageView alloc]initWithImage:image];
_imageview.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
_imageview.userInteractionEnabled = YES;
[_scrollview addSubview:_imageview];
}
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return _imageview;
}