UIEvent UIResponder UI_04

时间:2022-09-17 20:11:46
1、事件(UIEvent),是由硬件设备捕捉到用户对设备的操作,把这个操作抽象成一个事件对象
 
  ios中三大事件:触Touches摸晃动事件Motion,远程控制事件RemoteControl;其中应有最广泛的是触摸事件
UIView是支持触摸的,由于UIView
内部没有实现跟触摸相关的方法,所以我们再点击UIView创建其子类进行方法实现
2、一般准备做题思路:先在 AppDelegate.m中建立一个 TouchVC对象
TouchViewController *touchVC
= [[TouchViewController alloc]init];

//将touchVC指定self.window的为根视图控制器

self.window.rootViewController =
touchVC;

   
[touchVC release];
然后在 TouchViewController.m中设置颜色
self.view.backgroundColor =
[UIColor yellowColor];
————————————————————————————
以上方法为通常准备方法:
第一类型:点击移动视图
   
 MoveView *moveView = [[MoveView
alloc]initWithFrame:CGRectMake(110, 234, 100,
100)];
 
   moveView.backgroundColor =
[UIColor blackColor];
 
   [self.view
addSubview:moveView];
 
   [moveView
release];
第二类型:捏合视图对象
    PinchView *pinchView
= [[PinchView alloc]initWithFrame:CGRectMake(60, 184, 200, 200)];

pinchView.backgroundColor =
[UIColor orangeColor];

[self.view addSubview:pinchView];

   
[pinchView release];
————————————————————————————
首先总结点击移动视图:

TouchView.m

//如果想让一个视图对象对触摸事件作出反应,需要在这个类中的.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);

    NSLog(@"%s",__FUNCTION__);

TouchView.m

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

{

//1.获取手指对象

UITouch *finger
= [touches anyObject];

    //2.获取手指当前在自身视图的位置
    CGPoint currentLocation
= [finger locationInView:self];
    //3.获取手指之前在视图上的位置
    CGPoint 
previousLocation = [finger previousLocationInView:self];
    //4.计算手指在x轴和y轴上的增量

//手指在x轴上的增量

CGFloat dx
= currentLocation.x -
previousLocation.x;

//手指在y轴上的增量

    CGFloat dy
= currentLocation.y -
previousLocation.y;
    //获取中心点,之前center的位置

CGPoint center
= self.center;

self.center = CGPointMake(center.x +
dx, center.y +
dy);

}

@end
UIEvent UIResponder UI_04


——————————————————————————————————————————
第二种类型捏合视图对象:
首先要把默认的单触点关掉

PinchView.m  继承自UIView

