天天看點

解決iOS imageView 縮放和旋轉不靈敏問題

收到一個做挂件的小需求!使用三種手勢對UIImageView添加了,拖拽,旋轉,縮放三種手勢!結果發現用起來不是很爽,仔細分析了原因後,發現在旋轉的時候不能縮放,在縮放的時候又不能旋轉,嚴重影響使用者體驗!最後發現是手勢沖突硬氣的小問題!是以經過調研後發現系統提供了一個代理方法可以解決這個問題
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
複制代碼
           

這個方法傳回值是用來表示手勢是否共存的。隻要傳回YES,另外就不用管了,因為共存,是以共存,共同響應.也就是說兩個gesture recognizers的delegate方法隻要任意一個傳回YES,則這兩個就可以同時識别;隻有兩個都傳回NO的時候,才是互斥的。預設情況下是傳回NO。

#pragma mark - 貼圖demo

- (void)testPaster {
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.frame = CGRectMake(, , , );
    imageView.backgroundColor = [UIColor redColor];
    imageView.image = [UIImage imageNamed:@"paster"];
    [imageView setUserInteractionEnabled:YES];
    [imageView setMultipleTouchEnabled:YES];
    
    [self addGestureRecognizerToView:imageView];
    [self.view addSubview:imageView];
}

// 添加所有的手勢
- (void) addGestureRecognizerToView:(UIView *)view
{
    // 旋轉手勢
    UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateView:)];
    rotationGestureRecognizer.delegate = self;
    [view addGestureRecognizer:rotationGestureRecognizer];
    
    // 縮放手勢
    UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];
    pinchGestureRecognizer.delegate = self;
    [view addGestureRecognizer:pinchGestureRecognizer];
    
    // 移動手勢
    UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];
    panGestureRecognizer.delegate = self;
    [view addGestureRecognizer:panGestureRecognizer];
    
//    [pinchGestureRecognizer requireGestureRecognizerToFail:rotationGestureRecognizer];
}

// 處理旋轉手勢
- (void) rotateView:(UIRotationGestureRecognizer *)rotationGestureRecognizer
{
    UIView *view = rotationGestureRecognizer.view;
    if (rotationGestureRecognizer.state == UIGestureRecognizerStateBegan || rotationGestureRecognizer.state == UIGestureRecognizerStateChanged) {
        view.transform = CGAffineTransformRotate(view.transform, rotationGestureRecognizer.rotation);
        [rotationGestureRecognizer setRotation:];
    }
}

// 處理縮放手勢
- (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 = ;
    }
}

// 處理拖拉手勢
- (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];
    }
}


- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    //TODO: 解決手勢不靈敏問題
    return  YES;
}

複制代碼
           

繼續閱讀