iOS開發(fā)之手勢(shì)gesture詳解
前言
在iOS中,你可以使用系統(tǒng)內(nèi)置的手勢(shì)識(shí)別(GestureRecognizer),也可以創(chuàng)建自己的手勢(shì).GestureRecognizer將低級(jí)別的轉(zhuǎn)換為高級(jí)別的執(zhí)行行為,是你綁定到view的對(duì)象,當(dāng)發(fā)生手勢(shì),綁定到的view對(duì)象會(huì)響應(yīng),它確定這個(gè)動(dòng)作是否對(duì)應(yīng)一個(gè)特定的手勢(shì)(swipe,pinch,pan,rotation).如果它能識(shí)別這個(gè)手勢(shì),那么就會(huì)向綁定它的view發(fā)送消息,如下圖

UIKit框架提供了一些預(yù)定義的GestureRecognizer.包含下列手勢(shì)
- UITapGestureRecognizer敲擊手勢(shì)(單擊和雙擊)
- UIPanGestureRecognizer(拖動(dòng)手勢(shì))
- UIPinchGestureRecognizer(縮放手勢(shì))
- UISwipeGestureRecognizer(擦碰手勢(shì))
- UIRotationGestureRecognizer(旋轉(zhuǎn)手勢(shì))
- UILongPressGestureRecognizer(長(zhǎng)按手勢(shì))
如果你想讓你的應(yīng)用程序來識(shí)別一個(gè)獨(dú)特的手勢(shì),如選擇目錄或糾結(jié)的運(yùn)動(dòng),你可以創(chuàng)建自己的自定義GestureRecognizer,將在下篇介紹
將特定的手勢(shì)和view相關(guān)聯(lián)
每一個(gè)特定的手勢(shì)必須關(guān)聯(lián)到view對(duì)象中才會(huì)有作用,一個(gè)view對(duì)象可以關(guān)聯(lián)多個(gè)不同的特定手勢(shì),但是每一個(gè)特定的手勢(shì)只能與一個(gè)view相關(guān)聯(lián)。當(dāng)用戶觸摸了view,這個(gè)GestureRecognizer就會(huì)接受到消息,它可以響應(yīng)特定的觸摸事件。