-(instancetype)initWithFrame:(CGRect)frame{

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{

//得到集合中所有手指对象,并使用数组存储(数组有序)

    NSArray *allTouchs
= [touches allObjects];
 
     
//取出两个触点
        UITouch *touch1
= [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];

//求出之前两个点的距离

        CGFloat previousDistance
= [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);

}
UIEvent UIResponder UI_04


================================================================================
响应者链:

AppDelegate.m

//创建 Responder对象

    ResponderViewController *responderVC
= [[ResponderViewController alloc]init];
    //将responderVC指定为根控制器

self.window.rootViewController =
responderVC;

   
[responderVC release];
ResponderViewController.m
1、  UIResponder
响应者类,继承自NSObject,它是一个响应者的基类,它提供了一些处理事件的方法

//什么是响应者(响应者对象):1.继承自UIResponder
2.能对ios事件中(触摸事件,晃动事件,远程事件)做出响应的对象就叫做响应者

//
UILabel ,UIImageView 
默认用户交互是关闭的
响应者链:确定事件作用的对象时,UIAppcation
--->UIAppcationDelegate--->window--->视图控制器--->视图控制器上的子视图
响应事件:(有大到小)视图控制器上的子视图--->视图控制器--->window--->UIAppcationDelegate---UIAppcation 
如果都不处理这个事件,事件就会被丢弃

ResponderView.m

   ResponderView *redView
= [[ResponderView alloc]initWithFrame:[UIScreenmainScreen].bounds];

redView.tag = 101;

redView.userInteractionEnabled = NO;

redView.backgroundColor =
[UIColor redColor];

[self.view addSubview:redView];

[redView release];

 
    ResponderView *yellowView
= [[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];
}
ResponderView.m
- (void)touchesBegan:(NSSet *)touches
withEvent:(UIEvent *)event{
    switch (self.tag)
{

        case 101:

NSLog(@"红色视图");

break;

case 102:

NSLog(@"黄色视图");

break;

case 103:

NSLog(@"绿色视图");

break;

case 104:

NSLog(@"蓝色视图");

break;

default:

break;

   
}
==============================================
欢迎学习本文,未经博主许可,禁止转载!

UIEvent UIResponder UI_04的更多相关文章

  1. iOS学习笔记——触控与手势

    触控 此部分内容已学良久,恨记之甚晚,忙矣,懒矣!本文简而记焉,恐日后忘也. 在iOS的触控事件中,有触控.事件以及响应者这三个角色,一个触摸则代表了一只手指和屏幕接触这个动作所包含的信息:而事件则包 ...

  2. iOS-响应链(Responder Chain)

    2017.05.08 20:40* 字数 1306 阅读 740评论 6喜欢 9 工作接近一年,很久没有更新博客.工作中学到很多知识点后面将花时间整理,作为对一年知识学习的总结: 下面是本篇博客的写作 ...

  3. iOS开发 - 事件传递响应链

    序言 当我们在使用微信等工具,点击扫一扫,就能打开二维码扫描视图.在我们点击屏幕的时候,iphone OS获取到了用户进行了“单击”这一行为,操作系统把包含这些点击事件的信息包装成UITouch和UI ...

  4. iOS - UIEvent事件及UIResponder响应者

    在iOS中不是所有的对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事件,称之为响应者对象: UIApplication.UIViewController.UIView都继承自U ...

  5. UIResponder NSSet UITouch UIEvent

    UIResponder: UIView的超类,用来响应handle(触屏.motion.响应者等)事件. NSSet:一系列的类集合(类似数组). UITouch:一个点击类.负责:点击的view,w ...

  6. iOS 触摸事件与UIResponder(内容根据iOS编程编写)

    触摸事件 因为 UIView 是 UIResponder 的子类,所以覆盖以下四个方法就可以处理四种不同的触摸事件: 1.  一根手指或多根手指触摸屏幕 - (void)touchesBegan:(N ...

  7. iOS 事件处理之UIResponder简介

    在用户使用app过程中,会产生各种各样的事件 iOS中的事件可以分为3大类型:触摸事件.加速计事件.远程控制事件 在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处 ...

  8. iOS控件之UIResponder类

    iOS控件之UIResponder类 在iOS中UIResponder类是专门用来响应用户的操作处理各种事件的,我们知道UIApplication.UIView.UIViewController这几个 ...

  9. 你真的了解UIEvent、UITouch吗?

    一:首先查看一下关于UIEvent的定义 //事件类型 typedef NS_ENUM(NSInteger, UIEventType) { UIEventTypeTouches, UIEventTyp ...

随机推荐

  1. JS和jQuery中的事件总结(一)

    学而时习之,小白现在天天写页面,基础知识还是要恶补的. 进入正题,什么是事件(此处单独对jQuery.JS)?就是JS和Html之间的交互时呢,用户和浏览器操作页面时的动作(其实是为引发的效果的执行操 ...

  2. shell脚本集合

    慢慢学习,慢慢记吧 第一个shell脚本,创建用户,默认密码用户名,使得用户第一次登陆强制修改密码的脚本 #/bin/bash #创建用户,指定初始密码用户名,第一次登陆后强制修改用户名 userad ...

  3. LoadRunner执行过程报错“Failed to connect to server "xxx.xxx.xxx.xxx:xx":[10060] connetion time out”

    执行性能测试过程中,LR报错: Action.c(6):Error -27796: Failed to connect to server "xxx.xxx.xxx.xxx:xx" ...

  4. 将文件放到Android模拟器的SD卡中的两种解决方法

    两种方式:一.窗口界面操作1.打开DDMS页面2.打开File Explorer页,如果没有,在Window --> Show View -->File Explorer3.一般就在mnt ...

  5. 据序和中序序列或者也许为了一个二进制序列,恢复二进制和打印图像(c语言)

    首先要预购和序,以恢复它: 1.首先,我们使用的是递归的方式来完成 2.递归的最小单位:一个空的树和书的前言和第一序.该序列的第一个元素是树的第一序列根,调用这种方法 3.递归的终止条件是.当这棵树的 ...

  6. VMware 下快速克隆出多个 Linux 环境

    念念不忘,必有回响 好好工作,好好吃饭,困了倒头就睡:吃你认为好吃的,吃到饱:买贵的,你想买的:去玩去野: 就这样. 为什么要克隆多个 Linux 系统? 因为要玩阿. 其实也不是了,就是为了折腾嘛, ...

  7. tomcat 监控脚本

    ps -ef |grep tomcat |grep -w 'atlassian'|grep -v 'grep'|awk '{print $2}' curl -s -o /dev/null -m 10 ...

  8. WebAPI调用笔记

    前言 即时通信项目中初次调用OA接口遇到了一些问题,因为本人从业后几乎一直做CS端项目,一个简单的WebAPI调用居然浪费了不少时间,特此记录. 接口描述 首先说明一下,基于Http协议的Get.Po ...

  9. db2 v11 安装测试

    一.准备环境: 修改/etc/hosts如下配置: #vi /etc/hosts 127.0.0.1    localhost 修改系统内核参数 # vi /etc/sysctl.conf kerne ...

  10. python入门知识点(上)

    1.硬件系统: 主机部分: 1.*处理器(CPU): 电脑的大脑 运算器: 数值计算和逻辑判断 控制器: 可以电脑中的各个部件协同工作 2.内部存储器: 随机存储器:内存条 使用电信号表示数据; 特 ...