UIView通过某些事件

发布于 2024-10-15 22:11:14 字数 292 浏览 5 评论 0原文

我有一个 UIView 位于另一个 UIView 之上,我希望顶部的 UIView 仅响应 1、2 和 3 次点击的 UITapGestureRecognizer,但任何其他事件都会传递到其下方的 UIView。下面的视图有一个 UIPinchGestureRecognizer 和一个 UIPanGestureRecognizer,但如果任何触摸位于顶部 UIView 上,则该视图将不起作用。如果我将顶部 UIView 设置为 userInteractionEnabled 为 NO,它就可以正常工作,但是我当然无法从顶部 UIView 获得点击。非常感谢任何提示。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷弦 2024-10-22 22:11:14

我对 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;
        }
    }

我尚未完全测试所有代码。尝试一下,看看它是否有效。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文