UI day 4 事件处理(UIEvent) 触摸 晃动 远程控制事件
2015-12-21 20:49
316 查看
1.事件的概念:(UIEvent)是由硬件设备捕捉到用户对设备的操作,把这个操作抽象成一个事件对象
iOS中的三大事件:触摸,晃动事件,远程控制事件;其中应用最广泛的是触摸事件
2.UIView是支持触摸的,由于UIView内部没有实现触摸相关的方法,所以我们在点击UIView创建的视图对象时,没有任何反应
3.如果想让一个视图对象触摸事件作出响应,需要在这个类的.m文件中实现根触摸相关一些方法
#pragma mark 1.刚开始的触摸的时候触发
- (void)touchesBegan:(NSSet
*)touches withEvent:(UIEvent
*)event
{
//当触摸到视图的时候改变自身的颜色
self.backgroundColor
= [UIColor
randomColor];
NSLog(@"%s",__FUNCTION__);
}
#pragma mark 2.当手指在视图范围内移动的时候触发
-(void)touchesMoved:(NSSet
*)touches withEvent:(UIEvent
*)event
{
self.backgroundColor
= [UIColor
randomColor];
self.center =
CGPointMake(arc4random_uniform(220-100+1)+100,
arc4random_uniform(460-100+1)+100);
NSLog(@"%s",__FUNCTION__);
}
#pragma mark 3.当我们的手指离开视图的时候触发
-(void)touchesEnded:(NSSet
*)touches withEvent:(UIEvent
*)event
{
// tocuchs集合中存放的是我们触摸时,屏幕上的手指的对象
//
获取屏幕上的手指对象,由于屏幕上只有一根手指,所有集合中只存在一个手指对象
UITouch
*finger = [touches anyObject];
//获取手指对象finger点击次数
// finger.tapCount
//点击一次的时候改变自身背景颜色
if
(1==finger.tapCount) {
// self.backgroundColor =[UIColor randomColor];
//当点击次数是1的时候,延迟改变自身的颜色,看看是否还有一次以上的点击操作
[self
performSelector:@selector(ChangeBackgroundColor)
withObject:nil
afterDelay:0.3];
}else
if(2
== finger.tapCount){
//当点击次数大一1的时候,取消之前改变自身颜色的任务
[NSObject
cancelPreviousPerformRequestsWithTa
114cb
rget:self
selector:@selector(ChangeBackgroundColor)
object:nil];
//其他情况改变父视图颜色
self.superview.backgroundColor
= [UIColor
randomColor];
}
// UITouch存放的是手指对象
NSLog(@"%s",__FUNCTION__);
}
#pragma mark 4.当触摸被中断的时候触发(eg:来电话了,来短信了)
-(void)touchesCancelled:(NSSet
*)touches withEvent:(UIEvent
*)event
{
NSLog(@"%s",__FUNCTION__);
}
- (void)ChangeBackgroundColor
{
self.backgroundColor
=[UIColor
randomColor];
}
MoveView类 实现移动
-(void)touchesMoved:(NSSet
*)touches withEvent:(UIEvent
*)event
{
//1.获取手指对象
UITouch
*finger = [touches anyObject];
//2.获取手指当前的在视图上的位置
CGPoint currentLoction = [finger
locationInView:self];
//3.获取手指之前在视图上的位置
CGPoint previousLoction = [finger
previousLocationInView:self];
//4.计数手指在x轴和y轴上的增量
//x轴上的增量
CGFloat dx = currentLoction.x
- previousLoction.x;
//y轴上的增量
CGFloat dy = currentLoction.y
- previousLoction.y;
//获取中心点
CGPoint
center = self.center;
self.center
=
CGPointMake(center.x
+ dx, center.y
+ dy);
}
//移动的过程
MoveView
*moveView = [[MoveView
alloc]initWithFrame:CGRectMake(110,
234,
100,
100)];
moveView.backgroundColor
= [UIColor
blueColor];
[self.view
addSubview:moveView];
[moveView release];
PinchView类 实现捏合
-(instancetype)initWithFrame:(CGRect)frame
{
if
(self
= [super
initWithFrame:frame]) {
//ios支持多点触摸,只是默认是单点触摸
self.multipleTouchEnabled
=
YES;
}
return
self;
}
-(void)touchesMoved:(NSSet
*)touches withEvent:(UIEvent
*)event
{
//如果touchs这个集合中的手指对象个数为1,就直接返回这个touchsMoved方法
if
(1
== touches.count) {
return;
}else{
//得到集合中所有的手指对象,并使用数组存储
NSArray
*allTouches = [touches allObjects];
UITouch
*touth1 = [allTouches firstObject];
UITouch
*touth2 = [allTouches lastObject];
//1.求出两个手指对象当前的位置
CGPoint
currentFirstPoint = [touth1
locationInView:self];
CGPoint
currentSecondPoint = [touth2
locationInView:self];
//2.当前两个点的距离
CGFloat
currentDistance = [self
distanceFromFirstPoint:currentFirstPoint
toSecondPoint:currentSecondPoint];
//3.求出之前两个手指的位置
CGPoint previousFirstPoint = [touth1
previousLocationInView:self];
CGPoint previousSecondPoint = [touth2
previousLocationInView:self];
//4.之前两个点直接的距离
CGFloat
previousDistance = [self
distanceFromFirstPoint:previousFirstPoint
toSecondPoint:previousSecondPoint];
//5.获取捏合前后两个手指间接地比例
CGFloat
rate = currentDistance/previousDistance;
//6.缩放视图,如果视图的大小发生变化时,而中心点的位置不发生变化,修改bounds就可以了
self.bounds
=
CGRectMake(0,
0,
self.bounds.size.width*rate,
self.bounds.size.height*rate);
}
}
//封装一个计算距离的法发
-(CGFloat)distanceFromFirstPoint:(CGPoint)firstPoint toSecondPoint:(CGPoint)secondPoint{
CGFloat
dx = firstPoint.x
- secondPoint.x;
CGFloat
dy = firstPoint.y
- secondPoint.y;
return
sqrt(dx*dx + dy*dy);
}
//创建捏合视图对象
PinchView
*pinchView = [[PinchView
alloc]initWithFrame:CGRectMake(60,
184,
200,
200)];
pinchView.backgroundColor
= [UIColor
orangeColor];
[self.view
addSubview:pinchView];
[pinchView
release];
UIResponder响应者
1.UIResponder响应者类,继承自NSObject,它是一个响应者的基类,它提供了一些处理事件的方法
2.什么是响应者(响应者对象):第一要继承自Responder 第二能对ios中的事件(触摸,晃动,远程控制事件)作出相应的对象就叫做响应者
UILabel,UIImageView默认用户交互是关闭的
响应者链:(由大到小)确定事件作用的对象时:UIAppcation-->AppDelegate-->window-->视图控制器-->视图控制器上的子视图
响应事件:(由小到大)视图控制器上的子视图-->视图控制器-->window-->Appdelegate-->UIAppcation如果都不处理这个事件就会被丢弃
关闭用户的交互后,这条响应者连就断开了,事件只能找他的上一级去处理,如果上一级都不出理,此时事件就不了了之
yellowView.userInteractionEnabled
=YES;
iOS中的三大事件:触摸,晃动事件,远程控制事件;其中应用最广泛的是触摸事件
2.UIView是支持触摸的,由于UIView内部没有实现触摸相关的方法,所以我们在点击UIView创建的视图对象时,没有任何反应
3.如果想让一个视图对象触摸事件作出响应,需要在这个类的.m文件中实现根触摸相关一些方法
#pragma mark 1.刚开始的触摸的时候触发
- (void)touchesBegan:(NSSet
*)touches withEvent:(UIEvent
*)event
{
//当触摸到视图的时候改变自身的颜色
self.backgroundColor
= [UIColor
randomColor];
NSLog(@"%s",__FUNCTION__);
}
#pragma mark 2.当手指在视图范围内移动的时候触发
-(void)touchesMoved:(NSSet
*)touches withEvent:(UIEvent
*)event
{
self.backgroundColor
= [UIColor
randomColor];
self.center =
CGPointMake(arc4random_uniform(220-100+1)+100,
arc4random_uniform(460-100+1)+100);
NSLog(@"%s",__FUNCTION__);
}
#pragma mark 3.当我们的手指离开视图的时候触发
-(void)touchesEnded:(NSSet
*)touches withEvent:(UIEvent
*)event
{
// tocuchs集合中存放的是我们触摸时,屏幕上的手指的对象
//
获取屏幕上的手指对象,由于屏幕上只有一根手指,所有集合中只存在一个手指对象
UITouch
*finger = [touches anyObject];
//获取手指对象finger点击次数
// finger.tapCount
//点击一次的时候改变自身背景颜色
if
(1==finger.tapCount) {
// self.backgroundColor =[UIColor randomColor];
//当点击次数是1的时候,延迟改变自身的颜色,看看是否还有一次以上的点击操作
[self
performSelector:@selector(ChangeBackgroundColor)
withObject:nil
afterDelay:0.3];
}else
if(2
== finger.tapCount){
//当点击次数大一1的时候,取消之前改变自身颜色的任务
[NSObject
cancelPreviousPerformRequestsWithTa
114cb
rget:self
selector:@selector(ChangeBackgroundColor)
object:nil];
//其他情况改变父视图颜色
self.superview.backgroundColor
= [UIColor
randomColor];
}
// UITouch存放的是手指对象
NSLog(@"%s",__FUNCTION__);
}
#pragma mark 4.当触摸被中断的时候触发(eg:来电话了,来短信了)
-(void)touchesCancelled:(NSSet
*)touches withEvent:(UIEvent
*)event
{
NSLog(@"%s",__FUNCTION__);
}
- (void)ChangeBackgroundColor
{
self.backgroundColor
=[UIColor
randomColor];
}
MoveView类 实现移动
-(void)touchesMoved:(NSSet
*)touches withEvent:(UIEvent
*)event
{
//1.获取手指对象
UITouch
*finger = [touches anyObject];
//2.获取手指当前的在视图上的位置
CGPoint currentLoction = [finger
locationInView:self];
//3.获取手指之前在视图上的位置
CGPoint previousLoction = [finger
previousLocationInView:self];
//4.计数手指在x轴和y轴上的增量
//x轴上的增量
CGFloat dx = currentLoction.x
- previousLoction.x;
//y轴上的增量
CGFloat dy = currentLoction.y
- previousLoction.y;
//获取中心点
CGPoint
center = self.center;
self.center
=
CGPointMake(center.x
+ dx, center.y
+ dy);
}
//移动的过程
MoveView
*moveView = [[MoveView
alloc]initWithFrame:CGRectMake(110,
234,
100,
100)];
moveView.backgroundColor
= [UIColor
blueColor];
[self.view
addSubview:moveView];
[moveView release];
PinchView类 实现捏合
-(instancetype)initWithFrame:(CGRect)frame
{
if
(self
= [super
initWithFrame:frame]) {
//ios支持多点触摸,只是默认是单点触摸
self.multipleTouchEnabled
=
YES;
}
return
self;
}
-(void)touchesMoved:(NSSet
*)touches withEvent:(UIEvent
*)event
{
//如果touchs这个集合中的手指对象个数为1,就直接返回这个touchsMoved方法
if
(1
== touches.count) {
return;
}else{
//得到集合中所有的手指对象,并使用数组存储
NSArray
*allTouches = [touches allObjects];
UITouch
*touth1 = [allTouches firstObject];
UITouch
*touth2 = [allTouches lastObject];
//1.求出两个手指对象当前的位置
CGPoint
currentFirstPoint = [touth1
locationInView:self];
CGPoint
currentSecondPoint = [touth2
locationInView:self];
//2.当前两个点的距离
CGFloat
currentDistance = [self
distanceFromFirstPoint:currentFirstPoint
toSecondPoint:currentSecondPoint];
//3.求出之前两个手指的位置
CGPoint previousFirstPoint = [touth1
previousLocationInView:self];
CGPoint previousSecondPoint = [touth2
previousLocationInView:self];
//4.之前两个点直接的距离
CGFloat
previousDistance = [self
distanceFromFirstPoint:previousFirstPoint
toSecondPoint:previousSecondPoint];
//5.获取捏合前后两个手指间接地比例
CGFloat
rate = currentDistance/previousDistance;
//6.缩放视图,如果视图的大小发生变化时,而中心点的位置不发生变化,修改bounds就可以了
self.bounds
=
CGRectMake(0,
0,
self.bounds.size.width*rate,
self.bounds.size.height*rate);
}
}
//封装一个计算距离的法发
-(CGFloat)distanceFromFirstPoint:(CGPoint)firstPoint toSecondPoint:(CGPoint)secondPoint{
CGFloat
dx = firstPoint.x
- secondPoint.x;
CGFloat
dy = firstPoint.y
- secondPoint.y;
return
sqrt(dx*dx + dy*dy);
}
//创建捏合视图对象
PinchView
*pinchView = [[PinchView
alloc]initWithFrame:CGRectMake(60,
184,
200,
200)];
pinchView.backgroundColor
= [UIColor
orangeColor];
[self.view
addSubview:pinchView];
[pinchView
release];
UIResponder响应者
1.UIResponder响应者类,继承自NSObject,它是一个响应者的基类,它提供了一些处理事件的方法
2.什么是响应者(响应者对象):第一要继承自Responder 第二能对ios中的事件(触摸,晃动,远程控制事件)作出相应的对象就叫做响应者
UILabel,UIImageView默认用户交互是关闭的
响应者链:(由大到小)确定事件作用的对象时:UIAppcation-->AppDelegate-->window-->视图控制器-->视图控制器上的子视图
响应事件:(由小到大)视图控制器上的子视图-->视图控制器-->window-->Appdelegate-->UIAppcation如果都不处理这个事件就会被丢弃
关闭用户的交互后,这条响应者连就断开了,事件只能找他的上一级去处理,如果上一级都不出理,此时事件就不了了之
yellowView.userInteractionEnabled
=YES;
相关文章推荐
- GUI - Web前端开发框架
- 评价ui设计作品好坏的八个标准(界面/交互设计研究)
- 为Yahoo! UI Extensions Grid增加内置的可编辑器
- Cocos2d-x UI开发之文本类使用实例
- 谈谈对jquery ui tabs 的理解
- 在IE 浏览器中使用 jquery的fadeIn() 效果 英文字符字体加粗
- UI Events 用户界面事件
- jquery UI Datepicker时间控件的使用方法(基础版)
- 基于jQuery UI CSS Framework开发Widget的经验
- jquery ui 1.7 ui.tabs 动态添加与关闭(按钮关闭+双击关闭)
- jQuery UI设置固定日期选择特效代码分享
- Jquery ajax 同步阻塞引起的UI线程阻塞问题
- jQuery UI的Dialog无法提交问题的解决方法
- jQuery ui实现动感的圆角渐变网站导航菜单效果代码
- jquery UI Datepicker时间控件的使用方法(终结版)
- jquery UI Datepicker时间控件的使用方法(加强版)
- 关于jQuery UI 使用心得及技巧
- jQuery ui 1.7更新小结
- 从零开始学习jQuery (十) jQueryUI常用功能实战
- jquery ui对话框实例代码