觸摸事件和手勢(shì)

觸摸事件

1.事件是當(dāng)用戶手指觸擊屏幕及在屏幕上移動(dòng)時(shí),系統(tǒng)不斷發(fā)送給應(yīng)用程序的對(duì)象
2.系統(tǒng)將事件按照特定的路徑傳遞給可以對(duì)其進(jìn)行處理的對(duì)象
3.在iOS中,一個(gè)UITouch對(duì)象表示一個(gè)觸摸,一個(gè)UIEvent對(duì)象表示一個(gè)事件(觸發(fā)事件的集合NSSet)
4.事件對(duì)象中包含與當(dāng)前多點(diǎn)觸摸序列相對(duì)應(yīng)的所有觸摸對(duì)象,還可以提供與特定視圖或窗口相關(guān)聯(lián)的觸摸對(duì)象

觸摸的開始,移動(dòng)和結(jié)束
1.觸摸信息有時(shí)間和空間兩方面
-時(shí)間方面的信息稱為階段,表示觸摸是否剛剛開始、是否正在移動(dòng)或處于靜止?fàn)顟B(tài)以及何時(shí)結(jié)束(生命周期)
-觸摸信息還包括當(dāng)前在視圖或窗口中的位置信息,以及之前的位置信息(如果有的話)
-當(dāng)一個(gè)手指接觸屏幕時(shí),觸摸就和某個(gè)窗口或視圖關(guān)聯(lián)在一起,這個(gè)關(guān)聯(lián)在事件的整個(gè)生命周期都會(huì)得到維護(hù)

在屏幕上的手指操作

UIRsponder 接口
-UIResponder類定義了對(duì)象相應(yīng)和控制事件的接口,是UIApplication、UIView的超類,這類的實(shí)例通常被稱為響應(yīng)對(duì)象
-這個(gè)類中主要的事件控制方法是
開始: touchesBegan:withEvent: 移動(dòng): touchesMoved:withEvent: 結(jié)束: touchesEnded:withEvent: 取消事件響應(yīng):touchesCancelled:withEvent:

四個(gè)手勢(shì)通知方法

//開始觸摸的方法
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //獲取所有的觸摸點(diǎn)數(shù)量
    NSInteger count=touches.count;
    NSLog(@"獲取點(diǎn)擊次數(shù)%ld",(long)count);
    
    //獲取tap數(shù)量
    NSInteger tapCount=[[touches anyObject] tapCount];
    NSLog(@"獲取點(diǎn)擊次數(shù)%ld",(long)tapCount);
    
    
    //先通過事件獲取觸摸點(diǎn)
    UITouch *touch=(UITouch *)[event touchesForView:self.view];
    
    //獲取眾多觸摸點(diǎn)中的其中一點(diǎn)
    UITouch *touch1=(UITouch *)[touches anyObject];
    
    //獲取觸摸點(diǎn)在self.view上的坐標(biāo)
    CGPoint point=[touch1 locationInView:self.view];
    NSLog(@"@(%f,%f)",point.x,point.y);
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"移動(dòng)中");
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"移動(dòng)結(jié)束");
}

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"取消事件響應(yīng)");//鼠標(biāo)點(diǎn)擊屏幕長按,然后按住command+shift+h(home鍵)
}

觸控事件響應(yīng)鏈
-響應(yīng)者 (UIResponder)
-響應(yīng)鏈
-之前提到過第一響應(yīng)者,該響應(yīng)者通常是用戶當(dāng)前正在交互的對(duì)象
1.第一響應(yīng)者是響應(yīng)鏈的開始
2.以UIResponder為父類的任何類都是響應(yīng)者
3.如果第一響應(yīng)者不處理某個(gè)事件,則將該事件傳遞到響應(yīng)者鏈的下一級(jí),一般情況下如果下一級(jí)處理,則停止事件的傳遞
4.如果事件通過整個(gè)響應(yīng)者鏈并且沒有對(duì)象處理該事件,則丟棄該事件

響應(yīng)鏈順序

視圖->控件->視圖控制器->父視圖->父視圖控制器->窗口->應(yīng)用程序代理

向下一響應(yīng)者傳遞事件

可以在上面4個(gè)方法中調(diào)用傳遞事件的響應(yīng),將事件傳遞給下一個(gè)響應(yīng)者

[self.nextResponder touchesBegan:touches withEvent:event];
//這句用來把事件傳遞給下一響應(yīng)者,如果這句去掉,來就是不傳遞給下一個(gè)響應(yīng)者只有四個(gè)方法自己來響應(yīng),而無法把事件傳遞下去

