iOS图片添加平移/缩放/旋转多个手势

//
//  UIImageView+Utils.h
//  OpenWorkr
//
//  Created by 冰凉的枷锁 on 2017/3/6.
//  Copyright © 2017年 Eden. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface UIImageView (Utils) <UIGestureRecognizerDelegate>

- (void)showBigImageInWindow;

@end
//
//  UIImageView+Utils.m
//  OpenWorkr
//
//  Created by 冰凉的枷锁 on 2017/3/6.
//  Copyright © 2017年 Eden. All rights reserved.
//

#import "UIImageView+Utils.h"

@implementation UIImageView (Utils)

static CGRect oldframe;

- (void)showBigImageInWindow {

    UIImageView *currentImageview = self;
    UIImage *image = currentImageview.image;
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    backgroundView.backgroundColor = [UIColor colorWithWhite:0.7 alpha:1];
    //当前imageview的原始尺寸->将像素currentImageview.bounds由currentImageview.bounds所在视图转换到目标视图window中,返回在目标视图window中的像素值
    oldframe = [currentImageview convertRect:currentImageview.bounds toView:window];
//    [backgroundView setBackgroundColor:[UIColor colorWithRed:107 green:107 blue:99 alpha:0.6]];

    //此时视图不会显示
    [backgroundView setAlpha:0];
    //将所展示的imageView重新绘制在Window中
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:oldframe];
    [imageView setUserInteractionEnabled:YES];
    [imageView setImage:image];
    [imageView setTag:10];
    [backgroundView addSubview:imageView];
    [window addSubview:backgroundView];

    //向imageView添加手势 缩放图片
    UIPanGestureRecognizer *pan =[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
    //添加到指定视图
    [imageView addGestureRecognizer:pan];
    UIPinchGestureRecognizer *pinch =[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];
    [imageView addGestureRecognizer:pinch];
    UIRotationGestureRecognizer *rotation =[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
    [imageView addGestureRecognizer:rotation];
    pinch.delegate = self;
    rotation.delegate = self;

    //添加点击事件同样是类方法 -> 作用是再次点击回到初始大小
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideImageView:)];
    [backgroundView addGestureRecognizer:tapGestureRecognizer];

    //动画放大所展示的ImageView
    [UIView animateWithDuration:0.4 animations:^{
        CGFloat y,width,height;
        y = ([UIScreen mainScreen].bounds.size.height - image.size.height * [UIScreen mainScreen].bounds.size.width / image.size.width) * 0.5;
        width = [UIScreen mainScreen].bounds.size.width;
        //高度 根据图片宽高比设置
        height = image.size.height * [UIScreen mainScreen].bounds.size.width / image.size.width;
        [imageView setFrame:CGRectMake(0, y, width, height)];
        //重要! 将视图显示出来
        [backgroundView setAlpha:1];
    } completion:^(BOOL finished) {

    }];

}

/**
 *  恢复imageView原始尺寸
 *
 *  @param tap 点击事件
 */
- (void)hideImageView:(UITapGestureRecognizer *)tap{
    UIView *backgroundView = tap.view;
    //原始imageview
    UIImageView *imageView = [tap.view viewWithTag:10];
    //恢复
    [UIView animateWithDuration:0.4 animations:^{
        [imageView setFrame:oldframe];
        [backgroundView setAlpha:0];
    } completion:^(BOOL finished) {
        //完成后操作->将背景视图删掉
        [backgroundView removeFromSuperview];
    }];
}

//创建平移事件
-(void)panAction:(UIPanGestureRecognizer *)pan
{
    UIImageView *imageView = (UIImageView *)pan.view;
    if (!imageView) {
        return ;
    }
    //获取手势的位置
    CGPoint position =[pan translationInView:imageView];

    //通过stransform 进行平移交换
    imageView.transform = CGAffineTransformTranslate(imageView.transform, position.x, position.y);
    //将增量置为零
    [pan setTranslation:CGPointZero inView:imageView];
}

//添加捏合事件
-(void)pinchAction:(UIPinchGestureRecognizer *)pinch
{
    UIImageView *imageView = (UIImageView *)pinch.view;
    if (!imageView) {
        return ;
    }
    //通过 transform(改变) 进行视图的视图的捏合
    imageView.transform = CGAffineTransformScale(imageView.transform, pinch.scale, pinch.scale);
    //设置比例 为 1
    pinch.scale = 1;
}

//旋转事件
-(void)rotationAction:(UIRotationGestureRecognizer *)rote
{
    UIImageView *imageView = (UIImageView *)rote.view;
    if (!imageView) {
        return ;
    }
    //通过transform 进行旋转变换
    imageView.transform = CGAffineTransformRotate(imageView.transform, rote.rotation);
    //将旋转角度 置为 0
    rote.rotation = 0;
}

#pragma gesture delegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

@end
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值