#09-手勢識別(拖動,旋轉,捏合)#

09-手勢識別(拖動,旋轉,捏合)

1.平移    
      UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self
                                                                action:@selector(pan:)];
       添加手勢
      [self.imageV addGestureRecognizer:pan];
      
      實現手勢方法
       手指在屏幕上移動進調用
        - (void)pan:(UIPanGestureRecognizer *)pan{
            獲取當前手指移動的偏移量
            CGPoint transP =  [pan translationInView:self.imageV];
            NSLog(@"%@",NSStringFromCGPoint(transP));
            Make它會清空上一次的形變.
            self.imageV.transform = CGAffineTransformMakeTranslation(transP.x, transP.y);
            
            self.imageV.transform = CGAffineTransformTranslate(self.imageV.transform, 
                                                                       transP.x, transP.y);
            復位,相對于上一次.
            [pan  setTranslation:CGPointZero inView:self.imageV];
        }

2.旋轉
      
    添加旋轉手勢
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] 
                                 initWithTarget:self action:@selector(rotation:)];
    設置代理,設置代理的目的就讓它能夠同時支持旋轉跟縮放
    rotation.delegate = self;
    添加手勢
    [self.imageV addGestureRecognizer:rotation];
    
    
    當旋轉時調用
    - (void)rotation:(UIRotationGestureRecognizer *)rotation{
        旋轉也是相對于上一次
        self.imageV.transform = CGAffineTransformRotate(self.imageV.transform, 
                                                             rotation.rotation);
        設置代理,設置代理的目的就讓它能夠同時支持旋轉跟縮放
        rotation.delegate = self;
        也要做復位操作
        rotation.rotation = 0;
    }

3.添加縮放手勢
    添加縮放手勢
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]                                             initWithTarget:self action:@selector(pinch:)];
    
   [self.imageV addGestureRecognizer:pinch];

    
    縮放手勢時調用
    -(void)pinch:(UIPinchGestureRecognizer *)pinch{
        平移也是相對于上一次
        self.imageV.transform = CGAffineTransformScale(self.imageV.transform, pinch.scale, 
                                                                              pinch.scale);
        復位
        pinch.scale = 1;
    }
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容