高仿QQ、微信用户图像处理
高仿QQ、微信用户图像处理
iOS赵师顺 发表于1年前
高仿QQ、微信用户图像处理
  • 发表于 1年前
  • 阅读 6
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 新注册用户 域名抢购1元起>>>   

摘要: 封装一个图片处理工具,类似QQ、微信用户图像处理,支持单击、双击、缩放手势,一句代码搞定。

// .h文件

 

#import <Foundation/Foundation.h>

#import <UIKit/UIKit.h>

 

@interface ImageDandleTool : NSObject

// 获取控件的坐标,传入用户图片

-(void)fullScreenWithUIView:(UIView *)view Image:(UIImage *)UserImage;

@end

 

// .m

#import "ImageDandleTool.h"

 

@interface ImageDandleTool ()<UIScrollViewDelegate>

 

@end

static CGRect oldFrame;

@implementation ImageDandleTool

 

-(void)fullScreenWithUIView:(UIView *)view Image:(UIImage *)UserImage{

    UIImage *image = UserImage;

    UIWindow *window = [UIApplication sharedApplication].keyWindow;

    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds];

    scrollView.maximumZoomScale = 1.5;

    scrollView.minimumZoomScale = 1.0;

    scrollView.bounces = NO;

    scrollView.delegate = self;

//    坐标系的转化,将控件的坐标转换成目标视图中的坐标;

    oldFrame = [view convertRect:view.bounds toView:window];

    scrollView.backgroundColor = [UIColor blackColor];

    scrollView.alpha = 0;

    UIImageView *imageView = [[UIImageView alloc]initWithFrame:oldFrame];

    imageView.image = image;

    imageView.tag = 1;

    [scrollView addSubview:imageView];

    [window addSubview:scrollView];

    //    添加点击手势;

    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(fullScreenTapGR:)];

    [scrollView addGestureRecognizer: tap];

    [UIView animateWithDuration:0.3 animations:^{

        imageView.frame=CGRectMake(0,([UIScreen mainScreen].bounds.size.height-image.size.height*[UIScreen mainScreen].bounds.size.width/image.size.width)/2,[UIScreen mainScreen].bounds.size.width, image.size.height*[UIScreen mainScreen].bounds.size.width/image.size.width);

        scrollView.alpha=1;

    }

                     completion:^(BOOL finished) {

                     }];

    //    添加双击的手势;

    UITapGestureRecognizer *tapTwo = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapTwoAction:)];

    tapTwo.numberOfTapsRequired = 2;

    [scrollView addGestureRecognizer:tapTwo];

    //    当双击手势和单击手势共存的时候,只有没有检测到双击手势的时候,单击才有效;

    [tap requireGestureRecognizerToFail:tapTwo];

}

//双击手势触发的方法;

-(void)tapTwoAction:(UITapGestureRecognizer *)sender{

    UIScrollView *scrollView = (UIScrollView *)sender.view;

    if (scrollView.zoomScale <= 1.0) {

        [UIView animateWithDuration:0.3 animations:^{

            scrollView.zoomScale = 1.5;

        }];

    }else{

        [UIView animateWithDuration:0.3 animations:^{

            scrollView.zoomScale = 1.0;

        }];

    }

}

// 点击退回原来的图片;

-(void)fullScreenTapGR:(UITapGestureRecognizer*)tap{

    UIScrollView *backgroundView = (UIScrollView *)tap.view;

    UIImageView *imageView=(UIImageView*)[tap.view viewWithTag:1];

    [UIView animateWithDuration:0.3 animations:^{

        imageView.frame=oldFrame;

        backgroundView.alpha=0;

    }

                     completion:^(BOOL finished) {

                         [backgroundView removeFromSuperview];

                     }];

}

//返回要缩放的控件;

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{

    return [scrollView viewWithTag:1];

}

//只要进行缩放,就会执行的该方法,使图片居中;

-(void)scrollViewDidZoom:(UIScrollView *)scrollView{

    CGPoint cender = scrollView.center;

    if (scrollView.zoomScale >1) {

        cender.x = [UIScreen mainScreen].bounds.size.width/2 +((scrollView.zoomScale - 1) * [UIScreen mainScreen].bounds.size.width/2);

        cender.y = [UIScreen mainScreen].bounds.size.height/2;

    }

    [scrollView viewWithTag:1].center = cender;

}

 

共有 人打赏支持
粉丝 4
博文 70
码字总数 33551
×
iOS赵师顺
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: