码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-10-03 10:25:24      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   ar   for   sp   div   

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

bubuko.com,布布扣

没有屏蔽时候的现象:

bubuko.com,布布扣

屏蔽时候的现象:

bubuko.com,布布扣

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

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

以下是核心代码处:

bubuko.com,布布扣

解决问题的出处:

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

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

标签:style   blog   http   color   io   ar   for   sp   div   

原文地址:http://www.cnblogs.com/YouXianMing/p/4004719.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!