2011-02-03 13 views
4

私は別のUIViewの上にUIViewを持っていますが、1つ、2つ、3つのタップでUITapGestureRecognizerにのみ応答するようにしたいのですが、他のイベントは下のUIViewに渡されますそれ。下のビューにはUIPinchGestureRecognizerとUIPanGestureRecognizerがありますが、タッチのいずれかがトップUIViewにある場合は機能しません。トップUIViewをuserInteractionEnabledにNOに設定すると正しく動作しますが、もちろんトップUIViewからタップを取得できません。すべてのヒントを高く評価しています。UIViewが特定のイベントを通過する

答えて

0

私はTouchesサンプルで微調整しました。これが正しい方法であるかどうかは分かりません。

これは両方のビュー(topViewとbottomView)でView Controllerで行われます。私はトップビューにすべてのジェスチャー認識機能を追加するのviewDidLoadで

-

tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureCaught)]; 
    [tapGesture setDelegate:self]; 
    [topView addGestureRecognizer:tapGesture]; 
    [tapGesture release]; 

    panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panBottomView)]; 
    [panGestureRecognizer setMaximumNumberOfTouches:2]; 
    [panGestureRecognizer setDelegate:self]; 
    [topView addGestureRecognizer:panGestureRecognizer]; 
    [panGestureRecognizer release]; 


    pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scaleBottomView)]; 
    [pinchGesture setDelegate:self]; 
    [topView addGestureRecognizer:pinchGesture]; 
    [pinchGesture release]; 

ジェスチャーアクション

- (void)panBottomView 
    { 
     UIView *piece = bottomView; //Hardcoded because the pan gesture to be applied only on the bottom view. 

     [self adjustAnchorPointForGestureRecognizer:panGestureRecognizer]; 

     if ([panGestureRecognizer state] == UIGestureRecognizerStateBegan || [panGestureRecognizer state] == UIGestureRecognizerStateChanged) { 
      CGPoint translation = [panGestureRecognizer translationInView:[piece superview]]; 

      [piece setCenter:CGPointMake([piece center].x + translation.x, [piece center].y + translation.y)]; 
      [panGestureRecognizer setTranslation:CGPointZero inView:[piece superview]]; 
     } 
    } 


    // scale the piece by the current scale 
    // reset the gesture recognizer's rotation to 0 after applying so the next callback is a delta from the current scale 
    - (void)scaleBottomView { 


     [self adjustAnchorPointForGestureRecognizer:pinchGesture]; 

     if ([pinchGesture state] == UIGestureRecognizerStateBegan || [pinchGesture state] == UIGestureRecognizerStateChanged) { 
      bottomView.transform = CGAffineTransformScale([bottomView transform], [pinchGesture scale], [pinchGesture scale]); 
      [pinchGesture setScale:1]; 
     } 
    } 

    - (void) tapGestureCaught { 

//Do stuff for the topView 

    } 

    // scale and rotation transforms are applied relative to the layer's anchor point 
    // this method moves a gesture recognizer's view's anchor point between the user's fingers 
    - (void)adjustAnchorPointForGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer { 
     if (gestureRecognizer.state == UIGestureRecognizerStateBegan) { 

      /*You can put if conditions based on the class of gestureRecognizer for furthur customization. For me this method was not called for any gesture on the TOPVIEW. So i have sort of harcoded the following line*/ 
      UIView *piece = bottomView; 
      CGPoint locationInView = [gestureRecognizer locationInView:piece]; 
      CGPoint locationInSuperview = [gestureRecognizer locationInView:piece.superview]; 

      piece.layer.anchorPoint = CGPointMake(locationInView.x/piece.bounds.size.width, locationInView.y/piece.bounds.size.height); 
      piece.center = locationInSuperview; 
     } 
    } 

私は完全にすべてのコードをテストしていません。それを試して、それが動作するのを見てください。

関連する問題