开发者

UIView pass through certain events

I have a UIView on top of another UIView and I would like the top one to only respond to a开发者_运维百科 UITapGestureRecognizer on 1, 2 and 3 taps, but any other event would be passed though to the UIView below it. The view below has a UIPinchGestureRecognizer and a UIPanGestureRecognizer, but won't work if any of the touches are on the top UIView. It works right if I set the top UIView to userInteractionEnabled to NO, but then I of course can't get the taps from the top UIView. Any tips are greatly appreciated.


I tweaked around with the Touches sample. Not sure if this is the right way to go about it.

All this is done in the View Controller with both views (topView and bottomView).

in the viewDidLoad I add all gesture recognizers to the top view -

    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];

The gesture actions

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

I have not fully TESTED all the code. Try it out and see it works.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