亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

iOS手勢識別的詳細(xì)使用方法(拖動(dòng),縮放,旋轉(zhuǎn),點(diǎn)擊,手勢依賴,自定義手勢)

 更新時(shí)間:2016年11月08日 11:40:04   作者:super_man_風(fēng)清揚(yáng)  
這篇文章主要介紹了iOS手勢識別的詳細(xì)使用方法(拖動(dòng),縮放,旋轉(zhuǎn),點(diǎn)擊,手勢依賴,自定義手勢),具有一定的參考價(jià)值,有需要的可以參考一下。

手勢識別在iOS上非常重要,手勢操作移動(dòng)設(shè)備的重要特征,極大的增加了移動(dòng)設(shè)備使用便捷性。

1、UIGestureRecognizer介紹

手勢識別在iOS上非常重要,手勢操作移動(dòng)設(shè)備的重要特征,極大的增加了移動(dòng)設(shè)備使用便捷性。

iOS系統(tǒng)在3.2以后,為方便開發(fā)這使用一些常用的手勢,提供了UIGestureRecognizer類。手勢識別UIGestureRecognizer類是個(gè)抽象類,下面的子類是具體的手勢,開發(fā)這可以直接使用這些手勢識別。

  • UITapGestureRecognizer 
  • UIPinchGestureRecognizer
  • UIRotationGestureRecognizer
  • UISwipeGestureRecognizer
  • UIPanGestureRecognizer
  • UILongPressGestureRecognizer

上面的手勢對應(yīng)的操作是:

  • Tap(點(diǎn)一下)
  • Pinch(二指往內(nèi)或往外撥動(dòng),平時(shí)經(jīng)常用到的縮放)
  • Rotation(旋轉(zhuǎn))
  • Swipe(滑動(dòng),快速移動(dòng))
  • Pan (拖移,慢速移動(dòng))
  •  LongPress(長按)

UIGestureRecognizer的繼承關(guān)系如下:

2、使用手勢的步驟

使用手勢很簡單,分為兩步:

創(chuàng)建手勢實(shí)例。當(dāng)創(chuàng)建手勢時(shí),指定一個(gè)回調(diào)方法,當(dāng)手勢開始,改變、或結(jié)束時(shí),回調(diào)方法被調(diào)用。

添加到需要識別的View中。每個(gè)手勢只對應(yīng)一個(gè)View,當(dāng)屏幕觸摸在View的邊界內(nèi)時(shí),如果手勢和預(yù)定的一樣,那就會(huì)回調(diào)方法。

ps:一個(gè)手勢只能對應(yīng)一個(gè)View,但是一個(gè)View可以有多個(gè)手勢。

建議在真機(jī)上運(yùn)行這些手勢,模擬器操作不太方便,可能導(dǎo)致你認(rèn)為手勢失效。

3、Pan 拖動(dòng)手勢:

UIImageView *snakeImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"snake.png"]]; 
snakeImageView.frame = CGRectMake(50, 50, 100, 160); 
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] 
                        initWithTarget:self 
                        action:@selector(handlePan:)];   
[snakeImageView addGestureRecognizer:panGestureRecognizer]; 
[self.view setBackgroundColor:[UIColor whiteColor]]; 
[self.view addSubview:snakeImageView]; 

新建一個(gè)ImageView,然后添加手勢

回調(diào)方法:

- (void) handlePan:(UIPanGestureRecognizer*) recognizer 
{ 
  CGPoint translation = [recognizer translationInView:self.view]; 
  recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, 
                  recognizer.view.center.y + translation.y); 
  [recognizer setTranslation:CGPointZero inView:self.view]; 
   
} 

4、Pinch縮放手勢

UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] 
                            initWithTarget:self 
                            action:@selector(handlePinch:)];<p class="p1">[<span class="s1">snakeImageView</span> <span class="s2">addGestureRecognizer</span>:pinchGestureRecognizer];</p> 
