全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

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

先来看看实现效果(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  # 点击头像放大  # 图片缩放  # ios图片等比例缩放  # iOS应用开发中对UIImage进行截取和缩放的方法详解  # iOS应用开发中使用UIScrollView控件来实现图片缩放  # iOS UITableView展开缩放动画实例代码  # iOS开发中Quartz2D控制圆形缩放和实现刷帧效果  # iOS tableView实现头部拉伸并改变导航条渐变色  # iOS开发之tableView实现左滑删除功能  # iOS App中UITableView左滑出现删除按钮及其cell的重用  # 解决iOS11刷新tableview会出现漂移的现象  # IOS实现左右两个TableView联动效果  # iOS TableView头视图根据偏移量下拉缩放效果  # 双击  # 好了  # 可以看到  # 自定义  # 为了避免  # 单击  # 是为了  # 而在  # 要在  # 较好  # 很简单  # 弹出  # 就得  # 只需要  # 该如何  # 可以实现  # 要将  # 这篇文章  # 不需  # 谢谢大家 


相关文章: Android自定义控件实现温度旋转按钮效果  网站视频怎么制作,哪个网站可以免费收看好莱坞经典大片?  如何选择服务器才能高效搭建专属网站?  较简单的网站制作软件有哪些,手机版网页制作用什么软件?  ppt制作免费网站有哪些,ppt模板免费下载网站?  公司网站制作费用多少,为公司建立一个网站需要哪些费用?  如何快速配置高效服务器建站软件?  如何在云虚拟主机上快速搭建个人网站?  建站之星安装后界面空白如何解决?  已有域名如何快速搭建专属网站?  如何零基础开发自助建站系统?完整教程解析  专业公司网站制作公司,用什么语言做企业网站比较好?  网站制作新手教程,新手建设一个网站需要注意些什么?  济南专业网站制作公司,济南信息工程学校怎么样?  高端建站三要素:定制模板、企业官网与响应式设计优化  如何快速搭建支持数据库操作的智能建站平台?  建站上市公司网站建设方案与SEO优化服务定制指南  建站之星×万网:智能建站系统+自助建站平台一键生成  ,想在网上投简历,哪几个网站比较好?  如何快速搭建虚拟主机网站?新手必看指南  如何选择高效稳定的ISP建站解决方案?  如何制作算命网站,怎么注册算命网站?  如何在Golang中使用replace替换模块_指定本地或远程路径  详解ASP.NET 生成二维码实例(采用ThoughtWorks.QRCode和QrCode.Net两种方式)  网站制作难吗安全吗,做一个网站需要多久时间?  建站之星在线版空间:自助建站+智能模板一键生成方案  制作网站哪家好,cc、.co、.cm哪个域名更适合做网站?  武清网站制作公司,天津武清个人营业执照注销查询系统网站?  如何用狗爹虚拟主机快速搭建网站?  建站之星如何通过成品分离优化网站效率?  个人网站制作流程图片大全,个人网站如何注销?  如何在七牛云存储上搭建网站并设置自定义域名?  如何用虚拟主机快速搭建网站?详细步骤解析  建站之星2.7模板快速切换与批量管理功能操作指南  如何在阿里云虚拟服务器快速搭建网站?  建站主机如何选?高性价比方案全解析  如何在阿里云高效完成企业建站全流程?  西安大型网站制作公司,西安招聘网站最好的是哪个?  如何在Golang中引入测试模块_Golang测试包导入与使用实践  建站主机选择指南:服务器配置与SEO优化实战技巧  深圳网站制作公司好吗,在深圳找工作哪个网站最好啊?  建站ABC备案流程中有哪些关键注意事项?  唐山网站制作公司有哪些,唐山找工作哪个网站最靠谱?  香港服务器租用费用高吗?如何避免常见误区?  南平网站制作公司,2025年南平市事业单位报名时间?  Avalonia如何实现跨窗口通信 Avalonia窗口间数据传递  ,如何利用word制作宣传手册?  如何快速搭建高效香港服务器网站?  哈尔滨网站建设策划,哈尔滨电工证查询网站?  重庆市网站制作公司,重庆招聘网站哪个好? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。