2011-02-03 51 views
4

我有一个UIView在另一个UIView之上,我希望顶部只响应1,2和3水龙头上的UITapGestureRecognizer,但任何其他事件将通过传递给下面的UIView它。下面的视图有一个UIPinchGestureRecognizer和一个UIPanGestureRecognizer,但如果任何一个触摸位于顶部的UIView上,它将不起作用。它的工作原理是,如果我将顶级UIView设置为userInteractionEnabled为NO,但我当然无法从顶级UIView获取水龙头。任何提示,非常感谢。UIView通过某些事件

回答

0

我使用Touches示例进行了调整。不知道这是否正确。

所有这些都在视图控制器中使用两个视图(topView和bottomView)完成。

在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; 
     } 
    } 

我还没有完全测试的所有代码。试试看,看看它的作品。