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

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

-(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. 凸优化简介 Convex Optimization Overview

    最近的看的一些内容好多涉及到凸优化,没时间系统看了,简单的了解一下,凸优化的两个基本元素分别是凸函数与凸包 凸集 凸集定义如下: 也就是说在凸集内任取两点,其连线上的所有点仍在凸集之内. 凸函数 凸函 ...

  2. 利用ICSharpCode.SharpZipLib.Zip进行文件压缩

    官网http://www.icsharpcode.net/ 支持文件和字符压缩. 创建全新的压缩包 第一步,创建压缩包 using ICSharpCode.SharpZipLib.Zip; ZipOu ...

  3. 一起学CUDA(一)

    前提是电脑的显卡支持CUDA,N卡一般是支持的,如果是A卡就没办法了.主要针对Windows环境,Linux和Mac也有相应的安装包.CUDA环境搭建:Step1:安装代码环境VS2010:Step2 ...

  4. php.ini中Magic_Quotes_Gpc开关设置

    如果你网站空间的php.ini文件里的magic_quotes_gpc设成了off,那么PHP就不会在敏感字符前加上反斜杠(\\),由于表单提交的内容可能含有敏感字符,如单引号('),就导致了SQL ...

  5. Jquery 设置style:display 通过ID隐藏区域

    $("#id").css('display','none'); $("#id").css('display','block'); 或 $("#id&q ...

  6. [Everyday Mathematics]20150228

    试证: $$\bex \int_0^\infty \sin\sex{x^3+\frac{\pi}{4}}\rd x =\frac{\sqrt{6}+\sqrt{2}}{4}\int_0^\infty ...

  7. YII Framework学习教程-YII的安全

    web应用的安全问题是很重要的,在“黑客”盛行的年代,你的网站可能明天都遭受着攻击,为了从某种程度上防止被攻击,YII提供了防止攻击的几种解决方案.当然这里讲的安全是片面的,但是值得一看. 官方提供的 ...

  8. selenium-grid2 远程并发控制用例执行

    今天闲来无事,随意看了一下selenium,突然注意到grid这个功能以前都是,在读有关selenium的文档时候知道有这么个grid远程控制的功能,但一直没有去试过.所以呢,今天就简单的做了这么个小 ...

  9. 细雨学习笔记:Jmeter上一个请求的结果作为下一个请求的参数--使用正则提取器

    Jmeter接口自动化--使用正则提取器,可以把上一个请求的结果取出来,作为下一个请求的入参

  10. LR录制脚本IE不能打开解决方法

    运行环境:win7 64位 解决方法:1.卸载IE11 2.计算机——属性——高级系统设置——性能里的设置——数据执行保护——选择“为除下列选定程序之外的所有程序和服务启用”——添加IE浏览器和VUG ...