zoukankan      html  css  js  c++  java
  • 在手势中屏蔽响应事件继续向父视图传递

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

    没有屏蔽时候的现象:

    屏蔽时候的现象:

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

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

  • 相关阅读:
    pyautogui页面点击和键盘输入
    eclipse把函数内容折叠的方法
    python 文件夹下文件及文件夹名称获取
    python中strftime和strptime函数
    python数组中在某一元素前插入数据
    python的递归函数
    java定义时间
    如何在word中插入代码块
    【图文详解】用Eclipse创建Maven Web项目
    【图文详解】Eclipse中添加Tomcat服务器
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/4004719.html
Copyright © 2011-2022 走看看