日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOS开发概述-12.手势处理

發布時間:2023/12/14 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS开发概述-12.手势处理 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.手勢識別概述

  • 如果想監聽一個view上面的觸摸事件,之前的做法是
    • 自定義一個view
    • 實現view的touches方法,在方法內部實現具體處理代碼
  • 但是通過touches方法監聽view觸摸事件,有很明顯的幾個缺點
    • 必須得自定義view
    • 由于是在view內部的touches方法中監聽觸摸事件,因此默認情況下,無法讓其他外界對象監聽view的觸摸事件
    • 不容易區分用戶的具體手勢行為
    • iOS 3.2之后,蘋果推出了手勢識別功能(Gesture Recognizer),在觸摸事件處理方面,大大簡化了開發者的開發難度

2.UIGestureRecognizer(手勢識別器)

  • 為了完成手勢識別,必須借助于手勢識別器—-UIGestureRecognizer
  • 利用UIGestureRecognizer,能輕松識別用戶在某個view上面做的一些常見手勢
  • UIGestureRecognizer是一個抽象類,定義了所有手勢的基本行為,使用它的子類才能處理具體的手勢
    • UITapGestureRecognizer(敲擊)
    • UIPinchGestureRecognizer(捏合,用于縮放)
    • UIPanGestureRecognizer(拖拽)
    • UISwipeGestureRecognizer(輕掃)
    • UIRotationGestureRecognizer(旋轉)
    • UILongPressGestureRecognizer(長按)

3.手勢識別器的基本使用

  • UITapGestureRecognizer(點擊手勢)
#pragma mark - 添加點擊手勢 // 設置敲擊手勢 -(void)setUpTap{// 敲擊UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];// 連續敲擊兩次 // tap.numberOfTapsRequired = 2;// 需要兩個手指一起敲擊 // tap.numberOfTouchesRequired = 2;// 設置手勢代理,實現左半邊可以點擊,右半邊不能點擊tap.delegate = self;// 添加手勢[self.redView addGestureRecognizer:tap]; } #pragma mark -UIGestureRecognizerDelegate // 是否可以接受觸摸事件 -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{// 獲取當前觸摸點的位置CGPoint curP = [touch locationInView:self.redView];// 判斷觸摸點的位置if (curP.x > self.redView.bounds.size.width * 0.5) { // 觸摸位置在右半部分return NO;}else{// 觸摸位置在左半部分return YES;} } // 敲擊手勢識別方法 -(void)tap:(UITapGestureRecognizer *)tap{NSLog(@"敲擊手勢"); }
  • UIPinchGestureRecognizer(捏合手勢)
#pragma mark - 添加捏合手勢 // 添加捏合手勢 -(void)setUpPin{// 創建捏合手勢UIPinchGestureRecognizer *pin = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pin:)];[self.redView addGestureRecognizer:pin]; } // 捏合手勢實現方法 -(void)pin:(UIPinchGestureRecognizer *)pin{// 根據捏合比例,相對上一次的視圖尺寸進行放大縮小self.redView.transform = CGAffineTransformScale(self.redView.transform,pin.scale, pin.scale);// 復位,設定捏合后的視圖尺寸為1pin.scale = 1.0; }
  • UISwipeGestureRecognizer(輕掃手勢)
#pragma mark - 添加左右輕掃手勢 // 添加輕掃手勢 -(void)setUpSwipe{// 創建輕掃手勢UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];// 設置輕掃手勢方向,一個手勢只能添加一個方向/*typedef NS_OPTIONS(NSUInteger, UISwipeGestureRecognizerDirection) {UISwipeGestureRecognizerDirectionRight = 1 << 0, 右邊UISwipeGestureRecognizerDirectionLeft = 1 << 1, 左邊UISwipeGestureRecognizerDirectionUp = 1 << 2, 上邊UISwipeGestureRecognizerDirectionDown = 1 << 3 下邊};*/swipe.direction = UISwipeGestureRecognizerDirectionLeft;[self.redView addGestureRecognizer:swipe];// 添加新的手勢(向右輕掃)UISwipeGestureRecognizer *swipe1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];swipe1.direction = UISwipeGestureRecognizerDirectionRight;[self.redView addGestureRecognizer:swipe1]; } // 輕掃手勢實現方法 -(void)swipe:(UISwipeGestureRecognizer *)swipe{// 判斷手勢方向if(swipe.direction == UISwipeGestureRecognizerDirectionLeft){NSLog(@"向左掃");}else if(swipe.direction == UISwipeGestureRecognizerDirectionRight){NSLog(@"向右掃");} }
  • 同時添加旋轉和捏合手勢
#pragma mark - 添加旋轉+捏合手勢 // 添加旋轉手勢 -(void)setUpRotation{// 旋轉手勢UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];rotation.delegate = self;// 添加手勢[self.redView addGestureRecognizer:rotation];// 添加捏合手勢UIPinchGestureRecognizer *pin = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pin:)];pin.delegate = self;[self.redView addGestureRecognizer:pin];} // 旋轉執行方法 -(void)rotation:(UIRotationGestureRecognizer *)rotation{// 將弧度轉換為角度self.redView.transform = CGAffineTransformRotate(self.redView.transform, rotation.rotation);// 復位rotation.rotation = 0; } // 捏合手勢實現方法 -(void)pin:(UIPinchGestureRecognizer *)pin{// 根據捏合比例,相對上一次的視圖尺寸進行放大縮小self.redView.transform = CGAffineTransformScale(self.redView.transform,pin.scale, pin.scale);// 復位,設定捏合后的視圖尺寸為1pin.scale = 1.0; } #pragma mark -UIGestureRecognizerDelegate//可以同時識別多個手勢 -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{return YES; } #pragma mark -UIGestureRecognizerDelegate // 代理方法,可以讓視圖同時識別多個手勢 -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{return YES; }
  • UILongPressGestureRecognizer(長按手勢)
#pragma mark - 添加長安手勢 -(void)setUpPress{// 創建長按手勢UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];// 設置長按手勢的最小停留時間(系統默認是0.5s)longPress.minimumPressDuration = 1.0;// 設置長按允許滑動的距離(系統默認是10,一旦手勢停留時間超出最小停留時間,限制無效)longPress.allowableMovement = 5;// 添加長按手勢[self.redView addGestureRecognizer:longPress]; }// 長按手勢監聽方法實現 -(void)longPress:(UILongPressGestureRecognizer *)longPress{NSLog(@"長按手勢"); }
  • UIPanGestureRecognizer(拖拽手勢)
#pragma mark - 添加拖拽手勢 -(void)setUpPan{// 創建手勢UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];// 添加手勢[self.redView addGestureRecognizer:pan]; } -(void)pan:(UIPanGestureRecognizer *)pan{// 獲取拖拽的偏移位置CGPoint curP = [pan translationInView:self.redView];// 根據偏移位置形變self.redView.transform = CGAffineTransformTranslate(self.redView.transform, curP.x, curP.y);// 復位,讓下一次拖拽拿到的偏移位置是相對于現在的位置[pan setTranslation:CGPointZero inView:self.redView]; }

總結

以上是生活随笔為你收集整理的iOS开发概述-12.手势处理的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。