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.
resiprocate 之repro使用
2.
Ubuntu配置Github並且新建倉庫push代碼,從已有倉庫clone代碼,並且push
3.
設計模式9——模板方法模式
4.
avue crud form組件的快速配置使用方法詳細講解
5.
python基礎B
6.
從零開始···將工程上傳到github
7.
Eclipse插件篇
8.
Oracle網絡服務 獨立監聽的配置
9.
php7 fmp模式
10.
第5章 Linux文件及目錄管理命令基礎
本站公眾號
歡迎關注本站公眾號,獲取更多信息
相關文章
1.
iOS14開發-觸摸與手勢識別
2.
觸摸事件,手勢識別
3.
Android手勢識別ViewFlipper觸摸動畫
4.
iOS:觸摸事件和手勢識別的介紹
5.
觸摸事件和手勢
6.
Android事件處理之多點觸摸與手勢識別
7.
IOS 觸摸事件、手勢識別講解
8.
觸摸和點擊的衝突以及手勢onTouchEvent,performClick,performItemClick ,GestureDetector
9.
iOS開發————觸摸與手勢
10.
android觸摸手勢_在Android中檢測觸摸手勢
>>更多相關文章<<