iOS 上下左右手勢識別 – iPhone手機開發技術文章 iPhone軟體開發教學課程

– (void)viewDidLoad {  

//建立滑動手勢

    UISwipeGestureRecognizer *recognizer;   

    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:selfaction:@selector(handleSwipeFrom:)];   

//設置滑動方向,下面以此類推

    [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];   

    [[self view] addGestureRecognizer:recognizer];   

    [recognizer release];  

 

     recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:selfaction:@selector(handleSwipeFrom:)];   

    [recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];   

    [[self view] addGestureRecognizer:recognizer];   

    [recognizer release];   

      

    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:selfaction:@selector(handleSwipeFrom:)];   

    [recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];  

    [[self view] addGestureRecognizer:recognizer];   

    [recognizer release];   

      

    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:selfaction:@selector(handleSwipeFrom:)];   

    [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];   

    [[self view] addGestureRecognizer:recognizer];   

    [recognizer release];  

  

    [super viewDidLoad];  

 }  

  

//這個是協議回調,所以需要遵循 UIGestureRecognizerDelegate協議

-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {   

     NSLog(@"Swipe received.");    www.aiwalls.com

     if (recognizer.direction==UISwipeGestureRecognizerDirectionDown) {   

                NSLog(@"向下滑動");  

    //執行程序  

     }  

     if (recognizer.direction==UISwipeGestureRecognizerDirectionUp) {   

    

NSLog(@"向上滑動");  

    //執行程序  

     }  

     if (recognizer.direction==UISwipeGestureRecognizerDirectionLeft) {   

    

NSLog(@"向左滑動");  

    //執行程序  

     }  

     if (recognizer.direction==UISwipeGestureRecognizerDirectionRight) {   

    

NSLog(@"向右滑動");  

    //執行程序  

     }  

}  

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *