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

这篇教程是上一篇教程的升级版,将复杂的代码封装成了category,更便于使用:)
效果:

源码:
UIGestureRecognizer+EnvetInCurrentView.h 与 UIGestureRecognizer+EnvetInCurrentView.m
//
// UIGestureRecognizer+EnvetInCurrentView.h
// BackgroundView
//
// Created by YouXianMing on 14-10-3.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import <UIKit/UIKit.h> typedef void (^CurrentViewBlock)(UIGestureRecognizer *gesture);
typedef void (^OtherViewBlock)(UIGestureRecognizer *gesture); @interface UIGestureRecognizer (EnvetInCurrentView) - (void)eventInCurrentView:(CurrentViewBlock)currentViewBlock
inOtherView:(OtherViewBlock)otherViewBlock; @end
//
// UIGestureRecognizer+EnvetInCurrentView.m
// BackgroundView
//
// Created by YouXianMing on 14-10-3.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "UIGestureRecognizer+EnvetInCurrentView.h" @implementation UIGestureRecognizer (EnvetInCurrentView) - (void)eventInCurrentView:(CurrentViewBlock)currentViewBlock
inOtherView:(OtherViewBlock)otherViewBlock
{
UIEvent *event = [[UIEvent alloc] init];
CGPoint location = [self locationInView:self.view]; //check actually view you hit via hitTest
UIView *view = [self.view hitTest:location withEvent:event]; if ([view.gestureRecognizers containsObject:self]) {
currentViewBlock(self);
} else {
otherViewBlock(self);
}
} @end
使用时候的源码:
//
// ViewController.m
// BackgroundView
//
// Created by YouXianMing on 14-10-3.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "ViewController.h"
#import "UIView+BackgroundView.h"
#import "UIGestureRecognizer+EnvetInCurrentView.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:.f];
label.textColor = [UIColor redColor];
[self.view addSubview:label];
} - (void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer
{
[gestureRecognizer eventInCurrentView:^(UIGestureRecognizer *gesture) {
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:.f];
} inOtherView:^(UIGestureRecognizer *gesture) {
NSLog(@"其他视图的事件");
}];
} - (void)affterDelay
{
// 隐藏
[self.view removeBackgroundView];
} @end
以下是需要注意的地方:

屏蔽响应事件继续向父视图传递的category的更多相关文章
- iOS--子视图如何穿透上层视图响应事件
一.使用方法:- (nullable UIView *)hitTest:(CGPoint)point withEvent:(nullable UIEvent *)event; 二.背景知识iOS系统检 ...
- 使用AppDelegate单例,解决子视图无法给父视图发送消息的问题
关于单例模式,我会在实验过后再开一个博客重点讲单例的使用,这里只是介绍我在PhotoForBingyan的照片滤镜的项目中使用AppDelegate单例的情况. 碰到的问题: 由于这个项目是个多视图的 ...
- 让超出父视图范围的子视图响应事件,在UIView范围外响应点击
/** * 在父视图中重写该方法,这样可使超出部分响应事件. */ - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { ...
- ios 怎么禁止点击子视图的时候不响应父视图的点击事件
方法一 可以在触发手势的方法里添加一个区域的判断,如果点击区域正好是子视图的区域,则过滤掉,不处理此时的手势,如果点击的区域没有被子视图覆盖则,处理手势的事件.具体的代码如下: if( CGRect ...
- IOS子视图超过父视图frame后,无法交互响应
确定第一响应者 当用户触发某一事件(触摸事件或运动事件)后,UIKit会创建一个事件对象(UIEvent),该对象包含一些处理事件所需要的信息.然后事件对象被放到一个事件队列中.这些事件按照先进先出的 ...
- vue子组件使用自定义事件向父组件传递数据
使用v-on绑定自定义事件可以让子组件向父组件传递数据,用到了this.$emit(‘自定义的事件名称’,传递给父组件的数据) <!DOCTYPE html> <html lang= ...
- 从uibutton的点击谈谈ios的响应事件
最近在做一个项目,接连遇到两个关于点击事件的问题. 1.点击button不能响应事件的. 2.子view的frame超出了父view的容器大小,也不能响应点击事件. 效果图如右: 1.第一张图中的弹出 ...
- 超出父视图无法点击问题hitTest
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c91b13 } p.p2 { margin: 0.0px 0. ...
- UIImageView中的UIButton不响应事件解决方案
如下: CGRect imageRect = (CGRect){, , , }; UIImageView *imageView = [[[UIImageView alloc] initWithFram ...
随机推荐
- C++中虚函数的动态绑定和多态性
目录 静态类型 vs 动态类型.静态绑定 vs 动态绑定 虚函数动态绑定实现机制.虚析构函数 多态性 一.静态 vs 动态 静态类型 VS 动态类型.静态类型指的是对象声明的类型,在编译器确定的.动态 ...
- elasticsearch分析系列
http://www.opscoder.info/category/ElasticSearch/?page=2 https://www.jianshu.com/p/0908b9ee65fc https ...
- android开发学习笔记系列(1)-android起航
前言 在学习安卓的过程中,我觉得非常有必要将自己所学的东西进行整理,因为每每当我知道我应该是如何去实现功能的时候,有许多细节问题我总是会遗漏,因此我也萌生了写一系列博客来描述自己学习的路线,让我的an ...
- c#基础学习(0806)之抽象类实现多态
首先,要判断是否使用抽象类,可以从下面两个方面进行判断: 1.是不是需要被实例化 2.父类中有没有默认的实现 如果不需要被实例化,父类中没有默认的实现,则用抽象类(否则用虚方法来实现) 下面举个简单的 ...
- C#动态创建Gridview及批量插入到数据库
这里介绍两种动态创建Gridview的方法: (一).有时需要应付上头领导的检查,所以就弄一些静态的Gridview来显示数据,这种方法的优点就是不用连接数据库,比较方便,但是代码灵活性不高,所有数据 ...
- EL表达式无法获取Spring MVC的Model封装好的数据解决方法
1.在spring-mvc的配置文件中已经配置jsp的视图解析器 2.在Controller中使用Model的addAttribute方法添加属性name,msg 3.在jsp页面中使用${msg}取 ...
- 纠错帖:Zuul & Spring Cloud Gateway & Linkerd性能对比 (转载)
纠错帖:Zuul & Spring Cloud Gateway & Linkerd性能对比 Spring Cloud Spring Cloud Spring Cloud Gatew ...
- iOS中表视图单元格事件用nib和storyboard的两种写法总结
从ios6开始,苹果公司推出了storyborad技术取代了nib的写法,这样代码量确实少写了很多,也比较简洁.但是,从学习的角度来说,阿堂认为 用nib的写法,虽然多了些代码,但是对于掌握知识和原理 ...
- CCIE
400-251: CCIE Security Written
- Visualizing LSTM Layer with t-sne in Neural Networks
LSTM 可视化 Visualizing Layer Representations in Neural Networks Visualizing and interpreting represent ...