先来看看实现效果(GIF):


实现思路:
直接自定义 UIView(CYPhotoPreviewer),为了实现双击缩放,可以实现 UIScrollViewDelegate 对应的方法。如果需要模糊背景,可以在自定义的 UIView 中先添加模糊背景,再添加 UIScrollView,继而在 UIScrollView 中添加图片容器,这个容器就是要显示的图片的 superView,代码一目了然:
- (void)setup {
self.frame = [UIScreenmainScreen].bounds;
self.backgroundColor = [UIColorclearColor];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(singleTap:)];
[self addGestureRecognizer:singleTap];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(doubleTap:)];
doubleTap.numberOfTapsRequired = 2;
[singleTaprequireGestureRecognizerToFail:doubleTap];
[self addGestureRecognizer:doubleTap];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizeralloc] initWithTarget:selfaction:@selector(longPress:)];
[self addGestureRecognizer:longPress];
// 设置模糊背景
self.blurBackground = [[UIVisualEffectViewalloc] initWithEffect:[UIBlurEffecteffectWithStyle:UIBlurEffectStyleExtraLight]];
self.blurBackground.frame = self.frame;
[self addSubview:self.blurBackground];
// 设置 UIScrollView 相关属性
self.scrollView = [[UIScrollViewalloc] initWithFrame:[UIScreenmainScreen].bounds];
self.scrollView.delegate = self;
self.scrollView.bouncesZoom = YES;
self.scrollView.maximumZoomScale = 3.0;
self.scrollView.multipleTouchEnabled = YES;
self.scrollView.alwaysBounceVertical = NO;
self.scrollView.showsVerticalScrollIndicator = NO;
self.scrollView.showsHorizontalScrollIndicator = NO;
[self addSubview:self.scrollView];
// containerView
self.containerView = [[UIViewalloc] init];
[self.scrollViewaddSubview:self.containerView];
// imageView
self.imageView = [[UIImageViewalloc] init];
self.imageView.clipsToBounds = YES;
self.imageView.backgroundColor = [UIColorcolorWithWhite:1.0 alpha:0.5];
[self.containerViewaddSubview:self.imageView];
}另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。