UIImageView移动和拖拽手势

项目中要求对UIImageView进行放大和拖动,所以记录一下。

@interface PutQuestionToViewController ()\

@property (nonatomic, strong) UIImageView *maxImageView;

/** 记录大图的原始尺寸 */
@property (nonatomic, assign) CGRect oldFrame;

/** 大图的最大放大尺寸 */
@property (nonatomic, assign) CGRect largeFrame;

@end

@implementation PutQuestionToViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 添加图片
        self.maxImageView.image = [UIImage imageNamed:@"picture"];
    
}

//MARK: 大图maxImageView
- (UIImageView *)maxImageView {
    if (!_maxImageView) {
        _maxImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, SCREENSIZE.width, SCREENSIZE.height - 64)];
        _oldFrame = _maxImageView.frame;// 记录原始尺寸
        _largeFrame = CGRectMake(0 - SCREENSIZE.width, 0 - SCREENSIZE.height, 3 * _oldFrame.size.width, 3 * _oldFrame.size.height);// 制定最大放大尺寸////
        
        // 允许imageView响应用户操作(切记要让ImageView响应用户操作)
        _maxImageView.userInteractionEnabled = YES;
        
        // 添加点击手势
        UITapGestureRecognizer *tap  = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(maxImageViewTap)];
        tap.numberOfTapsRequired = 1;// 点击次数
        tap.numberOfTouchesRequired = 1;// 几个手指
        [_maxImageView addGestureRecognizer:tap];
        
        // 添加缩放手势
        UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];
        [_maxImageView addGestureRecognizer:pinchGestureRecognizer];
        
        // 添加移动手势
        UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];
        [_maxImageView addGestureRecognizer:panGestureRecognizer];
        
    }
    [self.view addSubview:_maxImageView];
    return _maxImageView;
}

#pragma mark- maxImageView手势事件处理
//MARK: 点击手势
- (void)maxImageViewTap {
    // 让图片回复最初大小,并从父视图移除
    self.maxImageView.frame = self.oldFrame;
    [self.maxImageView removeFromSuperview];
}

//MARK: 缩放手势
- (void) pinchView:(UIPinchGestureRecognizer *)pinchGestureRecognizer {
    UIView *view = pinchGestureRecognizer.view;
    if (pinchGestureRecognizer.state == UIGestureRecognizerStateBegan || pinchGestureRecognizer.state == UIGestureRecognizerStateChanged) {
        view.transform = CGAffineTransformScale(view.transform, pinchGestureRecognizer.scale, pinchGestureRecognizer.scale);
        if (self.maxImageView.frame.size.width < self.oldFrame.size.width) { //让图片无法缩得比原图小

            self.maxImageView.frame = self.oldFrame;
        }
        if (self.maxImageView.frame.size.width > 3 * self.oldFrame.size.width) {// 设置最大放大图
            self.maxImageView.frame = self.largeFrame;
        }
        pinchGestureRecognizer.scale = 1;
    }
}
//MARK: 拖拉手势
- (void) panView:(UIPanGestureRecognizer *)panGestureRecognizer {
    UIView *view = panGestureRecognizer.view;
    if (panGestureRecognizer.state == UIGestureRecognizerStateBegan || panGestureRecognizer.state == UIGestureRecognizerStateChanged) {
        CGPoint translation = [panGestureRecognizer translationInView:view.superview];
        double X ;
        double Y;
       // 在此需要判断中心点有没有越界(即图片移动后保证不和屏幕边界产生空白处)
        if (view.center.x + translation.x < - (CGRectGetWidth(view.frame)/2 - SCREENSIZE.width)) {
            X = - (CGRectGetWidth(self.maxImageView.frame)/2 - SCREENSIZE.width);
        }else if(view.center.x + translation.x > CGRectGetWidth(view.frame)/2) {
            X = CGRectGetWidth(view.frame)/2;
        }else {
            X = view.center.x + translation.x;
        }
        if (view.center.y + translation.y < - (CGRectGetHeight(view.frame)/2 - SCREENSIZE.height)) {
            Y = - (CGRectGetHeight(view.frame)/2 - SCREENSIZE.height);
        }else if(view.center.y + translation.y - 64 > CGRectGetHeight(view.frame)/2) {
            Y = CGRectGetHeight(view.frame)/2 + 64;
        }else {
            Y = view.center.y + translation.y;
        }
        
        [view setCenter:(CGPoint){X, Y}];
    }
    [panGestureRecognizer setTranslation:CGPointZero inView:view.superview];
    
}


@end

参考:
【IOS】使用手势对UIImageView进行缩放、旋转和移动

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,406评论 4 61
  • 又是一年芙蓉花开时,又是一年毕业季。看着朋友圈里,曾经的学生们发着各种各样的照片,不由地感叹岁月的无情,把我们曾经...
    落叶听雨声阅读 1,513评论 0 2
  • 文/瑶淼shirley 古代有一个人要入城,带了一根长棍子。到了城门口他犯难了:因为无论他把棍子横着拿,还是竖着拿...
    瑶淼淼阅读 4,766评论 12 9
  • “天地间没有一双足以抬起我的手,天地间也没有一双足以踩倒我的脚。” 没有所谓的豪言壮志,没有所谓的...
    木子望星田阅读 2,649评论 4 1

友情链接更多精彩内容