事件分类:晃动、触摸、远程控制(如遥控器、红外控制)

触摸开始时候的方法(判断单击,双击,三击事件可以写在这里)

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{}

//触摸开始时候的方法 ()
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"触摸开始");
//点击的时候随机改变背景颜色
// [self changeSelColor];
//touches 存储触摸屏幕时的手指对象
//UITouch 表示是一个手指类,每一个手指对象都是一个 UITouch 类型的对象
//1.取出手指对象
UITouch * aTouch = [touches anyObject];
//2.根据点击的次数来进行相应的处理
if (aTouch.tapCount == ) {
NSLog(@"");//这里的永远不会打印出来,因为,触摸必定会点击到屏幕
}
if ( aTouch.tapCount == ) {
// [self changeSelColor];
//让单击操作延迟。看是否有双击事件操作
[self performSelector:@selector(changeSelColor) withObject:nil afterDelay:0.3];
}
if (aTouch.tapCount == ) {
[self changFatherColor];
//双击操作的时候,取消改变自身颜色的操作
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(changeSelColor) object:nil];
}
if (aTouch.tapCount == ) {
[self changSelfStation];//点击3次,改变自身的位置
}
}

我们的双击,三击,单击事件 demo

手指还没有离开屏幕时候(在屏幕上移动手指)触发

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

触发结束时候,手指离开屏幕的那一时刻触发

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{}

触摸中断时候要触发的方法

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{}

随机改变颜色(一般写在一个方法的分类里,方便调用)

[UIColor  colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];

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

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

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

我们视图上的Button 我们的程序怎么知道我们点击了?每一个程序都是一个 UIApplication 对象

yellowView.userInteractionEnabled = NO;黄色视图的用户交互属性被关闭。

当我们查找的时候,是一层一层去查找,当我们去响应的时候,是从响应的地方开始,往上提交

代码demo


//
// AppDelegate.m #import "AppDelegate.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
RootViewController * rootView = [[RootViewController alloc]init];
self.window.rootViewController = rootView;
[rootView release]; return YES;
}
-(void)dealloc{
[self release];
[super dealloc];
}

mian.m

#import <UIKit/UIKit.h> @interface PinchView : UIView @end

PinchView.h

//
// PinchView.m #import "PinchView.h" //捏合效果(在 touchesMoved 里面实现)
@implementation PinchView
//IOS 支出多点触控,只不过默认的是开启单点触控
//重写初始化方法,修改为支持多点触控
-(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.multipleTouchEnabled = YES;//支持多点触摸
}
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ }
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
//一旦发现是用一个手指在操作的话,则不进行下面的捏合操作
if (touches.count == ) {
return ;//结束下面的代码 (return 后面的代码永远不会被编译到)
}
//1.获取触摸屏的两个手指对象
NSArray * allTouches = [touches allObjects];
UITouch * firstTouch = [allTouches firstObject];//取出第一个手指对象
UITouch * secondTouch = [allTouches lastObject];//取出第二个手指对象
//2.获取捏合后手指的位置
CGPoint currentPoint1 = [firstTouch locationInView:self];
CGPoint currentPoint2 = [secondTouch locationInView:self];
//3.获取手指捏合后的距离
CGFloat currentDistance = [self destanceBetweenPoint1:currentPoint1 Ponit2:currentPoint2];
//4.获取捏合之前的手指的位置
CGPoint previousPoint1 = [firstTouch previousLocationInView:self];
CGPoint previousPoint2 = [secondTouch previousLocationInView:self];
//5.获取捏合之前的两手指之间的距离
CGFloat previousDistance = [self destanceBetweenPoint1:previousPoint1 Ponit2:previousPoint2];
//6.缩放比 (捏合后和捏合前的比例)
CGFloat scale = currentDistance/previousDistance;
//7.修改视图的大小 修改 bounds 保持中心点不变
self.bounds = CGRectMake(, , self.frame.size.width*scale, self.frame.size.height*scale); /*
CGFloat ph1 = currentPoint1.y - currentPoint2.y;
//捏合前的两手指位置
CGPoint previousLocation1 = [firstTouch previousLocationInView:self];
CGPoint previousLocation2 = [secondTouch previousLocationInView:self];
//捏合前的两手指距离
CGFloat ph2 = previousLocation1.y - previousLocation2.y;
//计算比例
CGFloat change = ph1/ph2;
//得到缩放后的效果
self.center = self.center;
CGFloat w = self.frame.size.width * change;
CGFloat h = self.frame.size.height * change;
*/ }
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{ }
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ } //利用勾股定理 计算两个点之间的距离
-(CGFloat)destanceBetweenPoint1:(CGPoint )point1 Ponit2:(CGPoint )point2{
CGFloat dx = point1.x - point2.x;
CGFloat dy = point1.y - point2.y;
return sqrt(dx*dx + dy*dy);
}
@end

PinchView.m

