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

简介:

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

没有屏蔽时候的现象:

屏蔽时候的现象:

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

//
//  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

目录
相关文章
|
24天前
|
传感器 人工智能 算法
掌握C++中的状态-事件回调矩阵:打造强大的有限状态机
掌握C++中的状态-事件回调矩阵:打造强大的有限状态机
38 0
|
7月前
[虚幻引擎插件介绍] DTGlobalEvent 蓝图全局事件, Actor, UMG 相互回调,自由回调通知事件函数,支持自定义参数。
本插件可以在虚幻的蓝图 Actor, Obiect,UMG 里面指定绑定和执行消息,可带自定义参数。 参数支持 Bool,Byte,Int,Int64,Float,Name,String,Text,Vector,Rotator,Transform,Object,Actor。
42 0
uiu
|
JavaScript 调度
【Svelte】-(5)DOM事件 / 事件修饰符 / 组件事件 / 事件转发 (组件之间的事件沟通)
【Svelte】-(5)DOM事件 / 事件修饰符 / 组件事件 / 事件转发 (组件之间的事件沟通)
uiu
143 0
【Svelte】-(5)DOM事件 / 事件修饰符 / 组件事件 / 事件转发 (组件之间的事件沟通)
|
前端开发
如何自定义Electron的通知事件,自定义窗口大小
当外层添加拖拽后,里层添加点击事件时,点击事件的元素要添加这个样式方能添加点击事件
|
JavaScript 前端开发 API
SwiftUI—如何实现对视图显示和消失事件的监听
SwiftUI—如何实现对视图显示和消失事件的监听
589 0
SwiftUI—如何实现对视图显示和消失事件的监听
父组件异步请求到的数据,要在子组件显示
父组件异步请求到的数据,要在子组件显示(父传子,子传父)
父组件异步请求到的数据,要在子组件显示
|
Java Linux Android开发
【Android 事件分发】事件分发源码分析 ( 驱动层通过中断传递事件 | WindowManagerService 向 View 层传递事件 )
【Android 事件分发】事件分发源码分析 ( 驱动层通过中断传递事件 | WindowManagerService 向 View 层传递事件 )
268 0
【Android 事件分发】事件分发源码分析 ( 驱动层通过中断传递事件 | WindowManagerService 向 View 层传递事件 )
|
JSON 小程序 JavaScript
小程序事件、组件、生命周期、路由及数据请求
小程序事件、组件、生命周期、路由及数据请求
228 0
小程序事件、组件、生命周期、路由及数据请求
|
JavaScript 前端开发
如何找到一个前端事件对应的事件处理函数
如何找到一个前端事件对应的事件处理函数
312 0
如何找到一个前端事件对应的事件处理函数