iOS开发点击查看图片详情并可缩放拖拽图片长按图片下载
来源:互联网 发布:php设置头部 编辑:程序博客网 时间:2024/05/16 11:26
由图片缩略图进入查看图片原图页面,并可对图片进行缩放(最小不小于原图,最大不大于三倍原图),长按可保存到本地相册或者查看原图。
1)缩略图页面:
2)点击查看原图页面
代码部分:
1、初始化图片背景跟所有的手势。
//创建一个黑色背景 //初始化一个用来当做背景的View UIView *bgView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds]; _backgroundView = bgView; [bgView setBackgroundColor:[UIColor blackColor]]; [self.view addSubview:bgView]; //创建显示图像的视图 //初始化要显示的图片内容的imageView self.imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 100 * PROPORTION, AL_DEVICE_WIDTH, AL_DEVICE_HEIGHT - 200 * PROPORTION)]; //要显示的图片,即要放大的图片 [self.imgView sd_setImageWithURL:[NSURL URLWithString:self.arr[indexPath.row]]]; //设置最小跟最大位置 oldFrame = self.imgView.frame; largeFrame = CGRectMake(0 - AL_DEVICE_WIDTH, 0 - AL_DEVICE_HEIGHT, 3 * oldFrame.size.width, 3 * oldFrame.size.height); [bgView addSubview:self.imgView]; self.imgView.userInteractionEnabled = YES; self.imgView.multipleTouchEnabled = YES; //添加点击手势(即点击图片后退出全屏) UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(closeView)]; [self.imgView addGestureRecognizer:tapGesture]; [bgView addGestureRecognizer:tapGesture]; [self shakeToShow:bgView];//放大过程中的动画 // create and configure the pinch gesture UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGestureDetected:)]; [pinchGestureRecognizer setDelegate:self]; [self.imgView addGestureRecognizer:pinchGestureRecognizer]; // creat and configure the pan gesture UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureDetected:)]; [panGestureRecognizer setDelegate:self]; [self.imgView addGestureRecognizer:panGestureRecognizer]; // 添加长安手势(如果需求是长按保存图片) UILongPressGestureRecognizer *gestur = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)]; [gestur setDelegate:self]; [self.imgView addGestureRecognizer:gestur];
2、动画方法及手势的实现
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ return YES;}// 长按手势- (void)longPressAction:(UIGestureRecognizer *)gestrue{ if (gestrue.state != UIGestureRecognizerStateBegan) { //这个if一定要加,因为长按会有好几种状态,按住command键,点击UIGestureRecognizerStateBegan就能看到所有状态的枚举了,因为如果不加这句的话,此方法可能会被执行多次 return;//什么操作都不做,直接跳出此方法 } UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"保存图片到相册",@"查看原图", nil]; [sheet showInView:self.view];}- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ if (buttonIndex == 0) { NSLog(@"选择保存"); UIImageWriteToSavedPhotosAlbum(self.imgView.image/*你要保存到本地相册的图片对象,当然此处更多的需求可能是长按保存,那你就写个长按收拾UILongPressGestureRecognizer手势,给手势加个触发方法不就行了嘛*/, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil); }else{ NSLog(@"选择查看原图"); [UIView animateWithDuration:0.2 animations:^{ self.imgView.frame = oldFrame; }]; }}- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{ NSString *message = @"保存失败"; if (!error) { message = @"成功保存到相册"; }else { message = [error description]; } NSLog(@"message is %@",message); [self toast:message];}// 缩放手势- (void)pinchGestureDetected:(UIPinchGestureRecognizer *)recognizer{ UIGestureRecognizerState state = [recognizer state]; if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged) { if (self.imgView.frame.size.width < oldFrame.size.width) { [UIView animateWithDuration:0.2 animations:^{ self.imgView.frame = oldFrame; //让图片无法缩得比原图小 }]; } if (self.imgView.frame.size.width > 3 * oldFrame.size.width) { [UIView animateWithDuration:0.2 animations:^{ self.imgView.frame = largeFrame; }]; } CGFloat scale = [recognizer scale]; [recognizer.view setTransform:CGAffineTransformScale(recognizer.view.transform, scale, scale)]; [recognizer setScale:1.0]; }}// 拖拽手势- (void)panGestureDetected:(UIPanGestureRecognizer *)recognizer{ UIGestureRecognizerState state = [recognizer state]; if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged) { CGPoint translation = [recognizer translationInView:recognizer.view]; [recognizer.view setTransform:CGAffineTransformTranslate(recognizer.view.transform, translation.x, translation.y)]; [recognizer setTranslation:CGPointZero inView:recognizer.view]; }}- (void)closeView{ [_backgroundView removeFromSuperview]; [self.navigationController setNavigationBarHidden:NO];}//放大过程中出现的缓慢动画- (void) shakeToShow:(UIView*)aView{ CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"]; animation.duration = 0.1; NSMutableArray *values = [NSMutableArray array]; [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]]; [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]]; animation.values = values; [aView.layer addAnimation:animation forKey:nil];}
希望文章能对大家有所小帮助,谢谢大家的浏览。
0 0
- iOS开发点击查看图片详情并可缩放拖拽图片长按图片下载
- iOS 开发商品详情页中的banner中点击查看图片
- extjs4 图片查看可缩放
- Android 点击图片放大展示 展示中可调节图片的缩放显示 图片查看器
- iOS开发 上拉查看图片详情实现思路
- 图片手势缩放和图片下载
- android点击查看大图(长按保存图片)
- 点击查看大图(长按保存图片)
- 所有图片自动缩放并点击放大
- 融云开发文档之会话界面--点击图片消息查看详情
- Android图片查看器(图片可移动、缩放、旋转)
- 查看图片列表,选中之后查看大图,可缩放滑动
- IOS 点击查看预览大图缩放图片往下掉的处理
- iOS开发截屏并保存图片到本地相册以及长按保存图片到本地
- Android 网络加载图片点击大图后 浏览 可 缩放
- IOS图片拖拽.旋转.缩放
- IOS自定义图片浏览器,支持浏览多张图片,点击,双击,两手指缩放操作,并显示加载进度条
- 源码推荐(7.29):iOS图片查看器(多图全屏查看可缩放),给UITextView 添加超链接
- java实现二维码-使用jquery-qrcode方式生成二维码
- 2016.11.11 js对象(Math String )
- 一些图论算法
- Laravel框架中上传图片
- 使用Gson实现Json和Java对象的转换
- iOS开发点击查看图片详情并可缩放拖拽图片长按图片下载
- 数据结构之基础概念
- 国内gem源
- vector的sort操作
- 新手要想学好Linux系统就必须做好这四件事情
- bzoj 2282: [Sdoi2011]消防(树的直径+二分)
- Android 架构设计
- Tomcat 架构探索
- java实现二维码-生成带log的二维码