手勢(shì)和觸摸的關(guān)系
1.手勢(shì)是指從用一個(gè)或多個(gè)手指接觸屏幕開始,直到手指離開屏幕為止發(fā)生的所有事件
2.觸摸是指手指放到屏幕上從屏幕上拖動(dòng)或抬起,手勢(shì)中涉及的觸摸數(shù)量等于同時(shí)位于屏幕上的手指數(shù)量
3.手勢(shì)是觸摸事件的集合

檢測觸摸和移動(dòng)

@interface ViewController : UIViewController
@property (nonatomic) int moveCount;
@property (retain, nonatomic) UILabel *messageLabel;
@property (retain, nonatomic) UILabel *tapsLabel;
@property (retain, nonatomic) UILabel *touchesLabel;
-(void) updateLabelsFromTouches:(NSSet *)touches;
@end

控制器代碼
因?yàn)楸卷?xiàng)目需要檢測多點(diǎn)觸控,所以需要為視圖打開支持多點(diǎn)觸控開關(guān)
所以需要在viewDidLoad方法中添加如下代碼:
self.view.multipleTouchEnabled = YES;
-iPhone中最多同時(shí)可以支持5點(diǎn)觸摸
-iPad中最多同時(shí)可以支持11點(diǎn)觸摸。
-模擬器中按下option鍵,可以模擬兩點(diǎn)觸摸
-兩次單擊之間的間隔足夠小的話可以實(shí)現(xiàn)雙擊,三擊甚至更多

手勢(shì)
用一個(gè)或多個(gè)手指接觸屏幕開始,知道手指離開屏幕為止所有的事件
手勢(shì)的種類

點(diǎn)擊(tap)
拖動(dòng)(drag)
滑動(dòng)(flick)
橫掃(swipe)
雙擊(double tap)
放大(pinch)
縮小(pinch)
長按(touch hold)

手勢(shì)識(shí)別器

UIGestureRecognizer

手勢(shì)識(shí)別器類型

-UITapGestureRecognizer      (輕拍識(shí)別器)
-UIPinchGestureRecognizer    (捏合識(shí)別器)
-UIRotationGestureRecognizer (旋轉(zhuǎn)識(shí)別器)
-UISwipeGestureRecognizer    (掃動(dòng)識(shí)別器)
-UIPanGestureRecognizer      (拖動(dòng)識(shí)別器)
-UILongPressGestureRecognizer(長按識(shí)別器)
-UIScreenEdgePanGestureRecognizer (右滑返回識(shí)別器)
通過拖拽來實(shí)現(xiàn)手勢(shì)的添加
在此把用戶交互界面打開.png

創(chuàng)建一個(gè)視圖用來做一下觸碰的實(shí)踐

找一張圖命名1.jpg放入項(xiàng)目中
在函數(shù)的實(shí)現(xiàn)文件中寫

[super viewDidLoad];
    //創(chuàng)建圖片視圖
    UIImageView *imgV=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 150, 150)];
    //創(chuàng)建圖片
    UIImage *img=[UIImage imageNamed:@"1.jpg"];
    //設(shè)置圖片
    imgV.image=img;
    //打開用戶交互,默認(rèn)是NO
    imgV.userInteractionEnabled=YES;
    //居中
    imgV.center=self.view.center;
    //顯示
    [self.view addSubview:imgV];

-UITapGestureRecognizer (輕拍識(shí)別器)

創(chuàng)建

 //---------------創(chuàng)建輕拍手勢(shì)
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    //---------------給圖片視圖添加單擊手勢(shì)(單擊放大或縮小)
    [imgV addGestureRecognizer:tap];

手勢(shì)動(dòng)作方法

//實(shí)現(xiàn)輕拍手勢(shì)
-(void)tapAction:(UITapGestureRecognizer *)sender
{
    //獲取手勢(shì)所添加的視圖
    UIImageView *imgV=(UIImageView *)sender.view;
    
    if (self.imgVState)
    {
        //縮小
        [UIView animateWithDuration:1 animations:^{
            imgV.frame=CGRectMake(0, 0, 150, 150);
            imgV.center=self.view.center;
        }];
    }
    else
    {
        //放大
        [UIView animateWithDuration:1 animations:^{
            imgV.frame=CGRectMake(0, 0, 300, 300);
            imgV.center=self.view.center;
        }];
    }
    //取反用來反復(fù)縮小放大
    self.imgVState=!self.imgVState;
}

-UIPinchGestureRecognizer (捏合識(shí)別器)

先定義一個(gè)屬性

@interface ViewController ()
@property(nonatomic,assign)BOOL imgVState;//標(biāo)示圖片是放大還是縮小,YES放大/NO:縮小
@end

然后創(chuàng)建捏合識(shí)別器

//創(chuàng)建捏合手勢(shì)
    UIPinchGestureRecognizer *pinch=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];
    //添加捏合
    [imgV addGestureRecognizer:pinch];

創(chuàng)建捏合手勢(shì)的方法

