JavaShuo
栏目
标签
iphone开发 用户点击,触摸和手势识别 解析
时间 2019-11-17
标签
iphone
开发
用户
点击
触摸
手势
识别
解析
栏目
iPhone
繁體版
原文
原文链接
用户对屏幕(人机交互)的全部操做均可称为事件。事件包括用户点击,触摸和手势识别等。
一:UIView及UIViewController都继承自UIResponder类,而具备在屏幕上显示功能的类及其控制器类(UIControl)也都继承自UIView,因此他们都时响应者(即全部视图和所由控件都是响应者)。
内容结构图:
二:响应着链:事件是向上传递的(这点相似于java中的异常处理:throw),当当前响应者处理不了事件的时,他会将此事件传递给他的父视图,逐级向更高一层(下一个对象)传递。
若是窗口处理不了当前事件,此事件将会被传递到应用程序的UIApplication实例。若是仍是处理不了,此事件将进入睡眠状态。
响应者链图:
转发事件:从上面能够看到,事件是在逐个对象间的传递,当视图(如:表视图)不包含动做事件(如:轻扫手势)时,可能不会执行传递工做,这样其余对象将没法得到响应,阻止了其余视图的手势识别。这时就须要手动去传递:在下一个响应者上调用相同的方法来传递该对象,
[plain]
view plain
copy
-(void)<span style="background-color: rgb(153, 255, 153);">responder</span>:(UIEvent*)event{
//若是可处理传递事件,执行此句:
[self handleEvent:event ];
//不然手动传递到下一个响应者(递归)
[self.nextResponder <span style="background-color: rgb(153, 255, 153);">responder</span>:event ];
}
三: 4个通知手势的方法:
1.开始触摸:
[plain]
view plain
copy
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
//touches :动做的数量() 每一个对象都是一个UITouch对象,而每个事件均可以理解为一个手指触摸。
//获取任意一个触摸对象
UITouch *touch = [touches anyObject];
//得到触摸对象的点击数量,只捕捉一个触摸对象的点击。
NSInteger numTaps=[touch tapCount];
//得到触摸对象的数量
NSInteger numTouchs=[touches count];
//触摸对象的位置
CGPoint previousPoint = [touch previousLocationInView:self.view];
2.移动:
[plain]
view plain
copy
<pre name="code" class="plain">- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
}</pre>
<pre></pre>
3.离开:
<pre></pre>
[plain]
view plain
copy
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
}
4.取消: 电话呼入等中断手势时,会调用此方法。
[plain]
view plain
copy
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
}
四:手势识别器(UIGestureRecognizer):能够理解成一个容器,里面可添加它的子类(如:捏合(
UI
Pinch
GestureRecognizer
),轻扫(
UI
Swiper
GestureRecognizer
),点击(
UI
Tap
GestureRecognizer
)),这些子类都是封装好的,可响应屏幕上的事件进行处理。固然也可自定义手势识别器的子类,来响应须要的事件,这样显得更灵活些。
1.调用系统的手势识别器 以捏合为例:
[plain]
view plain
copy
- (void)viewDidLoad {
[super viewDidLoad];
//实例化捏合手势识别器,将实例好的手势识别器对象做为实参传递到doPinch:。
UIPinchGestureRecognizer *pinch = [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(doPinch:)] autorelease];
//将实例化捏合手势识别器加入视图识别器中。
[self.view addGestureRecognizer:pinch];
}
- (void)doPinch:(UIPinchGestureRecognizer *)pinch {
//若是开始触发
if (pinch.state == UIGestureRecognizerStateBegan) {
CGFloat initialFontSize = label.font.pointSize;
} else {
//按照捏合比例扩大或缩小
label.font = [label.font fontWithSize:initialFontSize * pinch.scale];
}
}
2.自定义手势识别器:
自定义MyGestureRecognizerView。
MyGestureRecognizerView.h
java
[plain]
view plain
copy
#import <UIKit/UIKit.h>
@interface MyGestureRecognizerView : UIGestureRecognizer
@end
MyGestureRecognizerView.m
[plain]
view plain
copy
#import "MyGestureRecognizerView.h"
@implementation MyGestureRecognizerView
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
//能够填写本身所须要的识别机制。。。。。。
//获取任意一个触摸对象
UITouch *touch = [touches anyObject];
//得到触摸对象的点击数量,只捕捉一个触摸对象的点击。
NSInteger numTaps=[touch tapCount];
//得到触摸对象的数量
NSInteger numTouchs=[touches count];
//触摸对象的位置
CGPoint previousPoint = [touch previousLocationInView:self.view];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
//能够填写本身所须要的识别机制。。。。。。。
}
@end
在主视图控制器中:
HelloViewController.h
[plain]
view plain
copy
#import <UIKit/UIKit.h>
@interface HelloViewController : UIViewController
@end
HelloViewController.m
[plain]
view plain
copy
#import "HelloViewController.h"
#import "MyGestureRecognizerView.h"
@interface HelloViewController ()
@end
@implementation HelloViewController
- (void)viewDidLoad
{
MyGestureRecognizerView *mygest=[[MyGestureRecognizerView alloc] initWithTarget:self action:@selector(doit:)];
[self.view addGestureRecognizer:mygest];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)doit:(MyGestureRecognizerView *)my{
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end
相关文章
1.
iOS14开发-触摸与手势识别
2.
触摸事件,手势识别
3.
Android手势识别ViewFlipper触摸动画
4.
iOS:触摸事件和手势识别的介绍
5.
触摸事件和手势
6.
Android事件处理之多点触摸与手势识别
7.
IOS 触摸事件、手势识别讲解
8.
触摸和点击的冲突以及手势onTouchEvent,performClick,performItemClick ,GestureDetector
9.
iOS开发————触摸与手势
10.
android触摸手势_在Android中检测触摸手势
更多相关文章...
•
ionic 手势事件
-
ionic 教程
•
jQuery Mobile 触摸事件
-
jQuery Mobile 教程
•
PHP开发工具
•
JDK13 GA发布:5大特性解读
相关标签/搜索
触摸屏手势识别
触摸
触击
手写识别
触点
触手
手势
iphone
识别
点击
iPhone
PHP参考手册
XLink 和 XPointer 教程
Spring教程
开发工具
应用
0
分享到微博
分享到微信
分享到QQ
每日一句
每一个你不满意的现在,都有一个你没有努力的曾经。
最新文章
1.
Duang!超快Wi-Fi来袭
2.
机器学习-补充03 神经网络之**函数(Activation Function)
3.
git上开源maven项目部署 多module maven项目(多module maven+redis+tomcat+mysql)后台部署流程学习记录
4.
ecliple-tomcat部署maven项目方式之一
5.
eclipse新导入的项目经常可以看到“XX cannot be resolved to a type”的报错信息
6.
Spark RDD的依赖于DAG的工作原理
7.
VMware安装CentOS-8教程详解
8.
YDOOK:Java 项目 Spring 项目导入基本四大 jar 包 导入依赖,怎样在 IDEA 的项目结构中导入 jar 包 导入依赖
9.
简单方法使得putty(windows10上)可以免密登录树莓派
10.
idea怎么用本地maven
本站公众号
欢迎关注本站公众号,获取更多信息
相关文章
1.
iOS14开发-触摸与手势识别
2.
触摸事件,手势识别
3.
Android手势识别ViewFlipper触摸动画
4.
iOS:触摸事件和手势识别的介绍
5.
触摸事件和手势
6.
Android事件处理之多点触摸与手势识别
7.
IOS 触摸事件、手势识别讲解
8.
触摸和点击的冲突以及手势onTouchEvent,performClick,performItemClick ,GestureDetector
9.
iOS开发————触摸与手势
10.
android触摸手势_在Android中检测触摸手势
>>更多相关文章<<