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

  • 相关阅读:
    【转】网络字节序与主机字节序
    VC之美化界面篇 (转)
    VS2008编译的程序在某些机器上运行提示“由于应用程序配置不正确,应用程序未能启动”的问题(转)
    符验手记
    一友人昨夜接到电话,发生何事
    [转]众VC论道IT峰会:投资是否靠运气
    路过一个小摊,看到一个有趣的现象
    PJSUA提示要注册线程的解决办法
    彩票股票金融与运气之研究(五) 明敌
    随手测一局婚姻,留验
  • 原文地址:https://www.cnblogs.com/xiangjune/p/4883080.html
Copyright © 2011-2022 走看看