zoukankan      html  css  js  c++  java
  • 触摸事件的传递

    提示:UIImageView的userInteractionEnabled 默认就是NO,因此UIImageView以及它的子控件默认是不能接触事件的

    Main.storyboard

    ViewController.m

    //

    //  ViewController.m

    //  7A02.触摸事件的传递

    //

    //  Created by huan on 16/2/3.

    //  Copyright © 2016 huanxi. All rights reserved.

    //

     

    #import "ViewController.h"

     

    @interface ViewController ()

    @property (weak, nonatomic) IBOutlet UIImageView *imageView;

     

    @end

     

    @implementation ViewController

     

    - (void)viewDidLoad {

        [super viewDidLoad];

        // Do any additional setup after loading the view, typically from a nib.

        //当父控件不能接收触摸事件的时候,它的子控制件不在遍历

        //当触摸点不在父控件的事件,它的子控件也不在遍历

        

        //往图片添加一个按钮

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd];

        [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];

        [self.imageView addSubview:btn];

    }

    -(void)btnClick{

        NSLog(@"%s", __func__);

    }

     

    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

        NSLog(@"%s %p", __func__, event);

    }

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

     

    @end

    CZWhiteView.m(控制器的view)

    #import "CZWhiteView.h"

     

    @implementation CZWhiteView

     

    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

        NSLog(@"%s %p", __func__, event);

    }

     

    @end

    CZRedView.m

    #import "CZRedView.h"

     

    @implementation CZRedView

     

    /**

     * 不实现touchesBegan方法,默认把事件传给上一个响应者(接收事件,不处理,交给上一个响应者)

     */

    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

        NSLog(@"%s, %p", __func__, event);

        //调用了super 方法,相当于把事件传给上一个响应者

        [super touchesBegan:touches withEvent:event];

    }

    @end

    CZGreen.m

    #import "CZGreenView.h"

     

    @implementation CZGreenView

     

    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

        NSLog(@"%s", __func__);

    }

     

    #pragma mark 判断当前的触摸点在不在自己的身上

    -(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{

        

        //绿色左边的可以响应事件,右边不可以响应事件

        if (point.x <= self.bounds.size.width * 0.5) {

            return YES;

        }

    //    BOOL pointInside = [super pointInside:point withEvent:event];

    //    NSLog(@"%s %d", __func__, pointInside);

    //    return pointInside;

        

        return NO;

    }

     

    @end

    CZBlue.m

    #import "CZBlueView.h"

     

    @implementation CZBlueView

     

    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

        NSLog(@"%s", __func__);

    }

    @end

     

  • 相关阅读:
    我们都可以把它放 Sidecar 容器中,这样微服务具备了 Super power,一种超能力
    DP 状态 DP 转移方程 动态规划解题思路
    完全二叉树 原因 完全二叉树最后一层节点靠左的原因
    延时任务最佳实践方案总结
    b+ 树 只存储 索引
    埋点质量保障体系建设
    linux命令重定向>、>>、 1>、 2>、 1>>、 2>>、 <
    卡特兰数
    python 使用函数名的字符串调用函数(4种方法)_black-heart的专栏-CSDN博客 https://blog.csdn.net/mrqingyu/article/details/84403924
    12 | 服务注册与发现如何满足服务治理
  • 原文地址:https://www.cnblogs.com/Lu2015-10-03/p/5179475.html
Copyright © 2011-2022 走看看