UIEvent UIResponder UI_04
ios中三大事件:触Touches摸晃动事件Motion,远程控制事件RemoteControl;其中应有最广泛的是触摸事件
内部没有实现跟触摸相关的方法,所以我们再点击UIView创建其子类进行方法实现
= [[TouchViewController alloc]init];
//将touchVC指定self.window的为根视图控制器
self.window.rootViewController =
touchVC;
[touchVC release];
[UIColor yellowColor];
MoveView *moveView = [[MoveView
alloc]initWithFrame:CGRectMake(110, 234, 100,
100)];
moveView.backgroundColor =
[UIColor blackColor];
[self.view
addSubview:moveView];
[moveView
release];
= [[PinchView alloc]initWithFrame:CGRectMake(60, 184, 200, 200)];
pinchView.backgroundColor =
[UIColor orangeColor];
[self.view addSubview:pinchView];
[pinchView release];
TouchView.m
- (void)touchesMoved:(NSSet *)touches
withEvent:(UIEvent *)event{
//
self.backgroundColor = [UIColor randomColor];
self.center = CGPointMake(arc4random_uniform(220 - 100 + 1)
+ 100, arc4random_uniform(468 - 100 +1)
+100);
//取出手指对像
UITouch *f
= [touches anyObject];
//当前手指的
位置
CGPoint location
= [f locationInView:self];
//之前手指位置
CGPoint previousLocation
= [f previousLocationInView:self];
CGPoint center
= self.center;
CGFloat dx
= location.x -
previousLocation.x;
CGFloat dy
= location.y -
previousLocation.y;
self.center = CGPointMake(center.x +
dx, center.y +
dy);
TouchView.m
(void)touchesMoved:(NSSet *)touches
withEvent:(UIEvent *)event
{
//1.获取手指对象
UITouch *finger
= [touches anyObject];
= [finger locationInView:self];
previousLocation = [finger previousLocationInView:self];
//手指在x轴上的增量
CGFloat dx
= currentLocation.x -
previousLocation.x;
//手指在y轴上的增量
= currentLocation.y -
previousLocation.y;
CGPoint center
= self.center;
self.center = CGPointMake(center.x +
dx, center.y +
dy);
}

PinchView.m 继承自UIView
if (self =
[super initWithFrame:frame])
{
//ios支持多点触摸,只是默认是单点触摸
self.multipleTouchEnabled = YES;
}
return self;
(void)touchesMoved:(NSSet *)touches
withEvent:(UIEvent *)event{
//如果集合中touch集合中手指对象个数为1,就直接返回touchsMoved方法
if (1 ==
touches.count)
{
return;//结束方法
}else{
//得到集合中所有手指对象,并使用数组存储(数组有序)
= [touches allObjects];
//取出两个触点
= [allTouchs firstObject];
UITouch *touch2
= [allTouchs lastObject];
//求出两个手指对象当前的位置
CGPoint
firstTouch1 = [touch1 locationInView:self];
CGPoint secondTouch2
= [touch2 locationInView: self];
//求出当前两个手指的间距
CGFloat currentDistance
= [self distanceFromeFirstPoint:firstTouch1 secondPoint:secondTouch2];
//求出之前两个手指的位置
CGPoint previousFirstPoint
= [touch1 previousLocationInView:self];
CGPoint previousSecondPoint
= [touch2 previousLocationInView:self];
//求出之前两个点的距离
= [self distanceFromeFirstPoint:previousFirstPoint secondPoint:previousSecondPoint];
CGFloat rate
= currentDistance / previousDistance;
//缩放视图,如果视图的大小发生变化时,而中心点的位置不发生变化,修改bounds就可以了
, 0, self.bounds.size.width *
rate, self.bounds.size.height *
rate);
}
}
//封装一个计算距离的方法(sqrt求开方)
- (CGFloat)distanceFromeFirstPoint
: (CGPoint)firstPoint
secondPoint : (CGPoint)secondPoint{
CGFloat dx
= firstPoint.x -
secondPoint.x;
CGFloat dy
= firstPoint.y -
secondPoint.y;
//当前两点距离
return sqrt(dx
* dx + dy * dy);

AppDelegate.m
//创建 Responder对象
= [[ResponderViewController alloc]init];
self.window.rootViewController =
responderVC;
[responderVC release];
响应者类,继承自NSObject,它是一个响应者的基类,它提供了一些处理事件的方法
//什么是响应者(响应者对象):1.继承自UIResponder
2.能对ios事件中(触摸事件,晃动事件,远程事件)做出响应的对象就叫做响应者
UILabel ,UIImageView
默认用户交互是关闭的
--->UIAppcationDelegate--->window--->视图控制器--->视图控制器上的子视图
如果都不处理这个事件,事件就会被丢弃
ResponderView.m
= [[ResponderView alloc]initWithFrame:[UIScreenmainScreen].bounds];
redView.tag = 101;
redView.userInteractionEnabled = NO;
redView.backgroundColor =
[UIColor redColor];
[self.view addSubview:redView];
[redView release];
= [[ResponderView alloc]initWithFrame:CGRectMake(30, 360, 320, 284)];
yellowView.tag = 102;
//关闭用户的交互造成响应者链就到这个位置断开了,事件只能找他的上一级处理,如果上一级都不处理,此事件就不了了之了
yellowView.userInteractionEnabled = YES;
yellowView.backgroundColor =
[UIColor yellowColor];
[redView addSubview:yellowView];
[yellowView release];
ResponderView *greenView
= [[ResponderView alloc]initWithFrame:CGRectMake(20, 20, 280, 244)];
greenView.tag = 103;
greenView.backgroundColor =
[UIColor greenColor];
[yellowView addSubview:greenView];
[greenView release];
ResponderView *blueView
= [[ResponderView alloc]initWithFrame:CGRectMake(20, 20, 240, 204)];
blueView.tag = 104;
blueView.backgroundColor =
[UIColor blueColor];
[greenView addSubview:blueView];
[blueView release];
withEvent:(UIEvent *)event{
{
case 101:
NSLog(@"红色视图");
break;
case 102:
NSLog(@"黄色视图");
break;
case 103:
NSLog(@"绿色视图");
break;
case 104:
NSLog(@"蓝色视图");
break;
default:
break;
}
UIEvent UIResponder UI_04的更多相关文章
- iOS学习笔记——触控与手势
触控 此部分内容已学良久,恨记之甚晚,忙矣,懒矣!本文简而记焉,恐日后忘也. 在iOS的触控事件中,有触控.事件以及响应者这三个角色,一个触摸则代表了一只手指和屏幕接触这个动作所包含的信息:而事件则包 ...
- iOS-响应链(Responder Chain)
2017.05.08 20:40* 字数 1306 阅读 740评论 6喜欢 9 工作接近一年,很久没有更新博客.工作中学到很多知识点后面将花时间整理,作为对一年知识学习的总结: 下面是本篇博客的写作 ...
- iOS开发 - 事件传递响应链
序言 当我们在使用微信等工具,点击扫一扫,就能打开二维码扫描视图.在我们点击屏幕的时候,iphone OS获取到了用户进行了“单击”这一行为,操作系统把包含这些点击事件的信息包装成UITouch和UI ...
- iOS - UIEvent事件及UIResponder响应者
在iOS中不是所有的对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事件,称之为响应者对象: UIApplication.UIViewController.UIView都继承自U ...
- UIResponder NSSet UITouch UIEvent
UIResponder: UIView的超类,用来响应handle(触屏.motion.响应者等)事件. NSSet:一系列的类集合(类似数组). UITouch:一个点击类.负责:点击的view,w ...
- iOS 触摸事件与UIResponder(内容根据iOS编程编写)
触摸事件 因为 UIView 是 UIResponder 的子类,所以覆盖以下四个方法就可以处理四种不同的触摸事件: 1. 一根手指或多根手指触摸屏幕 - (void)touchesBegan:(N ...
- iOS 事件处理之UIResponder简介
在用户使用app过程中,会产生各种各样的事件 iOS中的事件可以分为3大类型:触摸事件.加速计事件.远程控制事件 在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处 ...
- iOS控件之UIResponder类
iOS控件之UIResponder类 在iOS中UIResponder类是专门用来响应用户的操作处理各种事件的,我们知道UIApplication.UIView.UIViewController这几个 ...
- 你真的了解UIEvent、UITouch吗?
一:首先查看一下关于UIEvent的定义 //事件类型 typedef NS_ENUM(NSInteger, UIEventType) { UIEventTypeTouches, UIEventTyp ...
随机推荐
- RandomAccessFile&IO流&排序&方法论
RandomAccessFile&IO流&排序&方法论 我们总觉得历史是极其遥远的东西,与我们并无关联,又觉得历史隐藏在图书馆的旧书之中. 然而,我们每个人都有真真切切的历史. ...
- scratch写的图灵机
大多数人对于scratch不感冒,因为觉得这是孩子玩的.的确,积木的方式不适合专业程序员写代码,然而别小看scratch,怎么说,它也是图灵完备的.而且,过程支持递归,虽然带不了返回值. 虽然计算速度 ...
- <心得小记>2015年10月3日 14:16:42
急事,慢慢说:大事,清楚的说: 小事,幽默的说了:没把握的事,谨慎的说: 没发生的事,不要胡说:做不到的事,别乱说: 伤害人的事,不能说:讨厌的事,对事不对人的说: 开心的事,看场合说:伤心的事,不要 ...
- Node.js 进程
process 是全局对象,能够在任意位置访问,是 EventEmitter 的实例. 退出状态码 当没有新的异步的操作等待处理时,Node 正常情况下退出时会返回状态码 0 .下面的状态码表示其他状 ...
- ViewPager滑动后,可移动的Imageview会回到初始化的位置
知乎看到的原文http://www.zhihu.com/question/37398770?sort=created ViewPager滑动后,可移动的Imageview会回到初始化的位置? < ...
- iOS中的NSURLProtocol
转自:iOS知识小集 NSURLProtocol类(注意,这个不是协议)经常用于实现一些URL Loading System相关的黑魔法.它可以拦截URL Loading System相关的网络请求, ...
- Ubuntu安装telent服务器时出现:apt-get:Package has no installation
当我在终端敲下这条命令的时候,系统就提示telnetd:apt-get:Package has no installation sudo apt-get install xinetd telnetd ...
- 用类模拟C风格的赋值+返回值
这个方法比较好: class DataHolder: def __init__(self, value=None): self.value = value def set(self, value): ...
- C++ 虚函数表 多重继承
上次研究的是单继承的情况,这次研究多重继承下的虚函数表的排列情况. 这次A,A1,A2,B这几个类的继承关系如下图: 测试代码如下: #include<iostream> using na ...
- 一个maven项目打多个可执行Jar文件
使用maven-jar-plugin插件可以将一个maven项目按照需求打出多个可执行的jar文件. pom关键配置如下所示: <plugin> <groupId>org.ap ...