- (void) handlePinch:(UIPinchGestureRecognizer*) recognizer 
{ 
  recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale); 
  recognizer.scale = 1; 
} 

5、Rotation旋轉(zhuǎn)手勢

UIRotationGestureRecognizer *rotateRecognizer = [[UIRotationGestureRecognizer alloc] 
                         initWithTarget:self 
                         action:@selector(handleRotate:)]; 
[snakeImageView addGestureRecognizer:rotateRecognizer]; 
- (void) handleRotate:(UIRotationGestureRecognizer*) recognizer 
{ 
  recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation); 
  recognizer.rotation = 0; 
} 

添加了這幾個(gè)手勢后,運(yùn)行看效果,程序中的imageView放了一個(gè)

                   /^\/^\
                  _|__|  O|
         \/     /~     \_/ \
          \____|__________/  \
                 \_______      \
                         `\     \                 \
                           |     |                  \
                          /      /                    \
                         /     /                       \\
                       /      /                         \ \
                      /     /                            \  \
                    /     /             _----_            \   \
                   /     /           _-~      ~-_         |   |
                  (      (        _-~    _--_    ~-_     _/   |
                   \      ~-____-~    _-~    ~-_    ~-_-~    /
                     ~-_           _-~          ~-_       _-~ 
                        ~--______-~                ~-___-~

的圖片,在模擬器上拖動(dòng)是沒問題的??s放和旋轉(zhuǎn)有點(diǎn)問題,估計(jì)是因?yàn)樵谀M器上的模擬的兩個(gè)接觸點(diǎn)距離在imageView的邊界外了,所以操作無效果。

建議在真機(jī)上運(yùn)行這個(gè)手勢。

在模擬器上縮放和選擇的操作技巧:

可以把imageView的frame值設(shè)置大一點(diǎn),按住alt鍵,按下觸摸板(不按下不行),這樣就可以旋轉(zhuǎn)和縮放了。

6、添加第二個(gè)ImagView并添加手勢

記?。阂粋€(gè)手勢只能添加到一個(gè)View,兩個(gè)View當(dāng)然要有兩個(gè)手勢的實(shí)例了

- (void)viewDidLoad 
{ 
  [super viewDidLoad]; 
 
  UIImageView *snakeImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"snake.png"]]; 
  UIImageView *dragonImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dragon.png"]]; 
  snakeImageView.frame = CGRectMake(120, 120, 100, 160); 
  dragonImageView.frame = CGRectMake(50, 50, 100, 160); 
  [self.view addSubview:snakeImageView]; 
  [self.view addSubview:dragonImageView]; 
   
  for (UIView *view in self.view.subviews) { 
    UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] 
                            initWithTarget:self 
                            action:@selector(handlePan:)]; 
     
    UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] 
                              initWithTarget:self 
                              action:@selector(handlePinch:)]; 
     
    UIRotationGestureRecognizer *rotateRecognizer = [[UIRotationGestureRecognizer alloc] 
                             initWithTarget:self 
                             action:@selector(handleRotate:)]; 
     
    [view addGestureRecognizer:panGestureRecognizer]; 
    [view addGestureRecognizer:pinchGestureRecognizer]; 
    [view addGestureRecognizer:rotateRecognizer]; 
    [view setUserInteractionEnabled:YES]; 
  } 
  [self.view setBackgroundColor:[UIColor whiteColor]];    
} 

多添加了一條龍的view,兩個(gè)view都能接收上面的三種手勢。運(yùn)行效果如下:

7、拖動(dòng)(pan手勢)速度(以較快的速度拖放后view有滑行的效果)
如何實(shí)現(xiàn)呢?

  • 監(jiān)視手勢是否結(jié)束
  • 監(jiān)視觸摸的速度
- (void) handlePan:(UIPanGestureRecognizer*) recognizer 
{ 
  CGPoint translation = [recognizer translationInView:self.view]; 
  recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, 
                    recognizer.view.center.y + translation.y); 
  [recognizer setTranslation:CGPointZero inView:self.view]; 
   
  if (recognizer.state == UIGestureRecognizerStateEnded) { 
     
    CGPoint velocity = [recognizer velocityInView:self.view]; 
    CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y)); 
    CGFloat slideMult = magnitude / 200; 
    NSLog(@"magnitude: %f, slideMult: %f", magnitude, slideMult); 
     
    float slideFactor = 0.1 * slideMult; // Increase for more of a slide 
    CGPoint finalPoint = CGPointMake(recognizer.view.center.x + (velocity.x * slideFactor), 
                     recognizer.view.center.y + (velocity.y * slideFactor)); 
    finalPoint.x = MIN(MAX(finalPoint.x, 0), self.view.bounds.size.width); 
    finalPoint.y = MIN(MAX(finalPoint.y, 0), self.view.bounds.size.height); 
     
    [UIView animateWithDuration:slideFactor*2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{ 
      recognizer.view.center = finalPoint; 
    } completion:nil]; 
     
  } 

代碼實(shí)現(xiàn)解析:

  • 計(jì)算速度向量的長度(估計(jì)大部分都忘了)這些知識了。
  • 如果速度向量小于200,那就會(huì)得到一個(gè)小于的小數(shù),那么滑行會(huì)很短
  • 基于速度和速度因素計(jì)算一個(gè)終點(diǎn)
  • 確保終點(diǎn)不會(huì)跑出父View的邊界
  • 使用UIView動(dòng)畫使view滑動(dòng)到終點(diǎn)

運(yùn)行后,快速拖動(dòng)圖像view放開會(huì)看到view還會(huì)在原來的方向滑行一段路。

8、同時(shí)觸發(fā)兩個(gè)view的手勢

手勢之間是互斥的,如果你想同時(shí)觸發(fā)蛇和龍的view,那么需要實(shí)現(xiàn)協(xié)議

UIGestureRecognizerDelegate,

@interface ViewController : UIViewController<UIGestureRecognizerDelegate> 
@end 

并在協(xié)議這個(gè)方法里返回YES。

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer 
{ 
  return YES; 
} 

 把self作為代理設(shè)置給手勢:

panGestureRecognizer.delegate = self; 
pinchGestureRecognizer.delegate = self; 
rotateRecognizer.delegate = self; 

這樣可以同時(shí)拖動(dòng)或旋轉(zhuǎn)縮放兩個(gè)view了。

9、tap點(diǎn)擊手勢

這里為了方便看到tap的效果,當(dāng)點(diǎn)擊一下屏幕時(shí),播放一個(gè)聲音。

為了播放聲音,我們加入AVFoundation.framework這個(gè)框架。

- (AVAudioPlayer *)loadWav:(NSString *)filename { 
  NSURL * url = [[NSBundle mainBundle] URLForResource:filename withExtension:@"wav"]; 
  NSError * error; 
  AVAudioPlayer * player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; 
  if (!player) { 
    NSLog(@"Error loading %@: %@", url, error.localizedDescription); 
  } else { 
    [player prepareToPlay]; 
  } 
  return player; 
} 

我會(huì)在最后例子代碼給出完整代碼,添加手勢的步驟和前面一樣的。

#import <UIKit/UIKit.h> 
#import <AVFoundation/AVFoundation.h> 
 
@interface ViewController : UIViewController<UIGestureRecognizerDelegate> 
@property (strong) AVAudioPlayer * chompPlayer; 
@property (strong) AVAudioPlayer * hehePlayer; 
 
@end 
- (void)handleTap:(UITapGestureRecognizer *)recognizer { 
  [self.chompPlayer play]; 
} 

運(yùn)行,點(diǎn)一下某個(gè)圖,就會(huì)播放一個(gè)咬東西的聲音。

不過這個(gè)點(diǎn)擊播放聲音有點(diǎn)缺陷,就是在慢慢拖動(dòng)的時(shí)候也會(huì)播放。這使得兩個(gè)手勢重合了。怎么解決呢?使用手勢的:

requireGestureRecognizerToFail方法。

10、手勢的依賴性

在viewDidLoad的循環(huán)里添加這段代碼:

[tapRecognizer requireGestureRecognizerToFail:panGestureRecognizer]; 

意思就是,當(dāng)如果pan手勢失敗,就是沒發(fā)生拖動(dòng),才會(huì)出發(fā)tap手勢。這樣如果你有輕微的拖動(dòng),那就是pan手勢發(fā)生了。tap的聲音就不會(huì)發(fā)出來了。

11、自定義手勢

自定義手勢繼承:UIGestureRecognizer,實(shí)現(xiàn)下面的方法:

– touchesBegan:withEvent: 
– touchesMoved:withEvent: 
– touchesEnded:withEvent: 
- touchesCancelled:withEvent: 

新建一個(gè)類,繼承UIGestureRecognizer,代碼如下:
.h文件

#import <UIKit/UIKit.h> 
typedef enum { 
  DirectionUnknown = 0, 
  DirectionLeft, 
  DirectionRight 
} Direction; 
 
@interface HappyGestureRecognizer : UIGestureRecognizer 
@property (assign) int tickleCount; 
@property (assign) CGPoint curTickleStart; 
@property (assign) Direction lastDirection; 
 
@end 

.m文件

#import "HappyGestureRecognizer.h" 
#import <UIKit/UIGestureRecognizerSubclass.h> 
#define REQUIRED_TICKLES    2 
#define MOVE_AMT_PER_TICKLE   25 
 
@implementation HappyGestureRecognizer 
 
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
  UITouch * touch = [touches anyObject]; 
  self.curTickleStart = [touch locationInView:self.view]; 
} 
 
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
   
  // Make sure we've moved a minimum amount since curTickleStart 
  UITouch * touch = [touches anyObject]; 
  CGPoint ticklePoint = [touch locationInView:self.view]; 
  CGFloat moveAmt = ticklePoint.x - self.curTickleStart.x; 
  Direction curDirection; 
  if (moveAmt < 0) { 
    curDirection = DirectionLeft; 
  } else { 
    curDirection = DirectionRight; 
  } 
  if (ABS(moveAmt) < MOVE_AMT_PER_TICKLE) return; 
   
  // 確認(rèn)方向改變了 
  if (self.lastDirection == DirectionUnknown || 
    (self.lastDirection == DirectionLeft && curDirection == DirectionRight) || 
    (self.lastDirection == DirectionRight && curDirection == DirectionLeft)) { 
     
    // 撓癢次數(shù) 
    self.tickleCount++; 
    self.curTickleStart = ticklePoint; 
    self.lastDirection = curDirection; 
     
    // 一旦撓癢次數(shù)超過指定數(shù),設(shè)置手勢為結(jié)束狀態(tài) 
    // 這樣回調(diào)函數(shù)會(huì)被調(diào)用。 
    if (self.state == UIGestureRecognizerStatePossible && self.tickleCount > REQUIRED_TICKLES) { 
      [self setState:UIGestureRecognizerStateEnded]; 
    } 
  } 
   
} 
 
- (void)reset { 
  self.tickleCount = 0; 
  self.curTickleStart = CGPointZero; 
  self.lastDirection = DirectionUnknown; 
  if (self.state == UIGestureRecognizerStatePossible) { 
    [self setState:UIGestureRecognizerStateFailed]; 
  } 
} 
 
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
  [self reset]; 
} 
 
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
  [self reset]; 
} 
 
@end 

調(diào)用自定義手勢和上面一樣,回到這樣寫:

- (void)handleHappy:(HappyGestureRecognizer *)recognizer{ 
  [self.hehePlayer play]; 
} 

手勢成功后播放呵呵笑的聲音。

在真機(jī)上運(yùn)行,按住某個(gè)view,快速左右拖動(dòng),就會(huì)發(fā)出笑的聲音了。

代碼解析:

先獲取起始坐標(biāo):curTickleStart

通過和ticklePoint的x值對比,得出當(dāng)前的放下是向左還是向右。再算出移動(dòng)的x的值是否比MOVE_AMT_PER_TICKLE距離大,如果太則返回。

再判斷是否有三次是不同方向的動(dòng)作,如果是則手勢結(jié)束,回調(diào)。

源代碼:源代碼下載。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • unix 編程進(jìn)程控制詳細(xì)介紹

    unix 編程進(jìn)程控制詳細(xì)介紹

    這篇文章主要介紹了unix 編程進(jìn)程控制詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下
    2017-01-01
  • IOS使用TestFlight測試的使用方法

    IOS使用TestFlight測試的使用方法

    TestFlight是iOS系統(tǒng)上用來測試軟件的,打開了這個(gè)APP就相當(dāng)于打開了新世界的大門,這個(gè)APP直接可以改變你對IOS系統(tǒng)封閉的看法,讓你擁有媲美安卓用戶的更多自主權(quán)!
    2022-12-12
  • Xcode 下刪除Provisioning Profiles文件詳細(xì)介紹

    Xcode 下刪除Provisioning Profiles文件詳細(xì)介紹

    這篇文章主要介紹了Xcode 下刪除Provisioning Profiles文件詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下
    2016-12-12
  • iOS調(diào)用高德地圖SDK的完整步驟

    iOS調(diào)用高德地圖SDK的完整步驟

    高德LBS開放平臺(tái)將高德最專業(yè)的定位、地圖、搜索、導(dǎo)航等能力,以API、SDK等形式向廣大開發(fā)者免費(fèi)開放,下面這篇文章主要給大家介紹了關(guān)于iOS調(diào)用高德地圖SDK的完整步驟,需要的朋友可以參考下
    2021-11-11
  • 詳解iOS Method Swizzling使用陷阱

    詳解iOS Method Swizzling使用陷阱

    這篇文章主要介紹了詳解iOS Method Swizzling使用陷阱,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • 詳解iOS應(yīng)用程序內(nèi)購/內(nèi)付費(fèi)(一)

    詳解iOS應(yīng)用程序內(nèi)購/內(nèi)付費(fèi)(一)

    這篇文章主要介紹了詳解iOS應(yīng)用程序內(nèi)購/內(nèi)付費(fèi)(一),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。
    2016-12-12
  • 簡述iOS屬性中的內(nèi)存管理參數(shù)

    簡述iOS屬性中的內(nèi)存管理參數(shù)

    這篇文章主要介紹了簡述iOS屬性中的內(nèi)存管理參數(shù) 的相關(guān)資料,需要的朋友可以參考下
    2018-02-02
  • iOS視頻壓縮存儲(chǔ)至本地并上傳至服務(wù)器實(shí)例代碼

    iOS視頻壓縮存儲(chǔ)至本地并上傳至服務(wù)器實(shí)例代碼

    本篇文章主要介紹了iOS視頻壓縮存儲(chǔ)至本地并上傳至服務(wù)器實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • iOS長按UIlabel實(shí)現(xiàn)可復(fù)制功能

    iOS長按UIlabel實(shí)現(xiàn)可復(fù)制功能

    在我們?nèi)粘5拈_發(fā)中經(jīng)常會(huì)遇到一些小需求,比如需要長按控件來拷貝控件中得內(nèi)容,所以這篇文章跟大家分享下iOS中長按UIlabel實(shí)現(xiàn)可復(fù)制功能的方法,有需要的朋友們可以參考借鑒。
    2016-09-09
  • IOS 屏幕適配方案實(shí)現(xiàn)縮放window的示例代碼

    IOS 屏幕適配方案實(shí)現(xiàn)縮放window的示例代碼

    這篇文章主要介紹了IOS 屏幕適配方案實(shí)現(xiàn)縮放window的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04

最新評論