UIView 通过某些事件

标签 uiview uigesturerecognizer

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

最佳答案

我调整了 Touches 样本。不确定这是否是正确的方法。

所有这些都是在带有两个 View (topView 和 bottomView)的 View Controller 中完成的。

在 viewDidLoad 我将所有手势识别器添加到顶 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];

手势 Action
    - (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;
        }
    }

我还没有完全测试所有的代码。尝试一下,看看它是否有效。

关于UIView 通过某些事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4890043/

相关文章:

ios - UITapGestureRecognizer 未被调用

xcode - 在 SWIFT 中绘制圆弧进度

ios - UILongPressGestureRecognizer 识别其 View 外的触摸

iphone - UIGesture 识别器,区分单击和双击。

swift - TapGestureRecognizer 没有快速触发

swift - 淡入淡出动画级联 - 有更好的方法吗?

objective-c - 无法在iOS中实现简单的 View 滚动

iphone - 尝试将UIButton添加到UIView但得到NSInvalidArgumentException',原因:'-[UIView addSubView:]:无法识别

objective-c - 如何识别从左到右的滑动 Action ?

ios - Swift:通过其 super View 窗口中的平移手势更改(翻译)UIView 位置