天天看點

iOS中得手勢和觸摸使用(常見)

2013-12-19 21:59 171人閱讀 評論(0) 收藏 舉報

  事件産生與分

  觸摸

  手勢

  手勢識别器。

 事件産生與分發:ios中的觸摸事件,基于多點觸摸模型

    多點觸摸序列:

         一個或多個接觸螢幕的手指,識别為多點觸摸序列的一部分

         從第一個手指碰到螢幕開始,直到最後一個手指離開螢幕結束

    Touch 是指手指放在螢幕上。

    觸摸數量等于手指數量。

    手指觸摸螢幕、立即離開,則出發tap。

 手勢————

    UITouch:一個觸摸

    UIEvent:一個事件

 響應者鍊————

   ——》第一響應者是正在互動單對象

   ——》第一響應者不處理事件,事件會按照響應者鍊逐級發送

   ——》事件最終發送到uiapplication,若不處理,則被丢棄

 事件處理函數

   -toucheBegan:withEvent:

      當使用者開始觸摸螢幕時,在事件的開始階段調用

   -toucheMoved:withEvent:

      處理手指的移動

   -toucheEnded:withEvent:

      結束觸摸過程時調用

   -toucheCancelled:withEvent:

      觸摸事件被系統事件(呼入電話等)中斷時調用

 實作觸摸處理:

    建立單視圖工程(TouchExplorer)實作觸摸處理。

    XIB檔案配置:在界面上布置兩個标簽,實作連結

 開啟多點觸摸相關代碼:self.multipleTouchEnabled=YES;

                   (self.userInteractionEnabled=YES;)->可省

 擷取一個touch對象的相關代碼  UITouch *pTouch=[touches anyObject];

 擷取對象在坐标系中的點相關代碼:

      CGPoint point=[pTouch locationInView:self];

 擷取點選次數相關代碼: NSInteger tapCount=[pTouch tapCount];

 假設點選一次關聯方法為- (void)singleTouch:(id)sender;點選兩次關聯方法為

 - (void)doubleTouch:(id)sender,則有如下:

   一、  實作單次點選晚0.2秒執行代碼:  [self     performSelector:@selector(singleTouch:) withObject:nil     afterDelay:0.2];

   二、 實作輕按兩下,然後并不執行單擊方法相關代碼: [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(singleTouch:) object:nil]; 

 計算兩點之間距離的方法:(封裝方法)

  - (double)distanceOfPoint:(CGPoint)point1 withPoint:(CGPoint)point2{

    double num1=pow(point1.x - point2.x,2);

    double num2=pow(point1.y - point2.y,2);

    double distance=sqrt(num1+num2);

    return distance;

}

 利用自己封裝好的計算距離方法來求出執行個體中兩點間單距離:

    //擷取所有觸摸對象

    NSArray *pArr=[touches allObjects];

    //分别取出兩個touch對象

    UITouch *pTouch1=[pArr objectAtIndex:0];

    UITouch *pTouch2=[pArr objectAtIndex:1];

    //擷取兩個touch對象的坐标點

    CGPoint point1=[pTouch1 locationInView:self];

    CGPoint point2=[pTouch2 locationInView:self];

    //通過自己封裝的方法擷取兩個點之間的距離

    double distance=[self distanceOfPoint:point1 withPoint:point2];

              手勢識别器

  識别單擊的相關代碼(包括建立單擊對象以及關聯方法)下同:

      UITapGestureRecognizer *pSingleTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleGesture:)];

    [self.view addGestureRecognizer:pSingleTap];

  識别輕按兩下單相關代碼:

   UITapGestureRecognizer *pDoubleTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleGesture:)];

    pDoubleTap.numberOfTapsRequired=2; (多加注意)

    [self.view addGestureRecognizer:pDoubleTap];

  識别清掃手勢相關代碼:

    UISwipeGestureRecognizer *pSwip=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipGesture:)];

    //滑動方向(一定不要忘)

    pSwip.direction=UISwipeGestureRecognizerDirectionLeft;

    [self.view addGestureRecognizer:pSwip];

    [pSwip release];

  識别平移相關代碼:

     UIPanGestureRecognizer *pPan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGesture:)];

    [self.view addGestureRecognizer:pPan];

    [pPan release];

    (注意,加完平移代碼後會覆寫上面加載的清掃代碼,也就是不會再識别清掃)

   識别長按相關代碼:

      UILongPressGestureRecognizer *pLong=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longGesture:)];

    pLong.minimumPressDuration=1;(注意此步不能少)、、長按的時間間隔

    [self.view addGestureRecognizer:pLong];

    [pLong release];

    識别旋轉試圖器相關代碼:

       UIRotationGestureRecognizer *pRotation=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGesture:)];

    [self.view addGestureRecognizer:pRotation];

    [pRotation release];

上一篇: qt學習

繼續閱讀