服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - iOS实现点击微信头像(放大、缩放、保存)效果

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

2021-03-09 15:48iOS开发网 IOS

最近公司产品需要实现点击个人主页头像可以放大头像、缩放头像、保存头像效果(和点击微信个人头像类似),故找个时间实现一下,记录下来,供自己查看也给有需要的大家做个参考。下面来一起看看吧。

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

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

实现思路:

直接自定义 uiview(cyphotopreviewer),为了实现双击缩放,可以实现 uiscrollviewdelegate 对应的方法。如果需要模糊背景,可以在自定义的 uiview 中先添加模糊背景,再添加 uiscrollview,继而在 uiscrollview 中添加图片容器,这个容器就是要显示的图片的 superview,代码一目了然:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
- (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 了:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- (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 即可:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
- (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 的双击手势:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
- (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)了:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
- (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关于iOS自适应cell行高的那些事儿

    关于iOS自适应cell行高的那些事儿

    这篇文章主要给大家介绍了关于iOS自适应cell行高的那些事儿,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的...

    daisy6092021-05-17
  • IOSIOS 屏幕适配方案实现缩放window的示例代码

    IOS 屏幕适配方案实现缩放window的示例代码

    这篇文章主要介绍了IOS 屏幕适配方案实现缩放window的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...

    xiari5772021-06-01
  • IOSIOS开发之字典转字符串的实例详解

    IOS开发之字典转字符串的实例详解

    这篇文章主要介绍了IOS开发之字典转字符串的实例详解的相关资料,希望通过本文能帮助到大家,让大家掌握这样的方法,需要的朋友可以参考下...

    苦练内功5832021-04-01
  • IOSiOS布局渲染之UIView方法的调用时机详解

    iOS布局渲染之UIView方法的调用时机详解

    在你刚开始开发 iOS 应用时,最难避免或者是调试的就是和布局相关的问题,下面这篇文章主要给大家介绍了关于iOS布局渲染之UIView方法调用时机的相关资料...

    windtersharp7642021-05-04
  • IOSiOS中tableview 两级cell的展开与收回的示例代码

    iOS中tableview 两级cell的展开与收回的示例代码

    本篇文章主要介绍了iOS中tableview 两级cell的展开与收回的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    J_Kang3862021-04-22
  • IOSiOS通过逆向理解Block的内存模型

    iOS通过逆向理解Block的内存模型

    自从对 iOS 的逆向初窥门径后,我也经常通过它来分析一些比较大的应用,参考一下这些应用中某些功能的实现。这个探索的过程乐趣多多,不仅能满足自...

    Swiftyper12832021-03-03
  • IOSiOS 雷达效果实例详解

    iOS 雷达效果实例详解

    这篇文章主要介绍了iOS 雷达效果实例详解的相关资料,需要的朋友可以参考下...

    SimpleWorld11022021-01-28
  • IOS解析iOS开发中的FirstResponder第一响应对象

    解析iOS开发中的FirstResponder第一响应对象

    这篇文章主要介绍了解析iOS开发中的FirstResponder第一响应对象,包括View的FirstResponder的释放问题,需要的朋友可以参考下...

    一片枫叶4662020-12-25