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 ...
随机推荐
- 一起撸个简单粗暴的Tv应用主界面的网格布局控件(上)
这一篇是真的隔了好久了~~,也终于可以喘口气来好好写博客了,这段时间实在是忙不过来了,迭代太紧.好,废话不多说,进入今天的主题. 效果 图一是Tv应用:当贝市场的主页 图二是咱自己撸的简单粗暴的 Tv ...
- Appium--入门demo
Appium环境搭建已经在在博客中写出 http://www.cnblogs.com/feimaoyuzhubaobao/p/5057832.html 那么本篇博客主要介绍java版本的appiu ...
- Sencha EXTJS6的 Eclipse 插件安装指南
Sencha EXTJS的 Eclipse 插件安装指南 (翻译:苏生米沿) 本文地址:http://blog.csdn.net/sushengmiyan/article/details/52566 ...
- 解决HTML外部引用CSS文件不生效问题
作为一个前端小白,鼓捣了几天前端..今天突然发现我深信不疑的东西,竟然出现了问题..就比如我在css目录下面写了一个css样式文档:style.css.这时里面只有一句话: body { backgr ...
- 递归dict
一个看起来非常酷的定义 class Example(dict): def __getitem__(self, item): try: return dict.__getitem__(self, ite ...
- Core Python Programming一书中关于深浅拷贝的错误
该书关于深浅拷贝的论述: 6.20. *Copying Python Objects and Shallow and Deep Copies "when shallow copies are ...
- Windows 为右键菜单瘦身
当你想删除右键菜单中某些选项时,一种比较合适的思路是: 1.如果软件本身提供了控制选项,那么直接在该软件设置即可.没必要在注册表操作.比如360安全卫士和360杀毒都提供了这种机制. 值得一提的是,3 ...
- [安全]Back_Track_5 vm 版安装和使用
下载安装 下载使用国内的镜像 http://mirrors.ustc.edu.cn/kali-images/kali-1.0.9/ 我这里是vm9.0 下载之后解压,然后打开vm,然后 文件--&g ...
- Effective C++ ——实现
条款26:尽可能延后变量定义式的出现时间 当你定义一个变量的时候就要保证这个变量能够在程序中使用到,不要定义无意义的变量,这样就要求我们最好是在变量使用到的时候才做定义,因为如果一个变量定义了却不使用 ...
- Dynamics CRM2016 新功能之Solution enhancements
CRM2016中对解决方案的功能有了一定的加强,CRM自2011版本开始引入了solution的概念,但大家的共识是solution的导出导入以及发布都非常的慢,常常会出现发布超时的情况很是头疼. 以 ...