Cell中添加图片放大与缩小,长按保存等功能

2018-01-12  本文已影响29人  锦箫_1

好久没有更新我自己的简书了,最近公司项目紧,连续奋斗了三个月,顺利完成。接下来开始我今天的分享。
今天分享的功能点有:
1.在cell中的图片点击放大(+放大动画)。
2.在图片放大后进行缩放功能。
3.通过长按手势进行保存到相册。
4.放大后进行左右上下的移动。
5.点击图片缩小返回cell中。
PS:以上功能全部在tableviewcell中完成。
其实在这里我主要用到了 scrollView、uIimageview、以及几种手势。

- (void)_inteTap{
    //自己cell中的图片_ContImage
    _ContImage.userInteractionEnabled = YES;
    //添加手势进行放大
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBigTwo)];
    [_ContImage addGestureRecognizer:tap];
}
- (void)tapBigTwo{
    //弹出来的视图
    [self creactSubViewOne];
    [UIView animateWithDuration:0.4 animations:^{
        CGFloat y,width,height;
        y = ([UIScreen mainScreen].bounds.size.height - _ImageView.image.size.height * [UIScreen mainScreen].bounds.size.width / _ImageView.image.size.width) * 0.5;
        //宽度为屏幕宽度
        width = [UIScreen mainScreen].bounds.size.width;
        //高度 根据图片宽高比设置
        height = _ImageView.image.size.height * [UIScreen mainScreen].bounds.size.width / _ImageView.image.size.width;
        if (height>ScreenHeight) {
            [_ImageView setFrame:CGRectMake(0, 0, width, height)];
        }else{
            [_ImageView setFrame:CGRectMake(0, y, width, height)];
        }
        //重要! 将视图显示出来
        _scrollView.backgroundColor = [UIColor blackColor];
        
        [_scrollView setAlpha:1];
        
    } completion:^(BOOL finished) {
        
    }];
    
}
- (void)creactSubViewOne{
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    _scrollView.showsHorizontalScrollIndicator = YES;
    _scrollView.showsVerticalScrollIndicator = YES;
    [self.window addSubview:_scrollView];
    _scrollView.scrollEnabled = YES;
    _scale = 1.0;

    //滑动+放大动画
    _ImageOne =_ContImage.image;
    CGSize size = _ImageOne.size;
    
    CGFloat height = size.height * [UIScreen mainScreen].bounds.size.width / size.width;
    _ImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth,height)];
    
    _scrollView.contentSize = CGSizeMake(ScreenWidth,height);
    _ImageView.image = _ContImage.image;
    _ImageView.userInteractionEnabled = YES;
    
    //创建捏合手势进行缩放
    UIPinchGestureRecognizer* pinchGR = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchActionOne:)];
    // pinchGR.delegate = self; // 可以在同一个视图上实现多个手势
    [_ImageView addGestureRecognizer:pinchGR];
    
    
    //添加手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(smallTapOne)];
    [_ImageView addGestureRecognizer:tap];
    [_scrollView addSubview:_ImageView];
    UILongPressGestureRecognizer *longP = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longP:)];
    [_ImageView addGestureRecognizer:longP];
    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
    [_ImageView addGestureRecognizer:panRecognizer];
    

}

//捏合手势的回调方法
- (void)pinchActionOne:(UIPinchGestureRecognizer*)sender{
    
    //放大情况。宏定义:最大缩放比例MaxSCale
    if(sender.scale > 1.0){
        if (_scale <MaxSCale ) {
            _scale = sender.scale+_scale-1;
            
            self.ImageView.transform = CGAffineTransformScale(self.ImageView.transform, sender.scale, sender.scale);
        }else{
            
        }
    }
    //缩小情况 宏定义:最小缩放比例MinScale
    if (sender.scale < 1.0) {
        if (_scale > MinScale ) {
            _scale = sender.scale-1+_scale;
            self.ImageView.transform = CGAffineTransformScale(self.ImageView.transform, sender.scale, sender.scale);
            
        }else{
            
        }
    }
    
    //修改视图比例
    // self.scale *=scale;
    
    CGSize size = _ImageView.frame.size;
    _scrollView.contentSize = CGSizeMake(0,size.height);
    
    //清除叠加的数据
    sender.scale = 1.0;
}

