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

  • 相关阅读:
    Android之旅十六 android中各种资源的使用
    XTU OJ 1207 Welcome to XTCPC (字符串签到题)
    scala并发编程原生线程Actor、Case Class下的消息传递和偏函数实战
    【云图】怎样设置支付宝里的家乐福全国连锁店地图?
    怎样在QML中使用multitouch
    软件project师周兆熊给IT学子的倾情奉献
    Linux系统下怎样配置SSH?怎样开启SSH?
    数学之路-python计算实战(4)-Lempel-Ziv压缩(2)
    Day5上午解题报告
    一份只有巨佬才能看懂的代码
  • 原文地址:https://www.cnblogs.com/xiangjune/p/4883080.html
Copyright © 2011-2022 走看看