zoukankan      html  css  js  c++  java
  • iOS 事件穿透

    - (void)viewDidLoad {

        [super viewDidLoad];

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

        

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

        btn.frame = CGRectMake(80, 100, 100, 50);

        btn.backgroundColor = [UIColor redColor];

        [btn setTitle:@"click me" forState:UIControlStateNormal];

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

        [self.view addSubview:btn];

        

        MyView1 *view = [[MyView1 alloc] initWithTestFrame:CGRectMake(100, 100, 200, 200)];

        [self.view addSubview:view];

    }

    // ---------------------------------------------自定义VIEW

    #import "MyView1.h"

    @implementation MyView1

    /*

    // Only override drawRect: if you perform custom drawing.

    // An empty implementation adversely affects performance during animation.

    - (void)drawRect:(CGRect)rect {

        // Drawing code

    }

    */

    UIButton *btn;

    -(id)initWithTestFrame:(CGRect) frame

    {

        self = [super initWithFrame:frame];

        if(self)

        {

            

            self.backgroundColor = [UIColor blueColor];

            [self drawView];

            

    //        self.userInteractionEnabled = NO;

        }

        return self;

    }

    -(void) drawView

    {

        

        btn = [UIButton buttonWithType:UIButtonTypeCustom];

        btn.frame = CGRectMake(0, 0, 50, 30);

        btn.backgroundColor = [UIColor blackColor];

        [btn setTitle:@"btn" forState:UIControlStateNormal];

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

        [self addSubview:btn];

    }

    - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event

    {

        

        CGRect rect = btn.frame;

        BOOL containsPoint = CGRectContainsPoint(rect, point);

        if(containsPoint)

        {

            self.userInteractionEnabled = YES;

        }

        else

        {

            self.userInteractionEnabled = NO;

        }

        

        UIView *touchedView = [super hitTest:point withEvent:event];

        

        return touchedView;

    }

    -(void)click

    {

        NSLog(@"black...");

    }

    @end

  • 相关阅读:
    JVM源码分析之Object.wait/notify(All)完全解读
    进程无故消失的破案历程
    Jmeter——JDBC Connection Configuration参数化
    Jmeter——CSV DataSet Config参数化
    WeTest明星工具-移动端性能测试PerfDog初探
    基于appium实现的线性代码引用unittest单元测试框架
    Requests实践详解
    Appium-Server与Appium-Desktop的区别
    Appium Python API 中文版
    单元测试框架Uinttest一文详解
  • 原文地址:https://www.cnblogs.com/xiangjune/p/4883080.html
Copyright © 2011-2022 走看看