iOS 4 Gesture Recognizers 應用實戰

來源:互聯網
上載者:User

http://www.techotopia.com/index.php/An_iPhone_iOS_4_Gesture_Recognition_Tutorial_%28Xcode_4%29

在上一章,我們已經概要介紹了iPhone手勢識別技的概念,本章的目的是通過一個執行個體示範各種UIGestureRecognizer子類的用法。本章所建立的程式,將建立和設定各種不同的手勢辨識器,然後用標籤把每個偵測到的手勢的具體資訊顯示出來。

 

建立項目Creating the Gesture Recognition Project

 

開啟Xcode,建立一個View-baseapplication項目,命名為recognizer。

 

為Label設定出口

 

在View對象中,僅有一個label組件,用於顯示偵測到的使用者手勢類型。label上的文字在程式運行過程中會被代碼所改變,因此我們需要為它串連到一個出口上。在xcode的項目導航面板中,選中recognizerViewController.h檔案,將它修改為:

#import <UIKit/UIKit.h>
@interface recognizerViewController : UIViewController {
     UILabel *statusLabel; 
} 
@property (retain, nonatomic) IBOutlet UILabel *statusLabel; 
@end 

接著,編輯recognizerViewController.m。在@synthesize語句中增加相應出口並在合適的地方釋放它。

#import "recognizerViewController.h"  
@implementation recognizerViewController 
@synthesize statusLabel;
 . . 
- (void)dealloc {
     [statusLabel release];
     [super dealloc]; 
}
 . . 
- (void)viewDidUnload {
     [super viewDidUnload];
     self.statusLabel = nil; 
}
 . . 
@end 
 設計介面

 

選擇recognizerViewController.xib,在IB面板中編輯它。Xcode在建立項目時為我們建立了一個UIView,我們需要在這個UIView中增加一個label。從ObjectLibrary (View-> Utilities -> Object Library) 中拖一個Label對象,然後修改label屬性讓文本置中對齊:

圖片

Ctrl+左鍵(或者右鍵)從File’sOwner 拖一條線到Label對象,然後釋放按鍵。在彈出的菜單中選擇statusLabel出口。

 

設定GusetureRecognizers對象

 

我們需要在代碼中使用gesturerecognizers來識別輕擊、輕掃、旋轉和捏合。由於這些recognizers需要串連到view對象,因此建立它們的理想地是recognizerViewController類的viewDidLoad方法:

- (void)viewDidLoad {
     UITapGestureRecognizer *doubleTap = 
        [[UITapGestureRecognizer alloc]
        initWithTarget:self
         action:@selector(tapDetected:)];
     doubleTap.numberOfTapsRequired = 2;
     [self.view addGestureRecognizer:doubleTap];
     [doubleTap release];
     UIPinchGestureRecognizer *pinchRecognizer =          [[UIPinchGestureRecognizer alloc]
         initWithTarget:self
          action:@selector(pinchDetected:)];
     [self.view addGestureRecognizer:pinchRecognizer];
     [pinchRecognizer release];
     UIRotationGestureRecognizer *rotationRecognizer =         [[UIRotationGestureRecognizer alloc]
         initWithTarget:self
          action:@selector(rotationDetected:)];
     [self.view addGestureRecognizer:rotationRecognizer];
     [rotationRecognizer release];
      UISwipeGestureRecognizer *swipeRecognizer =         [[UISwipeGestureRecognizer alloc]
        initWithTarget:self
         action:@selector(swipeDetected:)];
     swipeRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
     [self.view addGestureRecognizer:swipeRecognizer];
     [swipeRecognizer release];
     UILongPressGestureRecognizer *longPressRecognizer =           [[UILongPressGestureRecognizer alloc]
          initWithTarget:self
           action:@selector(longPressDetected:)];
     longPressRecognizer.minimumPressDuration = 3;
     longPressRecognizer.numberOfTouchesRequired = 1;
     [self.view addGestureRecognizer:longPressRecognizer];
     [longPressRecognizer release];
     [super viewDidLoad]; 
 
添加處理方法

 

設定完gesturerecognizer之後,就應當編寫處理方法,處理方法將在相應手勢偵測到之後被recognizer所調用。這些方法也放在了recognizerViewController.m檔案裡,同時這些方法會根據相應手勢的具體資訊來重新整理label上顯示的文字。

-(IBAction)longPressDetected:(UIGestureRecognizer *)sender{
     statusLabel.text = @"Long Press"; 
}
- (IBAction)swipeDetected:(UIGestureRecognizer *)sender {
     statusLabel.text = @"Right Swipe"; 
}
- (IBAction)tapDetected:(UIGestureRecognizer *)sender {
     statusLabel.text = @"Double Tap"; 
}
- (IBAction)pinchDetected:(UIGestureRecognizer *)sender {
     CGFloat scale =  [(UIPinchGestureRecognizer *)sender scale];
     CGFloat velocity = [(UIPinchGestureRecognizer *)sender velocity];
     NSString *resultString = [[NSString alloc] initWithFormat:
          @"Pinch - scale = %f, velocity = %f",
          scale, velocity];
     statusLabel.text = resultString;
     [resultString release]; 
}
- (IBAction)rotationDetected:(UIGestureRecognizer *)sender {
     CGFloat radians = [(UIRotationGestureRecognizer *)sender rotation];
     CGFloat velocity = [(UIRotationGestureRecognizer *)sender velocity];
      NSString *resultString = [[NSString alloc] initWithFormat:
               @"Rotation - Radians = %f, velocity = %f",
         radians, velocity];
     statusLabel.text = resultString;
     [resultString release]; 
} 
 
測試程式

 

最後,編譯和運行程式。為了能夠充分測試捏合和旋轉手勢,最好是在物理裝置中運行程式(因為模擬器上無法類比多點觸摸)。串連已啟用的調試裝置(參考TestingiOS 4 Apps on the iPhone – Developer Certificates and Provisioning Profiles), 然後點擊Xcode的Run按鈕。當程式運行後,進行手勢動作並觀察label中顯示文本的變化。

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.