你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS編程技術 >> 基礎手勢識別

基礎手勢識別

編輯:IOS編程技術

一、UIGestureRecognizer介紹

    手勢在ios經常用到,可以說是非常重要的存在,在之前,大部分都是通過touch方法來進行手勢的判定,但是在ios系統3.2之後,添加了UIGestureRecongnizer抽象類,抽象出了幾個子類  
  • UITapGestureRecognizer  (點)
  • UIPinchGestureRecognizer (縮放)
  • UIRotationGestureRecognizer (旋轉)
  • UISwipeGestureRecognizer (輕掃)
  • UIPanGestureRecognizer (拖動)
  • UILongPressGestureRecognizer (長按)
  這些手勢都很簡單,基本上明白一個的用法,其他的也就都明白了,所以我們今天就以 拖動為例,給大家做一個側滑展開菜單欄   我們先來了解一下這些手勢的基本用法  

1、縮放手勢

UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self  action:@selector(handlePinch:)];  
[imageView addGestureRecognizer:pinchGestureRecognizer];  

回調方法

- (void) handlePinch:(UIPinchGestureRecognizer*) recognizer  
{  
    recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);  
    recognizer.scale = 1;  
}  

2.拖動手勢

UIPanGestureRecognizer * panRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];  
 //添加手勢到 tabBarController.view  
 [tabBarController.view addGestureRecognizer:panRecognizer];  

回調方法

- (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];  
      
}  

3.旋轉手勢

/** 
 *  處理旋轉手勢 
 * 
 *  @param recognizer 旋轉手勢識別器對象實例 
 */  
- (void)handleRotation:(UIRotationGestureRecognizer *)recognizer 
{ recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation); recognizer.rotation = 0.0; }

 

大家需要注意的是,一個手勢只能對應一個View, 但是一個View可以有多個手勢

 

 

 

 

 

 

 

 
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved