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