美文网首页
IOS开发 事件响应链

IOS开发 事件响应链

作者: 奔跑的小小鱼 | 来源:发表于2017-05-19 22:11 被阅读78次

本节学习内容:

1.事件响应链的概念

2.事件响应链的传递机制

3.事件响应链的应用

事件响应原理

响应顺序 Subview>MainView>UIView>VCRoot>MyWindow>UIApplication>AppDelegate

1.添加一个视图控制器命名为VCRoot

【VCRoot.h】

#import<UIKit/UIKit>

#import"MainView.h"

#import"subView.h"

@interface VCRoot:UIViewConroller{

//主视图定义

MainView * _mainView

//子视图对象定义

SubView* _subView;

}

@end

【VCRoot.m】

#import"VCRoot.h"

@interface VCRoot()

@end

@implementation VCRoot

-(void)viewDidLoad{

[super viewDidLoad];

//创建主视图

_mainView=[[MainView alloc]init];

_mainView.frame=CGRectMAke(50,50,200,300);

_mainView.backgroundColor=[UIColor orangeColor];

[self.view addSubview:_mainView];

//创建子视图

_subView=[[subView alloc]init];

_subView.frame=CGRectMake(30,30,100,200);

[_mainView addSubview:_subView];

[self.view addSubview:_mainView];

//改变视图背景颜色

self.view.backgroundColor=[UIColor blueColor];

}

//当点击屏幕时,调用此函数

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

NSLog(@"RootView 事件响应!");

}

2.创建一个主视图命名为:mainview

#import"MainView.h"

@iplementation MainView

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

NSLog(@"MainView 事件响应!next==@%",self.nextResponder);

//手动响应下传递

[super touchesBegan:touches withEvent:event];

}

3.创建一子视图命名为SubView

【SubView.m】

#import"subView.h"

@implementation subView

//在子视图中优先级最高,当响应事件,事件到此结束-

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

NSLog(@"SubView 事件响应!next==@%",self.nextResponder);

//手动响应下传递

[super touchesBegan:touches withEvent:event];

}

4.创建一个Window命名为MyWindow

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

NSLog(@"MyWindow事件响应!next==@%",self.nextResponder);

//手动响应下传递

[super touchesBegan:touches withEvent:event];

}

5.创建一个UIApplication命名为MyApplication

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

NSLog(@"MyApplication 事件响应!next==@%",self.nextResponder);

//手动响应下传递

[super touchesBegan:touches withEvent:event];

}


【AppDelegate.m】

#import"AppDelegate.h"

#import"VCRoot.h"

#import"Myapplication.h"

#import"MyWindow.h"

@interface AppDelegate()

@end

@implementation AppDelegate

//appdelegate是最后一个响应

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

NSLog(@"MyApplication 事件响应!next==@%",self.nextResponder);

//手动响应下传递

[super touchesBegan:touches withEvent:event];

}

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

//创建一个window对象

self.window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];

//根视图控制器创建

self.window.rootViewController=[[VCRoog alloc]init];

[self.window makeKeyAndVisible];

return YES;

}

执行结果事件响应顺序:

事件响应顺序日志

相关文章

  • iOS 响应链

    iOS开发 - 事件传递响应链iOS 响应者链,事件的传递事件传递之响应链Cocoa Touch事件处理流程--响...

  • 二、事件传递链和响应者链

    iOS触摸事件详解iOS开发-事件传递响应链 响应者链 UIResponser包括了各种Touch message...

  • iOS响应者链

    参考好文 iOS开发-事件传递响应链,用运行时分析 iOS事件传递:响应者链[译] http://www.jian...

  • 深入浅出iOS事件机制

    深入浅出iOS事件机制事件传递:响应链事件传递响应链

  • 响应链

    iOS事件响应链中Hit-Test View的应用从iOS的事件响应链看TableView为什么不响应touche...

  • 关于IOS中的响应者链

    今天简单的谈一下IOS开发中经常用到的响应者链, 在ios开发中所有能够响应touch事件的对象都继承自UISRe...

  • IOS开发 事件响应链

    本节学习内容: 1.事件响应链的概念 2.事件响应链的传递机制 3.事件响应链的应用 响应顺序 Subview>M...

  • 事件的响应链与传递链

    iOS事件链有两条:事件的响应链;Hit-Testing事件的传递链 响应链:由离用户最近的view向系统传递。i...

  • iOS 中事件的响应链和传递链

    iOS事件链有两条:事件的响应链;Hit-Testing事件的传递链 响应链:由离用户最近的view向系统传递。i...

  • iOS 中事件的响应链和传递链

    iOS事件链有两条:事件的响应链;Hit-Testing事件的传递链 响应链:由离用户最近的view向系统传递。i...

网友评论

      本文标题:IOS开发 事件响应链

      本文链接:https://www.haomeiwen.com/subject/qafexxtx.html