码迷,mamicode.com
首页 > 移动开发 > 详细

IOS中的响应者链

时间:2015-08-27 22:45:16      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

响应者链就是当子视图不响应,父视图有响应事件,父视图响应

#import "RootViewController.h"
#import "ResponderView.h"
@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    ResponderView *viewTop = [[ResponderView alloc] initWithFrame:CGRectMake(0, 0, 320, 284)];
    viewTop.tag = 100;
    viewTop.backgroundColor = [UIColor redColor];
    [self.view addSubview:viewTop];
    [viewTop release];
    ResponderView *viewDow = [[ResponderView alloc] initWithFrame:CGRectMake(0, 284, 320, 284)];
    viewDow.tag = 101;
    viewDow.userInteractionEnabled = NO;
    viewDow.backgroundColor = [UIColor yellowColor];
    
    [self.view addSubview:viewDow];
    [viewDow release];
    
    ResponderView *viewNe1 = [[ResponderView alloc] initWithFrame:CGRectMake(40, 40, 240, 204)];
    viewNe1.tag = 102;
    viewNe1.backgroundColor = [UIColor greenColor];

    [viewDow addSubview:viewNe1];
    [viewNe1 release];
    
    ResponderView *viewNe2 = [[ResponderView alloc] initWithFrame:CGRectMake(40, 40, 160, 124)];
    viewNe2.tag = 103;
    viewNe2.backgroundColor = [UIColor blueColor];
    [viewNe1 addSubview:viewNe2];
    [viewNe2 release];


    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(20, 50, 100, 100)];
    lab.backgroundColor = [UIColor greenColor];
   // lab.userInteractionEnabled = NO;
    lab.tag = 104;
    [viewTop addSubview:lab];
    [lab release];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
   // btn.backgroundColor = [UIColor blueColor];
    btn.frame = CGRectMake(0, 0, 50,50);
    btn.tag = 105;
    //btn.userInteractionEnabled = NO;
    [btn setTitle:@"按钮" forState:UIControlStateNormal];
    [lab addSubview:btn];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"我是视图控制器 我响应!");
}
@end
#import "ResponderView.h"

@implementation ResponderView

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    switch (self.tag) {
        case 100:
            NSLog(@"红色视图");
            break;
        case 101:
             NSLog(@"黄色视图");
            break;
        case 102:
             NSLog(@"绿色视图");
            break;
        case 103:
             NSLog(@"蓝色视图");
            break;
        case 104:
            NSLog(@"fdsf视图");
            break;
        case 105:
            NSLog(@"fdsfddddd视图");
            break;
        default:
            break;
    }
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
    
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    
}


@end

 

IOS中的响应者链

标签:

原文地址:http://www.cnblogs.com/wohaoxue/p/4764734.html

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