// 移动
-(void)move:(UIPanGestureRecognizer *)gestureRecognizer {
    
    CGPoint translation = [gestureRecognizer translationInView:[_ImageView superview]];
    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan ) {
        
        [_ImageView setCenter:CGPointMake([_ImageView center].x + translation.x, [_ImageView center].y + translation.y)];
        [gestureRecognizer setTranslation:CGPointZero inView:[_ImageView superview]];
    }else if (gestureRecognizer.state == UIGestureRecognizerStateChanged)
    {
        _ImageView.center = CGPointMake(_ImageView.center.x + translation.x, _ImageView.center.y);
        [gestureRecognizer setTranslation:CGPointMake(0,0) inView:self];
        if (_ImageView.center.x > _ImageView.frame.size.width ||
            _ImageView.center.x < -_ImageView.frame.size.width
            )
        {
            gestureRecognizer.enabled = NO;
            return;
        }
    }
    
    //
}

//回调cell缩小
- (void)smallTapOne{
    
    [UIView animateWithDuration:0.3 animations:^{
        
        _ImageView.frame = _ContImage.frame;
        
    } completion:^(BOOL finished) {
        self.hidden = NO;
        [_scrollView removeFromSuperview];
        
    }];
}


//长按手势
-(void)longP:(UILongPressGestureRecognizer *)sender
{
    if (sender.state == UIGestureRecognizerStateBegan) {
        
        UIAlertView *WXinstall=[[UIAlertView alloc]initWithTitle:@"提示" message:@"确定保存图片到本地" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        //        [[UIView appearance]setTintColor:BlackeColor];
        [WXinstall show];
    }
    
    
}
//监听点击事件 代理方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *btnTitle = [alertView buttonTitleAtIndex:buttonIndex];
    if ([btnTitle isEqualToString:@"取消"]) {
        NSLog(@"你点击了取消");
    }else if ([btnTitle isEqualToString:@"确定"] ) {
        UIImageWriteToSavedPhotosAlbum(_ImageView.image,self,@selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:),NULL); // 写入相册
        
        
    }//https在iTunes中找,这里的事件是前往手机端App store下载微信
}
// 完善回调
-(void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    if(!error){
        NSLog(@"保存成功");
        [self LabXian:@"保存成功"];
    }else{
        NSLog(@"保存失败");
    }
}
// 附赠:提示。  不占用主线程完成回显提示
-(void)LabXian:(NSString *)Xiaoxi
{
    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth/2-80, ScreenHeight/2, 160, 40)];
    label.backgroundColor = [UIColor blackColor];
    label.alpha = 0.5;
    label.font = [UIFont systemFontOfSize:14];
    label.textAlignment = NSTextAlignmentCenter;
    label.layer.cornerRadius = 10;//设置那个圆角的有多圆
    label.layer.masksToBounds = YES;
    label.text =Xiaoxi;
    label.textColor = WhiteNoneColor;
    [_scrollView addSubview:label];
    //设置动画
    CATransition * transion = [CATransition animation];
    transion.type = @"push";//设置动画方式
    transion.subtype = @"fromBotton";//设置动画从那个方向开始
    [label.layer addAnimation:transion forKey:nil];//给Label.layer 添加动画 //设置延时效果
    //不占用主线程
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(),^{
        [label removeFromSuperview];
    });
    
}
cell中添加,参数自己设定

复制以上代码到自己工程中,便可以解决图片简单处理问题。
PS:本人小白,如有更好方法请留言。互相学习、互相帮助。勿喷。谢谢

上一篇下一篇

猜你喜欢

热点阅读