UI11-事件传递

本文介绍了一个简单的iOS应用实现触控响应的过程。首先在APPDelegate中设置窗口和根视图控制器,接着在MainViewController中添加不同颜色的子视图,并在各层级视图中实现触控开始、移动、结束等方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

创建MainViewController继承自UIViewController,创建SubView、MyWindow 继承自UIView

一、在APPDelegate.m中编写代码:

#import "AppDelegate.h"

#import "MyWindow.h"

#import "MainViewController.h"


@interface AppDelegate ()


@end


@implementation AppDelegate



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

    

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

    [self.window makeKeyAndVisible];

    //视图要有一个背景颜色才能被触控响应

    [self.window setBackgroundColor:[UIColor redColor]];

    [self.window setRootViewController:[[MainViewController alloc] init]];

    

    return YES;

}


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

{

    NSLog(@"%@ %@", [self class], NSStringFromSelector(_cmd));

}

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

{

    NSLog(@"%@ %@", [self class], NSStringFromSelector(_cmd));

}

//触控结束

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

{

    NSLog(@"%@ %@", [self class], NSStringFromSelector(_cmd));

}

//取消触控

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

{

    NSLog(@"%@ %@", [self class], NSStringFromSelector(_cmd));

}


@end


二、在MainViewController.m中编写代码:

#import "MainViewController.h"

#import "SubView.h"

@interface MainViewController ()


@end


@implementation MainViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    [self.view setBackgroundColor:[UIColor lightGrayColor]];

    

    SubView *subView1 = [[SubView alloc] initWithFrame:CGRectMake(20, 50, 100, 100)];

    [subView1 setBackgroundColor:[UIColor blackColor]];

    [subView1 setViewName:@"Black"];

    [self.view addSubview:subView1];

    

    SubView *subView2 = [[SubView alloc] initWithFrame:CGRectMake(200, 200, 100, 100)];

    [subView2 setBackgroundColor:[UIColor greenColor]];

    [subView2 setViewName:@"Green"];

    [self.view addSubview:subView2];

    

    SubView *subView3 = [[SubView alloc] initWithFrame:CGRectMake(0, 0, 50, 150)];

    [subView3 setBackgroundColor:[UIColor yellowColor]];

    [subView3 setViewName:@"Yellow"];

    [self.view addSubview:subView3];

    


}

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

{

    [super touchesBegan:touches withEvent:event];

    NSLog(@"%@ %@", [self class], NSStringFromSelector(_cmd));

}

@end


三、在SubView.m中编写代码

#import "SubView.h"


@implementation SubView



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

{

    [super touchesBegan:touches withEvent:event];

    NSLog(@"%@ %@", self.viewName, NSStringFromSelector(_cmd));

}

@end


四、MyWindow.m

#import "MyWindow.h"


@implementation MyWindow


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

{

//    [super touchesBegan:touches withEvent:event];

    NSLog(@"%@ %@", [self class], NSStringFromSelector(_cmd));

}


@end





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值