ios開發--旋轉、移動、縮放手勢實例代碼


// 添加所有的手勢  
- (void) addGestureRecognizerToView:(UIView *)view  
{  
    // 旋轉手勢  
    UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateView:)];  
    [view addGestureRecognizer:rotationGestureRecognizer];  
      
    // 縮放手勢  
    UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];  
    [view addGestureRecognizer:pinchGestureRecognizer];  
      
    // 移動手勢  
    UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];  
    [view addGestureRecognizer:panGestureRecognizer];  
}  
  
// 處理旋轉手勢  
- (void) rotateView:(UIRotationGestureRecognizer *)rotationGestureRecognizer  
{  
    UIView *view = rotationGestureRecognizer.view;  
    if (rotationGestureRecognizer.state == UIGestureRecognizerStateBegan || rotationGestureRecognizer.state == UIGestureRecognizerStateChanged) {  
        view.transform = CGAffineTransformRotate(view.transform, rotationGestureRecognizer.rotation);  
        [rotationGestureRecognizer setRotation:0];  
    }  
}  
  
// 處理縮放手勢  
- (void) pinchView:(UIPinchGestureRecognizer *)pinchGestureRecognizer  
{  
    UIView *view = pinchGestureRecognizer.view;  
    if (pinchGestureRecognizer.state == UIGestureRecognizerStateBegan || pinchGestureRecognizer.state == UIGestureRecognizerStateChanged) {  
        view.transform = CGAffineTransformScale(view.transform, pinchGestureRecognizer.scale, pinchGestureRecognizer.scale);  
        pinchGestureRecognizer.scale = 1;  
    }  
}  
  
// 處理拖拉手勢  
- (void) panView:(UIPanGestureRecognizer *)panGestureRecognizer  
{  
    UIView *view = panGestureRecognizer.view;  
    if (panGestureRecognizer.state == UIGestureRecognizerStateBegan || panGestureRecognizer.state == UIGestureRecognizerStateChanged) {  
        CGPoint translation = [panGestureRecognizer translationInView:view.superview];  
        [view setCenter:(CGPoint){view.center.x + translation.x, view.center.y + translation.y}];  
        [panGestureRecognizer setTranslation:CGPointZero inView:view.superview];  
    }  
} 

別忘了

[self addGestureRecognizerToView:view];

如果處理的是圖片,別忘了

[imageView setUserInteractionEnabled:YES];
[imageView setMultipleTouchEnabled:YES];

轉自:

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容