與特定view關(guān)聯(lián)
- 創(chuàng)建GestureRecognizer實(shí)例
- addGestureRecognizer
- 實(shí)現(xiàn)處理手勢(shì)的方法
可以使用removeGestureRecognizer: 來移除手勢(shì)。
_panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlerPanGesture:)];
_panGestureRecognizer.delegate = self;
_panGestureRecognizer.maximumNumberOfTouches = 2;
_panGestureRecognizer.minimumNumberOfTouches = 2;
[self.view addGestureRecognizer:_panGestureRecognizer];
- (void)handlerPanGesture:(UIPanGestureRecognizer *)recognizer
{
if ((recognizer.state == UIGestureRecognizerStateBegan) ||
(recognizer.state == UIGestureRecognizerStateChanged))
{
CGPoint offset = [recognizer translationInView:self.view];
CGRect frame = self.rightViewController.view.frame;
frame.origin.x += offset.x;
if (frame.origin.x >= 0 && frame.origin.x <= kScreenWidth)
{
self.rightViewController.view.frame = frame;
}
[recognizer setTranslation:CGPointZero inView:self.view];
}
else if (recognizer.state == UIGestureRecognizerStateEnded)
{
BOOL isVisible = self.rightViewController.view.frame.origin.x < kScreenWidth / 2;
[self showRightView:isVisible];
}
}
手勢(shì)識(shí)別狀態(tài)
Gesture recognizers從一個(gè)狀態(tài)轉(zhuǎn)到另一狀態(tài)(state)。對(duì)于每個(gè)狀態(tài),根據(jù)它們是否符合特定條件來決定時(shí)候可以移動(dòng)到下一個(gè)狀態(tài)。它們分析多點(diǎn)觸摸。是否識(shí)別失敗。未能識(shí)別手勢(shì)意味著state 轉(zhuǎn)換失敗。UIGestureRecognizerStateFailed。詳見UIGestureRecognizerState枚舉
typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
UIGestureRecognizerStatePossible, // the recognizer has not yet recognized its gesture, but may be evaluating touch events. this is the default state
UIGestureRecognizerStateBegan, // the recognizer has received touches recognized as the gesture. the action method will be called at the next turn of the run loop
UIGestureRecognizerStateChanged, // the recognizer has received touches recognized as a change to the gesture. the action method will be called at the next turn of the run loop
UIGestureRecognizerStateEnded, // the recognizer has received touches recognized as the end of the gesture. the action method will be called at the next turn of the run loop and the recognizer will be reset to UIGestureRecognizerStatePossible
UIGestureRecognizerStateCancelled, // the recognizer has received touches resulting in the cancellation of the gesture. the action method will be called at the next turn of the run loop. the recognizer will be reset to UIGestureRecognizerStatePossible
UIGestureRecognizerStateFailed, // the recognizer has received a touch sequence that can not be recognized as the gesture. the action method will not be called and the recognizer will be reset to UIGestureRecognizerStatePossible
// Discrete Gestures – gesture recognizers that recognize a discrete event but do not report changes (for example, a tap) do not transition through the Began and Changed states and can not fail or be cancelled
UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded // the recognizer has received touches recognized as the gesture. the action method will be called at the next turn of the run loop and the recognizer will be reset to UIGestureRecognizerStatePossible
};
為view添加多個(gè)手勢(shì)
當(dāng)一個(gè)view添加多個(gè)手勢(shì)時(shí),在缺省情況下,沒有為優(yōu)先執(zhí)行哪個(gè)手勢(shì)做排序,每次發(fā)生不同。不過你可以覆蓋默認(rèn)的行為(使用類方法、委托方法、和子類化覆蓋這些)
指定一個(gè)Gesture recognizers應(yīng)該在另一個(gè)前捕捉。
requireGestureRecognizerToFail: 這個(gè)方法就是在作為參數(shù)的Gesture recognizer失敗以后接受者才發(fā)生,否則從不會(huì)發(fā)生。
[self.panRecognizer requireGestureRecognizerToFail:self.swipeRecognizer];
允許2個(gè)手勢(shì)同時(shí)操作
gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
禁止在某一點(diǎn)發(fā)生Gesture recognizers
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ([touch.view isKindOfClass:[UIControl class]])
{
return NO;
}
return YES;
}
指定一個(gè)單向關(guān)系兩個(gè)手勢(shì)識(shí)別器
想控制兩個(gè)識(shí)別器相互作用,但你需要指定一個(gè)單向關(guān)系,您可以重寫或canPreventGestureRecognizer:或canBePreventedByGestureRecognizer:子類方法。return yes。例如,如果你想要一個(gè)旋轉(zhuǎn)的姿態(tài)來防止捏動(dòng)作,但你不想夾手勢(shì)防止旋轉(zhuǎn)的姿態(tài)。例如,你想一個(gè)旋轉(zhuǎn)手勢(shì)阻止一個(gè)縮放手勢(shì),但你不想一個(gè)縮放手勢(shì)阻止旋轉(zhuǎn)手勢(shì),就加入下面代碼
[rotationGestureRecognizer canPreventGestureRecognizer:pinchGestureRecognizer];
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- iOS開發(fā)之手勢(shì)識(shí)別實(shí)例
- iOS手勢(shì)識(shí)別的詳細(xì)使用方法(拖動(dòng),縮放,旋轉(zhuǎn),點(diǎn)擊,手勢(shì)依賴,自定義手勢(shì))
- iOS輕點(diǎn)、觸摸和手勢(shì)代碼開發(fā)
- 使用Swift代碼實(shí)現(xiàn)iOS手勢(shì)解鎖、指紋解鎖實(shí)例詳解
- iOS開發(fā)之觸摸事件以及手勢(shì)
- iOS仿郵箱大師的九宮格手勢(shì)密碼解鎖
- iOS實(shí)現(xiàn)手勢(shì)解鎖操作
- iOS開發(fā)中的幾個(gè)手勢(shì)操作實(shí)例分享
- IOS手勢(shì)操作(拖動(dòng)、捏合、旋轉(zhuǎn)、點(diǎn)按、長(zhǎng)按、輕掃、自定義)
相關(guān)文章
iOS中使用UISearchBar控件限制輸入字?jǐn)?shù)的實(shí)現(xiàn)方法
這篇文章主要介紹了iOS中使用UISearchBar控件限制輸入字?jǐn)?shù)的實(shí)現(xiàn)方法的相關(guān)資料,需要的朋友可以參考下2016-08-08
iOS 中 使用UITextField格式化銀行卡號(hào)碼的解決方案
今天小編給大家分享ios中使用UITextField格式化銀行卡號(hào)碼的實(shí)現(xiàn)思路詳解,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2016-12-12
分享一個(gè)關(guān)于Storyboard 跳轉(zhuǎn)與傳值
近日不忙,給大家分享一個(gè)關(guān)于storyboard跳轉(zhuǎn)傳值的相關(guān)知識(shí),感興趣的朋友一起看看吧2015-12-12
iOS應(yīng)用開發(fā)中SQLite的初步配置指南
這篇文章主要介紹了iOS應(yīng)用開發(fā)中SQLite的初步配置指南,SQLite是一個(gè)極輕量級(jí)可作嵌入式的數(shù)據(jù)庫(kù),非常適合入門開發(fā)者使用,需要的朋友可以參考下2015-12-12
IOS 開發(fā)中發(fā)送e-mail的幾種方法總結(jié)
這篇文章主要介紹了IOS 開發(fā)中發(fā)送e-mail的幾種方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-03-03
iOS UIScrollView滾動(dòng)視圖/無限循環(huán)滾動(dòng)/自動(dòng)滾動(dòng)的實(shí)例代碼
這篇文章主要介紹了iOS UIScrollView滾動(dòng)視圖/無限循環(huán)滾動(dòng)/自動(dòng)滾動(dòng),需要的朋友可以參考下2017-02-02

