UI 事件处理UITouch

一、事件的基本概念

UIEvent:事件,是由硬件捕捉的⼀个表⽰用户操作设备的对象。分三类:触摸事件、晃动事件、远程控制事件

触摸事件:⽤户通过触摸设备屏幕操作对象、输⼊数据。⽀持多点触摸,包含1个到多个触摸点


二、触摸的基本概念

1、实现触摸:

UIView⽀持触摸事件(因为继承于UIResponder),⽽且支持多点触摸。

需要定义UIView⼦类,实现触摸相关的⽅法。

touches..begantouches..movedtouches...ended touches..canceled

2、使用触摸实现手势

⼿势:有规律的触摸。

UITouch代表触摸在屏幕上的⼀根⼿指。可以获取触摸时间和触摸位置。

如何获取touch对象。touches集合中包含了视图上的所有⼿势。

三、响应者链

响应者链:由多个响应者对象组成的链。

响应者:

UIResponder。响应者类。

iOS中所有能响应事件(触摸、晃动、远程事件)的对象都是响应者。

系统定义了⼀个抽象的⽗类UIResponder来表⽰响应者。其⼦类都是响应者。

检测触碰视图:

硬件检测到触摸操作,会将信息交给UIApplication,开始检测。

UIApplication -> window -> viewController -> view -> 检测所有⼦视图

最终确认触碰位置,完成响应者链的查询过程。

处理触碰事件:

检测到响应者后,实现touchesBegan:withEvent:等⽅方法,即处理事件。

如果响应者没有处理事件,事件会向下传递。如果没有响应者处理,则丢弃触摸事件。

事件处理的顺序与触摸检测查询相反。

触摸的⼦视图 -> view -> viewController -> window -> UIApplication

阻断响应者链:

响应者链可以被打断。⽆法完成检测查询过程。

视图类的属性 : userInteractionEnabled。关闭后能阻断查询过程。

MyView.h文件的内容


@interface MyView : UIView

@property(nonatomic,retain) UIView *redView;
@property(nonatomic,assign) CGPoint startPoint;

@end


MyView.m文件的内容


@implementation MyView

- (void)dealloc
{
[_redView release];
[super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
//打开多点触摸。UIView默认是单点触摸
self.multipleTouchEnabled=YES;
self.backgroundColor=[UIColor cyanColor];
UIView *redView=[[UIView alloc]initWithFrame:CGRectMake(100, 100, 150, 150)];
redView.backgroundColor=[UIColor redColor];
[self addSubview:redView];
[redView release];
//属性与redView建立关联
self.redView=redView;

UIView *view=[[UIView alloc]initWithFrame:CGRectMake(20, 100, 100, 100)];
view.backgroundColor=[UIColor yellowColor];
//打断事件的交互
view.userInteractionEnabled=NO;
[self addSubview:view];
[view release];

UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
button.frame=CGRectMake(10, 10, 40, 30);
button.backgroundColor=[UIColor redColor];
[button setTitle:@"button" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:button];

}

return self;
}

- (void)buttonAction:(UIButton*)button
{
NSLog(@"buttonAction:");
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//获取触摸开始的点,并赋值给实例变量startPoint
CGPoint startPoint=[[touches anyObject] locationInView:self];
self.startPoint=startPoint;

//获取手指
UITouch *touch=[touches anyObject];
//打印点击次数
NSLog(@"点击%d次",touch.tapCount);
//改变redView的颜色
UIColor *randomColor=[UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
self.redView.backgroundColor=randomColor;
}


-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
//打印函数名,行数
NSLog(@"%s %d",__FUNCTION__,__LINE__);
//打印手指的个数
NSLog(@"%d",touches.count);
//1.获取移动先前的一个位置
UITouch *touch=[touches anyObject];
CGPoint prePoint=[touch previousLocationInView:self];
//获取touch发生在哪个视图上
UIView *touchView=[touch view];
if (touchView==self.redView) {
//2.获取当前位置
CGPoint nowPoint=[touch locationInView:self];
//3.计算偏移量
float offsetX=nowPoint.x-prePoint.x;
float offsetY=nowPoint.y-prePoint.y;
//4.根据偏移量改变redView的位置
self.redView.center=CGPointMake(self.redView.center.x+offsetX, self.redView.center.y+offsetY);
}
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//实现轻扫手势,并判断方向
//获取手指所在的位置,即触摸结束的位置

CGPoint endPoint=[[touches anyObject] locationInView:self];
//计算从触摸开始到结束的偏移量
float offsetX=endPoint.x-self.startPoint.x;
float offsetY=endPoint.y-self.startPoint.y;
//根据偏移量,确定是向哪个方向轻扫
if (offsetX>50&&fabs(offsetY)<=50) {
NSLog(@"向右");
}else if (offsetX<-50&&fabs(offsetY)<=50)
{
NSLog(@"向左");
}else if (fabs(offsetX)<50&&offsetY>50)
{
NSLog(@"向下");
}else if(fabs(offsetX)<50&&offsetY<-50){
NSLog(@"向上");
}
NSLog(@"%s %d",__FUNCTION__,__LINE__);
}

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"%s %d",__FUNCTION__,__LINE__);
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/


@end







Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值