//// ImageVC.h// MovieCrowdfunding//// Created by Sun-Mac on 2016/11/1.// Copyright © 2016年 All rights reserved.//#import@interface ImageVC : UIViewController
@property(strong,nonatomic)NSURL *imageURL;//创建vc对象之后要传imageURL
@end
//
// ImageVC.m
// MovieCrowdfunding
//
// Created by Sun-Mac on 2016/11/1.
// Copyright © 2016年 . All rights reserved.
//
#import "ImageVC.h"
@interface ImageVC ()
{
UIScrollView *scrollView;
UIImageView *imageView;
}
@end
@implementation ImageVC
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
scrollView=[[UIScrollView alloc]initWithFrame:self.view.bounds];
scrollView.maximumZoomScale=5.0;//图片的放大倍数
scrollView.minimumZoomScale=1.0;//图片的最小倍率
scrollView.contentSize=CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height);
scrollView.delegate=self;
imageView=[[UIImageView alloc]initWithFrame:self.view.bounds];
[imageView setImageWithURL:self.imageURL placeholderImage:[UIImage imageNamed:@"1"]];
[scrollView addSubview:imageView];
[self.view addSubview:scrollView];
imageView.userInteractionEnabled=YES;//注意:imageView默认是不可以交互,在这里设置为可以交互
UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImage:)];
tap.numberOfTapsRequired=1;//单击
tap.numberOfTouchesRequired=1;//单点触碰
[imageView addGestureRecognizer:tap];
UITapGestureRecognizer *doubleTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleTap:)];
doubleTap.numberOfTapsRequired=2;//避免单击与双击冲突
[tap requireGestureRecognizerToFail:doubleTap];
[imageView addGestureRecognizer:doubleTap];
imageView.contentMode=UIViewContentModeScaleAspectFit;
}
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView //委托方法,必须设置 delegate
{
return imageView;//要放大的视图
}
-(void)doubleTap:(id)sender
{
scrollView.zoomScale=2.0;//双击放大到两倍
}
- (IBAction)tapImage:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];//单击图像,dismissViewController
}
@end
网友评论