国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

iOS開發(fā)之手勢gesture詳解

2020-07-26 03:04:18
字體:
供稿:網(wǎng)友

前言  

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

UIKit框架提供了一些預(yù)定義的GestureRecognizer.包含下列手勢

  •  UITapGestureRecognizer敲擊手勢(單擊和雙擊)
  •  UIPanGestureRecognizer(拖動手勢)
  •  UIPinchGestureRecognizer(縮放手勢)
  •  UISwipeGestureRecognizer(擦碰手勢)
  •  UIRotationGestureRecognizer(旋轉(zhuǎn)手勢)
  •  UILongPressGestureRecognizer(長按手勢)

如果你想讓你的應(yīng)用程序來識別一個獨特的手勢,如選擇目錄或糾結(jié)的運動,你可以創(chuàng)建自己的自定義GestureRecognizer,將在下篇介紹

將特定的手勢和view相關(guān)聯(lián)

每一個特定的手勢必須關(guān)聯(lián)到view對象中才會有作用,一個view對象可以關(guān)聯(lián)多個不同的特定手勢,但是每一個特定的手勢只能與一個view相關(guān)聯(lián)。當(dāng)用戶觸摸了view,這個GestureRecognizer就會接受到消息,它可以響應(yīng)特定的觸摸事件。

與特定view關(guān)聯(lián)

  • 創(chuàng)建GestureRecognizer實例
  • addGestureRecognizer
  • 實現(xiàn)處理手勢的方法

可以使用removeGestureRecognizer: 來移除手勢。

_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]; }}

手勢識別狀態(tài)
Gesture recognizers從一個狀態(tài)轉(zhuǎn)到另一狀態(tài)(state)。對于每個狀態(tài),根據(jù)它們是否符合特定條件來決定時候可以移動到下一個狀態(tài)。它們分析多點觸摸。是否識別失敗。未能識別手勢意味著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 

主站蜘蛛池模板:
绥阳县|
高邮市|
灵璧县|
开阳县|
墨脱县|
桃园县|
班玛县|
锦屏县|
金华市|
松潘县|
沂源县|
德昌县|
永清县|
东台市|
民县|
绥江县|
贡觉县|
玉林市|
青海省|
班戈县|
南平市|
怀安县|
浦县|
徐闻县|
苍山县|
保亭|
汕头市|
图们市|
南木林县|
文登市|
类乌齐县|
嘉峪关市|
玉龙|
通河县|
隆安县|
琼结县|
瓦房店市|
宁城县|
普安县|
宁波市|
小金县|