//
// TouchView.h #import <UIKit/UIKit.h>
#import "UIColor+Addition.h"
#import "PanView.h" @interface TouchView : UIView -(void)changeSelColor;
-(void)changFatherColor;
-(void)changSelfStation;
@end

TouchView.h

//
// TouchView.m #import "TouchView.h" @implementation TouchView //对于 UIView 类的视图可以接受触摸事件,但是让他响应,必须实现以下的几个方法 //单击的时候改变自己的颜色,双击的时候改变父视图的颜色 //触摸开始时候的方法 ()
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"触摸开始");
//点击的时候随机改变背景颜色
// [self changeSelColor];
//touches 存储触摸屏幕时的手指对象
//UITouch 表示是一个手指类,每一个手指对象都是一个 UITouch 类型的对象
//1.取出手指对象
UITouch * aTouch = [touches anyObject];
//2.根据点击的次数来进行相应的处理
if (aTouch.tapCount == ) {
NSLog(@"");//这里的永远不会打印出来,因为,触摸必定会点击到屏幕
}
if ( aTouch.tapCount == ) {
// [self changeSelColor];
//让单击操作延迟。看是否有双击事件操作
[self performSelector:@selector(changeSelColor) withObject:nil afterDelay:0.3];
}
if (aTouch.tapCount == ) {
[self changFatherColor];
//双击操作的时候,取消改变自身颜色的操作
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(changeSelColor) object:nil];
}
if (aTouch.tapCount == ) {
[self changSelfStation];//点击3次,改变自身的位置
}
}
//手指还没有离开屏幕 (手指移动的时候)
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
//手指移动的时候,改变父视图的颜色
[self changFatherColor];
}
//触摸中断的时候触发的事件 (例如,手机有电话了)
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
self.backgroundColor = [UIColor blackColor];
}
//触摸结束的时候触发的事件 (手指离开屏幕的时候)
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
//x 在 100-300 y 在 100 - 500
// [self changSelfStation];
} //改变自身视图的颜色
-(void)changeSelColor{
self.backgroundColor = [UIColor randomColor];
}
//改变父视图的颜色
-(void)changFatherColor{
self.superview.backgroundColor = [UIColor randomColor];
}
//改变自身的位置
-(void)changSelfStation{
self.center = CGPointMake(arc4random()%((-+)+)*1.0, arc4random()%((-+)+)*1.0);
} @end

TouchView.m

#import <UIKit/UIKit.h> @interface PanView : UIView @end

PanView.h

#import "PanView.h"

@implementation PanView

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
//1.获取触摸屏幕的手指对象
UITouch * aTouch = [touches anyObject];
//2.获取手指所在点移动之前的位置和移动之后的位置
CGPoint currentLocation = [aTouch locationInView:self];//当前点位置,移动之后
CGPoint previousLocation = [aTouch previousLocationInView:self];//原来点位置,移动之前的点位置
//3.计算改变量
CGFloat dx = currentLocation.x - previousLocation.x;
CGFloat dy = currentLocation.y - previousLocation.y;
//4.移动之后视图所在的位置(中心点)
self.center = CGPointMake(self.center.x + dx, self.center.y + dy); }
@end

PanView.m

#import <UIKit/UIKit.h>

@interface UIColor (Addition)
//声明类方法的随机色
+(UIColor * )randomColor;
@end

UIColor+Addition.h

#import "UIColor+Addition.h"

@implementation UIColor (Addition)

+(UIColor * )randomColor{
// return [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];
return [self colorWithRed:arc4random()%/255.0 green:arc4random()%/255.0 blue:arc4random()%/255.0 alpha:1.0];
} @end
#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController

@end

RootViewController.h

