在手势中屏蔽响应事件继续向父视图传递

在手势中屏蔽响应事件继续向父视图传递

没有屏蔽时候的现象:

屏蔽时候的现象:

源码如下(用到了上一篇教程的源码):

//
//  ViewController.m
//  BackgroundView
//
//  Created by YouXianMing on 14-10-3.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import "ViewController.h"
#import "UIView+BackgroundView.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 添加手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                          action:@selector(handleSingleTap:)];
    [self.view addGestureRecognizer:tap];

    UILabel *label      = [[UILabel alloc] initWithFrame:self.view.bounds];
    label.text          = @"Y.X. Touch Test";
    label.textAlignment = NSTextAlignmentCenter;
    label.font          = [UIFont fontWithName:@"HelveticaNeue-Thin" size:40.f];
    label.textColor     = [UIColor redColor];
    [self.view addSubview:label];
}

- (void)tapEvent:(UITapGestureRecognizer *)tap
{
    // 显示
    [self.view showBackgroundViewAndConfig:^(BackgroundView *configView) {
        configView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
        configView.startDuration = 0.4f;
        configView.endDuration   = 0.4f;
    }];

    // 延迟3s执行
    [self performSelector:@selector(affterDelay)
               withObject:nil
               afterDelay:3.f];
}

- (void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer
{
    UIEvent *event = [[UIEvent alloc] init];
    CGPoint location = [gestureRecognizer locationInView:gestureRecognizer.view];

    //check actually view you hit via hitTest
    UIView *view = [gestureRecognizer.view hitTest:location withEvent:event];

    if ([view.gestureRecognizers containsObject:gestureRecognizer]) {
        NSLog(@"当前视图响应了事件");

        // 显示
        [self.view showBackgroundViewAndConfig:^(BackgroundView *configView) {
            configView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
            configView.startDuration = 0.4f;
            configView.endDuration   = 0.4f;
        }];

        // 延迟3s执行
        [self performSelector:@selector(affterDelay)
                   withObject:nil
                   afterDelay:3.f];
    }
    else {
        NSLog(@"子视图响应了事件");
    }
}

- (void)affterDelay
{
    // 隐藏
    [self.view removeBackgroundView];
}

@end

以下是核心代码处:

解决问题的出处:

http://stackoverflow.com/questions/5222998/uigesturerecognizer-blocks-subview-for-handling-touch-events/11573711#11573711

时间: 2024-08-02 02:23:12

在手势中屏蔽响应事件继续向父视图传递的相关文章

屏蔽响应事件继续向父视图传递的category

屏蔽响应事件继续向父视图传递的category 这篇教程是上一篇教程的升级版,将复杂的代码封装成了category,更便于使用:) 效果: 源码: UIGestureRecognizer+EnvetInCurrentView.h 与 UIGestureRecognizer+EnvetInCurrentView.m // // UIGestureRecognizer+EnvetInCurrentView.h // BackgroundView // // Created by YouXianMin

c# 中在响应事件中跳出事件

问题描述 比如一个下拉框的点击下拉框的事件,里面有一个判断if(){跳出这个事件,同时重点是,这个点击下拉框的事件不发生,意味着回复到做这个事件动作之前}else{正常程序...}请问个位大神该怎样解决 解决方案 解决方案二:不用下拉框的点击事件,用valuechange或selectitemchange试试解决方案三:如果你只是希望下拉框不能被点击,设置Enable属性不就可以了?

java中怎么实现有一个button去结束另一个button的响应事件

问题描述 java中怎么实现有一个button去结束另一个button的响应事件 小弟在做一个界面,刚开始学java,怎么实现有一个button去结束另一个button的响应事件,求各位大神指点一下,越详细越好!!! 解决方案 定义一个成员变量: boolean isstop=false; 在button1里面写 for (...) { 你的代码 if (isstop) { isstop = false; return; } } 在另一个按钮(停止)里写 isstop=true; 解决方案二:

由UIImageView中的UIButton不响应事件引发的

今天写了这么一小段测试代码,如下: CGRect imageRect = (CGRect){100, 100, 100, 100}; UIImageView *imageView = [[[UIImageView alloc] initWithFrame:imageRect] autorelease]; imageView.backgroundColor = [UIColor yellowColor]; [self.view addSubview:imageView]; UIButton *ma

WPF编程--如何屏蔽对话框中的鼠标事件

问题描述 WPF开发,做了一个可以用鼠标画一个矩形来选择范围进行放大的控件,发现当使用对话框来选择文件后,底下的控件受到了影响:被误以为选择了范围,然后就放大控件,如何屏蔽这个事件? 解决方案 解决方案二:在事件处理器中处理解决方案三:被误以为选择了范围->你选择文件之后和"范围"有什么关系

疑问-IOS开发,cell中的UIView上添加的按钮不能响应事件

问题描述 IOS开发,cell中的UIView上添加的按钮不能响应事件 cell 里添加UIView,UIView上添加按钮,但是按钮不能响应点击时间,交互都打开了,怎么回事,求解.. 解决方案 可能是线程卡死,也可能是所有事件全部都集中在了UIView上无法分发时间,这里给你推荐一个博客http://blog.csdn.net/u013243469/article/details/41284027

MFC 按钮添加=BN_CLICKED后 及父窗口类中 不响应按钮点击事件

问题描述 MFC 按钮添加=BN_CLICKED后 及父窗口类中 不响应按钮点击事件 按钮类 添加消息处理 子窗口 =BN_CILCKED void C4PicBtn::OnBnClicked() { MessageBox(L"1"); } 父窗口 ON_BN_CLICKED(IDC_BtnPlay, &CMp3PlayerDlg::OnBnClickedBtnplay) void CMp3PlayerDlg::OnBnClickedBtnplay() { MessageBox

UIView独占响应事件

exclusiveTouch A Boolean value that indicates whether the receiver handles touch events exclusively. Setting this property to YES causes the receiver to block the delivery of touch events to other views in the same window. The default value of this p

触屏中的JavaScript事件分析_javascript技巧

本文实例讲述了触屏中的JavaScript事件.分享给大家供大家参考.具体分析如下: 一.触摸事件 ontouchstart ontouchmove ontouchend ontouchcancel目前移动端浏览器均支持这4个触摸事件,包括IE.由于触屏也支持MouseEvent,因此他们的顺序是需要注意的:touchstart → mouseover → mousemove → mousedown → mouseup → click1 实例如下: /** * onTouchEvent */ v