UIImageView移动和拖拽手势

作者: 西叶lv | 来源:发表于2017-05-17 14:48 被阅读21次

    项目中要求对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进行缩放、旋转和移动

    相关文章

      网友评论

        本文标题:UIImageView移动和拖拽手势

        本文链接:https://www.haomeiwen.com/subject/fsydxxtx.html