屏蔽响应事件继续向父视图传递的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的更多相关文章

  1. iOS--子视图如何穿透上层视图响应事件

    一.使用方法:- (nullable UIView *)hitTest:(CGPoint)point withEvent:(nullable UIEvent *)event; 二.背景知识iOS系统检 ...

  2. 使用AppDelegate单例,解决子视图无法给父视图发送消息的问题

    关于单例模式,我会在实验过后再开一个博客重点讲单例的使用,这里只是介绍我在PhotoForBingyan的照片滤镜的项目中使用AppDelegate单例的情况. 碰到的问题: 由于这个项目是个多视图的 ...

  3. 让超出父视图范围的子视图响应事件,在UIView范围外响应点击

    /** *  在父视图中重写该方法,这样可使超出部分响应事件. */ - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {  ...

  4. ios 怎么禁止点击子视图的时候不响应父视图的点击事件

    方法一 可以在触发手势的方法里添加一个区域的判断,如果点击区域正好是子视图的区域,则过滤掉,不处理此时的手势,如果点击的区域没有被子视图覆盖则,处理手势的事件.具体的代码如下:  if( CGRect ...

  5. IOS子视图超过父视图frame后,无法交互响应

    确定第一响应者 当用户触发某一事件(触摸事件或运动事件)后,UIKit会创建一个事件对象(UIEvent),该对象包含一些处理事件所需要的信息.然后事件对象被放到一个事件队列中.这些事件按照先进先出的 ...

  6. vue子组件使用自定义事件向父组件传递数据

    使用v-on绑定自定义事件可以让子组件向父组件传递数据,用到了this.$emit(‘自定义的事件名称’,传递给父组件的数据) <!DOCTYPE html> <html lang= ...

  7. 从uibutton的点击谈谈ios的响应事件

    最近在做一个项目,接连遇到两个关于点击事件的问题. 1.点击button不能响应事件的. 2.子view的frame超出了父view的容器大小,也不能响应点击事件. 效果图如右: 1.第一张图中的弹出 ...

  8. 超出父视图无法点击问题hitTest

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c91b13 } p.p2 { margin: 0.0px 0. ...

  9. UIImageView中的UIButton不响应事件解决方案

    如下: CGRect imageRect = (CGRect){, , , }; UIImageView *imageView = [[[UIImageView alloc] initWithFram ...

随机推荐

  1. linux更改文件权限

    chown –Rh cheat:cheat /home/cheat/task/Cheat

  2. springcloud-06-feign的使用

    在spring Cloud Netflix栈中,各个微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使用HTTP客户端.我们可以使用JDK原生的URLConnection.Ap ...

  3. 5-niginx-健康检查模块

    1,  nginx有一个自带的健康检查模块, 过于丑陋... 只需要在 nginx.conf下的http中的server配置如下即可 location /basic_status { stub_sta ...

  4. 配置私有仓库(使用registry镜像搭建一个私有仓库)

    在使用Docker一段时间后,往往会发现手头积累了大量的自定义镜像文件,这些文件通过公有仓库进行管理并不方便:另外有时候只是希望在内部用户之间进行分享,不希望暴露出去.这种情况下,就有必要搭建一个本地 ...

  5. php发送get请求

    感谢:http://www.zoneself.org/2014/07/21/content_2665.html 1.用PHP发送get请求,很简单: <?php $url='http://www ...

  6. PLSQL计算质数

    看到有人实现了一个计算质数的函数,就是效率有点差,贴一个以前写的计算质数的算法. 目标很简单,列出100以内的质数.其实算法很简单,两个循环就搞定了.但是发现使用不同的算法,执行效率差别之大相当惊人, ...

  7. ruby文件操作

    Ruby代码 1.#读文件 2.f = File.open("myfile.txt", "r") 3.f.each_line do|line| 4.puts & ...

  8. Object-C语言Block的实现方式

    开场白 Block基本概念 中间态转换方法 Block编译后结果分析 Block运行时状态与编译状态对比   开场白   Object-C语言是对C语言的扩展,所以将OC源码进行编译的时候,会将OC源 ...

  9. MySQL中You can't specify target table for update in FROM clause异常

    mysql中You can't specify target table <tbl> for update in FROM clause错误的意思是说,不能先select出同一表中的某些值 ...

  10. Linux skbuff注释笔记

    SKB结构定义   /usr/src/linux/include/linux/skbuff.h sk_buff_head: struct sk_buff_head { //SKB的头结点 /* The ...