//捏合的方法
-(void)pinchAction:(UIPinchGestureRecognizer *)sender
{
    sender.view.transform=CGAffineTransformScale(sender.view.transform, sender.scale, sender.scale);
    //捏合的速度設(shè)置(數(shù)字越大,捏合的速度越快)
    sender.scale=1;
}

-UIRotationGestureRecognizer (旋轉(zhuǎn)識(shí)別器)

先定義屬性

@interface ViewController ()
@property(nonatomic,assign)CGFloat rotation;//記錄每一次旋轉(zhuǎn)后的值
@end

然后創(chuàng)建旋轉(zhuǎn)識(shí)別器

//實(shí)現(xiàn)旋轉(zhuǎn)的手勢(shì)的方法
-(void)rotatingAction:(UIRotationGestureRecognizer *)sender
{
    UIImageView *imgV=(UIImageView *)sender.view;
    //設(shè)置圖片的旋轉(zhuǎn)大小
    imgV.transform=CGAffineTransformMakeRotation(sender.rotation+self.rotation);
    //旋轉(zhuǎn)結(jié)束狀態(tài)結(jié)束后記錄這次旋轉(zhuǎn)的角度
    if (sender.state==UIGestureRecognizerStateEnded)//如果結(jié)束的話便記錄下來
    {
        self.rotation=sender.rotation;
    }
}

-UISwipeGestureRecognizer (掃動(dòng)識(shí)別器)

創(chuàng)建掃動(dòng)識(shí)別器

 //---------------創(chuàng)建掃動(dòng)
    UISwipeGestureRecognizer *swipe=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
    //添加掃動(dòng)
    [imgV addGestureRecognizer:swipe];
    //設(shè)置掃動(dòng)的方向
    swipe.direction=UISwipeGestureRecognizerDirectionDown;;//向下掃動(dòng)
    [pan requireGestureRecognizerToFail:swipe];//當(dāng)掃動(dòng)swipe失敗后才啟用拖拽pan
    [imgV addGestureRecognizer:swipe];

掃動(dòng)識(shí)別器的方法

//掃動(dòng)的動(dòng)畫
-(void)swipeAction:(UISwipeGestureRecognizer *)sender
{
    //掃動(dòng)時(shí)操作把圖定位在(0,0)位置,圖大小(250,250)
    [UIView animateWithDuration:1 animations:^{
        sender.view.frame=CGRectMake(0, 0, 250, 250);
    }];
}

-UIPanGestureRecognizer (拖動(dòng)識(shí)別器)

創(chuàng)建拖動(dòng)識(shí)別器

//---------------創(chuàng)建拖拽手勢(shì)
    UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
    //添加拖拽
    [imgV addGestureRecognizer:pan];

拖動(dòng)識(shí)別的方法

//拖動(dòng)手勢(shì)的方法
-(void)panAction:(UIPanGestureRecognizer *)sender
{
    //獲取手勢(shì)在屏幕上拖動(dòng)的點(diǎn)
    CGPoint p=[sender translationInView:sender.self.view];
    //設(shè)置中心點(diǎn)
    sender.view.center=CGPointMake(sender.view.center.x+p.x, sender.view.center.y+p.y);
    //設(shè)置視圖在父視圖上拖拽的位置
    [sender setTranslation:CGPointZero inView:sender.self.view];//用來設(shè)置勻速運(yùn)動(dòng)
}

-UILongPressGestureRecognizer(長按識(shí)別器)

創(chuàng)建長按識(shí)別器

   //---------------創(chuàng)建添加長按識(shí)別器
    UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
    //添加長按的動(dòng)畫
    [imgV addGestureRecognizer:longPress];

長按識(shí)別器的方法

//長按的方法
-(void)longPressAction:(UILongPressGestureRecognizer *)sender
{
    //判斷開始按下
    if (sender.state==UIGestureRecognizerStateBegan)
    {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"確定刪除" delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:@"取消", nil];
        //顯示提示框
        [alert show];
    }
}

-UIScreenEdgePanGestureRecognizer (右滑返回識(shí)別器)

此為iOS7中新添加的屬性
創(chuàng)建右滑動(dòng)返回識(shí)別器

//---------------創(chuàng)建添加右滑返回識(shí)別器
    UIScreenEdgePanGestureRecognizer *edgePan=[[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(edgePanAction:)];
    //設(shè)置滑動(dòng)的方式,為右滑動(dòng)
    edgePan.edges=UIRectEdgeLeft;
    [self.view addGestureRecognizer:edgePan];

右滑返回識(shí)別的方法

//右滑返回識(shí)別的方法
-(void)edgePanAction:(UIScreenEdgePanGestureRecognizer *)sender
{
    NSLog(@"實(shí)現(xiàn)了右滑動(dòng)返回操作");
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容