//
// RootViewController.m #import "RootViewController.h"
#import "TouchView.h"
#import "PanView.h"
#import "PinchView.h" @interface RootViewController () @end @implementation RootViewController - (void)viewDidLoad {
[super viewDidLoad];
//视图类都可以接触到触摸的事件
self.view.backgroundColor = [UIColor grayColor];
TouchView * redView = [[TouchView alloc]initWithFrame:CGRectMake(, , , )];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];
[redView release]; PanView * yellowView = [[PanView alloc]initWithFrame:CGRectMake(, , , )];
yellowView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:yellowView];
[yellowView release]; PinchView * greenView = [[PinchView alloc]initWithFrame:CGRectMake(, , , )];
greenView.backgroundColor = [UIColor greenColor];
[self.view addSubview:greenView];
[greenView release]; } - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} /*
#pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/ @end

UI:触摸事件 与 事件的回应的更多相关文章

  1. NGUI之UICamera控制触摸,鼠标事件

    http://blog.csdn.net/onerain88/article/details/18963539 . UICamera 功能介绍 主要包括UI事件的监听,分发,覆盖范围为此Camera渲 ...

  2. iOS事件:触摸事件.运动事件.远程控制事件

    iOS中,提供了事件处理:触摸事件,运动事件,远程控制事件.这很大得方便程序猿的工作. 这里先简单做个介绍: // // ViewController.m // demo // // Created ...

  3. win10 支持默认把触摸提升鼠标事件 打开 Pointer 消息

    原文:win10 支持默认把触摸提升鼠标事件 打开 Pointer 消息 在 WPF 经常需要重写一套触摸事件,没有UWP的Pointer那么好用. 如果一直都觉得 WPF 的触摸做的不好,或想解决 ...

  4. iOS10 UI教程层次结构的事件

    iOS10 UI教程层次结构的事件 iOS10 UI教程层次结构的事件,层次结构中存在7个事件,对于这些事件的介绍如表1-3所示.通过这些事件,可以监听视图,当视图在层次结构上发生变化时可以被拦截,也 ...

  5. JavaScript触摸与手势事件

    JavaScript触摸与手势事件 发表于 2012-12-10 由 admin iOS版Safari为了向开发人员传达一些特殊信息,新增了一些专有事件.因为iOS设备既没有鼠标也没有键盘,所以在为移 ...

  6. js监听事件 上滑消失下滑出现的效果 触摸与手势事件

    https://www.w3cmm.com/javascript/touch.html //触摸与手势事件连接tinyscrollbar //方法1var _this = $('#fabu');var ...

  7. js实现Mac触摸板双指事件(上、下、左、右、放大、缩小)

    前言 这几天在修复一个web问题时,需要捕获Mac触摸板双指事件(上.下.左.右.放大.缩小),但发现并没有现成的轮子,还是要自己造. 例如:jquery.mousewheel.js(添加跨浏览器的鼠 ...

  8. kendo ui grid选中行事件,获取combobox选择的值

    背景: 以前用 telerik ui做的grid现在又要换成kendo ui,不过说句实话kendo ui真的比telerik好多,可以说超级升级改头换面.当然用的mvc的辅助方法,以前的teleri ...

  9. 横向滑动的listview和其中用到的触摸监听事件详解

    一.首先把横向的listview的代码放上来 HorizontalListView: package com.common.cklibrary.utils.myview; import java.ut ...

  10. Javascript高级编程学习笔记(69)—— 事件(13)触摸与手势事件

    触摸与手势事件 由于移动设备既没有鼠标也没有键盘,所以在为移动浏览器开发交互性网页时,常规的鼠标键盘事件根本不够用 所以早期的苹果为Safari 添加了一些与触摸相关的事件 随着后面Android的W ...

随机推荐

  1. 【英语】Bingo口语笔记(58) - blow系列

  2. 【英语】Bingo口语笔记(79) - fish系列

  3. JS一般般的网页重构可以使用Node.js做些什么(转)

    一.非计算机背景前端如何快速了解Node.js? 做前端的应该都听过Node.js,偏开发背景的童鞋应该都玩过. 对于一些没有计算机背景的,工作内容以静态页面呈现为主的前端,可能并未把玩过Node.j ...

  4. [转] VS 整合NUnit进行单元测试

    Jeff Wong原文 5分钟实现VS2010整合NUnit进行单元测试 1.下载安装NUnit(最新win版本为NUnit-2.6.0.12051.msi) http://www.nunit.org ...

  5. [Papers]MHD, $\p_3\pi$, Lebesgue space [Jia-Zhou, JMAA, 2012]

    $$\bex \p_3\pi\in L^p(0,T;L^q(\bbR^3)),\quad \frac{2}{p}+\frac{3}{q}=2,\quad 3\leq q\leq \infty. \ee ...

  6. 职业操盘手内部教材 z

    重 点抢筹区:   是主力机构在低位拉高建仓后的一个相当尴尬的区域!因为在这个区域,场外的绝大多数投资者不敢买,而场内持有的人却很想卖!所以会出现成片的卖盘挂单! 由于主力向上做的意图已经非常明显,所 ...

  7. 怎样为EXCEL2010添加下拉列表

    注意,下面是Excel2010的步骤和截图,其他版本的Excel类似.   首先用鼠标左键点击你要添加下拉列表的单元格. 如果你只想部分区域有下拉列表,也可以选择部分区域. 下面图片是选择的整个列都是 ...

  8. FreeMarker笔记 第二章 数值和类型

    2.1 基本内容 2.1.1 简介 2.1.2 什么是数值 和程序语言中的数值类型是相似的. 2.1.3 什么是类型? 2.1.4 数据模型是哈希表 2.2 类型 2.2.1 简介 2.2.2 标量 ...

  9. Python在centos下的安装

    1.wget http://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz默认下载到主目录下 2.tar xzf Python-2.6.6.tgz 3 ...

  10. WS之cxf与spring整合1

    1.在web.xml中加入CXFServlet: <!-- 下面表示所有来自/cxfservice/*的请求,都交给 CXFServlet来处理 .--> <servlet>  ...