IOS觸摸事件和手勢識別

來源:互聯網
上載者:User

標籤:style   io   ar   os   sp   for   strong   on   log   

IOS觸摸事件和手勢識別

 

目錄

  • 概述
  • 觸摸事件
  • 手勢識別

 

概述

為了實現一些新的需求,我們常常需要給IOS添加觸摸事件和手勢識別

 

觸摸事件

觸摸事件的四種方法

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event  開始觸摸所觸發的方法

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event  移動時觸發的方法

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event  離開時觸發的方法

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event  系統由於某原因取消觸摸事件時所調用的方法

獲得觸摸位置的方法

for(UITouch *t in touches){

CGPoint touchPosition = [t locationInView:self.view];

NSLog(@"%f,%f",touchPosition.x,touchPosition.y);  

}

  

手勢識別

手勢種類

UITapGestureRecognizer輕觸

UISwipeGestureRecognizer很快的滑動

UIPanGestureRecognizer拖動

UIPinchGestureRecognizer兩個手指頭捏或放

UIRotationGestureRecognizer手指方向操作

UILongPressGestureRecognizer長按

UITapGestureRecognizer輕觸

初始化

UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];

[view addGestureRecognizer:tapGR];

常用方法

[self setNumberOfTapsRequired:numbers];

[self setNumberOfTouchesRequired:number2];

其他方法可以參考UIGestureRecognizer文檔

UITapGestureRecognizer的回應程式法

-(void)tapAction:(UITapGestureRecognizer *)sender{

//Tap拿起的時候

if(tapGR.state == UIGestureRecognizerStateEnded){

}

//Tap按下的時候

if(tapGR.state == UIGestureRecognizerStateBegan){

}

//等等其他狀態

}

獲得UITapGestureRecognizer按下的位置

CGPoint point = [sender locationInView:view];

 

 

IOS觸摸事件和手勢識別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.