iOS实现点击微信头像(放大、缩放、保存)效果

所属分类: 软件编程 / IOS 阅读数: 1058
收藏 0 赞 0 分享

先来看看实现效果(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];
}

可以看到,我们给设置了模糊背景,给这个 CYPhotoPreviewer 添加了单击手势(关闭 PhotoPreviewer)、双击手势(缩放图片)、长按手势(使用 UIAlertController 菜单,比如保存图片等)。

好,确定了这个 CYPhotoPreviewer 中的显示内容,那么我们该如何显示这个 CYPhotoPreviewer 呢?

  1. 直接将这个 CYPhotoPreviewer 添加到 keyWindow 上
  2. 将这个 CYPhotoPreviewer 添加到控制器的 self.view 上

这两种方式的实现都差不多,不过如果使用第一种方式的话,会导致将 CYPhotoPreviewer 添加到 keyWindow 上之后,再长按继续将 UIAlertController 显示就比较麻烦了,因此,这里打算采用将 CYPhotoPreviewer 添加到控制器的 self.view 上,继而就可以很方便的显示 UIAlertController 了:

- (void)previewFromImageView:(UIImageView *)fromImageViewinContainer:(UIView *)container {
 _fromImageView = fromImageView;
 fromImageView.hidden = YES;
 [containeraddSubview:self]; // 将 CYPhotoPreviewer 添加到 container 上
 
 self.containerView.origin = CGPointZero;
 self.containerView.width = self.width; // containerView 的宽度是屏幕的宽度
 
 UIImage *image = fromImageView.image;
 
 // 计算 containerView 的高度
 if (image.size.height / image.size.height > self.height / self.width) {
 self.containerView.height = floor(image.size.height / (image.size.width / self.width));
 } else {
 CGFloatheight = image.size.height / image.size.width * self.width;
 if (height self.height && self.containerView.height - self.height

可以看到,我们将外面的图片 fromImageView 传递进来,是为了显示更好的动画效果;将控制器的 container(self.view)传递进来,是为了将 CYPhotoPreviewer 添加到 container 的细节不需要在调用处处理,即初始化 CYPhotoPreviewer 之后,CYPhotoPreviewer 就直接被 container 添加为 subview 了。动画很简单不再细说。

显示的效果已经做好,单击关闭 CYPhotoPreviewer 也比较好实现,只需要从父类移除 CYPhotoPreviewer 即可:

- (void)dismiss {
 [UIViewanimateWithDuration:0.18 delay:0.0 options:UIViewAnimationOptionCurveEaseInOutanimations:^{
 CGRectfromRect = [self.fromImageViewconvertRect:self.fromImageView.boundstoView:self.containerView];
 self.imageView.contentMode = self.fromImageView.contentMode;
 self.imageView.frame = fromRect;
 self.blurBackground.alpha = 0.01;
 } completion:^(BOOL finished) {
 [UIViewanimateWithDuration:0.10 delay:0 options:UIViewAnimationOptionCurveEaseInOutanimations:^{
 self.fromImageView.hidden = NO;
 self.alpha = 0;
 } completion:^(BOOL finished) {
 [self removeFromSuperview];
 }];
 }];
}

好了,显示和关闭 CYPhotoPreviewer 都实现了,如果需要双击缩放图片效果,就得实现 UIScrollViewDelegate 的两个方法以及 CYPhotoPreviewer 的双击手势:

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
 return self.containerView;
}
 
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
 UIView *subView = self.containerView;
 
 CGFloatoffsetX = (scrollView.bounds.size.width > scrollView.contentSize.width)?
 (scrollView.bounds.size.width - scrollView.contentSize.width) * 0.5 : 0.0;
 
 CGFloatoffsetY = (scrollView.bounds.size.height > scrollView.contentSize.height)?
 (scrollView.bounds.size.height - scrollView.contentSize.height) * 0.5 : 0.0;
 
 subView.center = CGPointMake(scrollView.contentSize.width * 0.5 + offsetX,
  scrollView.contentSize.height * 0.5 + offsetY);
}
 
- (void)doubleTap:(UITapGestureRecognizer *)recognizer {
 if (self.scrollView.zoomScale > 1.0) {
 [self.scrollViewsetZoomScale:1.0 animated:YES];
 } else {
 CGPointtouchPoint = [recognizerlocationInView:self.imageView];
 CGFloatnewZoomScale = self.scrollView.maximumZoomScale;
 CGFloatxSize = self.width / newZoomScale;
 CGFloatySize = self.height / newZoomScale;
 [self.scrollViewzoomToRect:CGRectMake(touchPoint.x - xSize / 2, touchPoint.y - ySize / 2, xSize, ySize) animated:YES];
 }
}

最后一个就是长按弹出菜单(UIAlertController)了:

- (void)longPress:(UILongPressGestureRecognizer *)recognizer {
 
 // 为了避免弹警告:Warning: Attempt to present on which is already presenting ,最好加入状态判断
 if (recognizer.state == UIGestureRecognizerStateBegan) {
 UIAlertController *alertController = [UIAlertControlleralertControllerWithTitle:@"QuoraDots" message:nilpreferredStyle:UIAlertControllerStyleActionSheet];
 
 [alertControlleraddAction:[UIAlertActionactionWithTitle:@"保存" style:UIAlertActionStyleDefaulthandler:nil]];
 [alertControlleraddAction:[UIAlertActionactionWithTitle:@"取消" style:UIAlertActionStyleCancelhandler:nil]];
 
 UIViewController *vc = self.viewController;
 [vcpresentViewController:alertControlleranimated:YEScompletion:nil];
 }
}

注意一点longPress: 这个方法会调用很频繁,因此,为了避免 Attempt to present xxx on xxx which is already presenting xxx 这个警告,我们需要判断手势的状态。

源码下载

后话:

这个只是显示单张图片的大图,如果需要显示多张图片类似微信微博的九宫格图片的大图显示,则需要将这个 CYPhotoPreviewer 搞成 UICollectionView 的 item 即可,大家可以尝试尝试。

好了,以上就是这篇文章的全部内容了,希望本文的内容对各位iOS开发者们能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

更多精彩内容其他人还在看

详细整理iOS中UITableView的性能优化

最近在微博上看到一个很好的开源项目,是关于如何优化UITableView的,加上正好最近也在优化项目中的类似朋友圈功能这块,思考了很多关于UITableView的优化技巧,所以决定详细的整理下对优化UITableView的理解,需要的朋友们可以参考借鉴。
收藏 0 赞 0 分享

IOS开发中禁止NavigationController的向右滑动返回

这篇文章主要介绍了IOS开发中禁止NavigationController的向右滑动返回的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

iOS实现微信/QQ显示最近拍摄图片的功能实例代码

如果你刚刚拍摄了图片,在使用微信/QQ发生消息时会显示“你可能要发送的图片”,这个功能非常人性化,怎么实现的呢?下面小编给大家分享iOS实现微信/QQ显示最近拍摄图片的功能实例代码,一起看看吧
收藏 0 赞 0 分享

iOS实现动态自适应标签

这篇文章主要为大家详细介绍了iOS动态自适应标签的相关资料,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

iOS实现图片存在本地、再从本地获取图片的功能

本文主要介绍了iOS实现图片存在本地、再从本地获取图片的功能的代码。具有很好的参考价值。下面跟着小编一起来看下吧
收藏 0 赞 0 分享

iOS视频录制(或选择)压缩及上传功能(整理)

最新做的一个功能涉及到了视频的录制、压缩及上传功能,经过大神的一番教导,终于倒腾清楚了,今天小编把问题经过记录一下分享到脚本之家平台,供大家参考
收藏 0 赞 0 分享

iOS中打包上传常见的错误与解决办法

关于打包上传至AppStore,大家都认为是最后一步了,其实到了这里往往会遇到很多的坑。对于踩过的坑我不想再踩第二遍,所以在此将我遇到的所有奇葩问题在此做一个记录,当作对自己的一个提醒,同时也分享给给位,需要的朋友可以参考下。
收藏 0 赞 0 分享

解决Xcode 8构建版本iTunes Connect获取不到应用程序状态的办法

这篇文章主要介绍了关于解决Xcode 8构建版本iTunes Connect获取不到应用程序状态的办法,需要的朋友可以参考下
收藏 0 赞 0 分享

Objective-C实现身份证验证的方法示例

这篇文章主要给大家分享了Objective-C实现身份证验证的方法,文中给出了详细的示例代码,对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
收藏 0 赞 0 分享

ios启动页强制竖屏(进入App后允许横屏与竖屏)

最近工作遇到这样一个需要,当进入启动页需要强制竖屏,而进入APP后就允许横屏与竖屏,通过查找相关的资料找到了解决的方法,所以将实现的方法整理后分享出来,需要的朋友们可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享
查看更多