UIImageView響應點擊事件


  1. C代碼    收藏代碼
    1. UIImageView *imageView = [self getYourImageView];  
    2. imageView.image = [self getYourImage];  
    3. imageView.userInteractionEnabled = YES;  
    4. UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(yourHandlingCode:)];  
    5. [imageView addGestureRecognizer:singleTap];  
    6. [singleTap release];  
     UITapGestureRecognizer 的用法
Java代碼    收藏代碼
  1. //單指單擊  
  2. UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self  
  3.  action:@selector(handleSingleFingerEvent:)];  
  4.  singleFingerOne.numberOfTouchesRequired = 1//手指數  
  5.  singleFingerOne.numberOfTapsRequired = 1//tap次數  
  6.  singleFingerOne.delegate = self;  
  7.   
  8.  //單指雙擊  
  9.  UITapGestureRecognizer *singleFingerTwo = [[UITapGestureRecognizer alloc] initWithTarget:self  
  10.  action:@selector(handleSingleFingerEvent:)];  
  11.  singleFingerTwo.numberOfTouchesRequired = 1;  
  12.  singleFingerTwo.numberOfTapsRequired = 2;  
  13.  singleFingerTwo.delegate = self;  
  14.  //雙指單擊  
  15. UITapGestureRecognizer *doubleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self  
  16. action:@selector(handleDoubleFingerEvent:)];  
  17.  doubleFingerOne.numberOfTouchesRequired = 2;  
  18.  doubleFingerOne.numberOfTapsRequired = 1;  
  19. doubleFingerOne.delegate = self;  
  20. UITapGestureRecognizer *doubleFingerTwo = [[UITapGestureRecognizer alloc] initWithTarget:self  
  21.                      action:@selector(handleDoubleFingerEvent:)];  
  22.  doubleFingerTwo.numberOfTouchesRequired = 2;  
  23.  doubleFingerTwo.numberOfTapsRequired = 2;  
  24.  doubleFingerTwo.delegate = self;  
  25. //若是不加下面的話,當單指雙擊時,會先調用單指單擊中的處理,再調用單指雙擊中的處理  
  26. [singleFingerOne requireGestureRecognizerToFail:singleFingerTwo];  
  27.  //同理雙指亦是如此  
  28.  [doubleFingerOne requireGestureRecognizerToFail:doubleFingerTwo];  
  29.  [self.view addGestureRecognizer:singleFingerOne];  
  30. [self.view addGestureRecognizer:singleFingerTwo];  
  31. [self.view addGestureRecognizer:doubleFingerOne];  
  32.  [self.view addGestureRecognizer:doubleFingerTwo];  
  33.  [singleFingerOne release];  
  34.  [singleFingerTwo release];  
  35. [doubleFingerOne release];  
  36.  [doubleFingerTwo release];  
  37. 處理事件的方法,代碼:  
  38. //處理單指事件  
  39.  - (void)handleSingleFingerEvent:(UITapGestureRecognizer *)sender  
  40. {  
  41. if (sender.numberOfTapsRequired == 1) {  
  42.  //單指單擊  
  43.  NSLog(@"單指單擊");  
  44. }else if(sender.numberOfTapsRequired == 2){  
  45.  //單指雙擊  
  46. NSLog(@"單指雙擊");  
  47. }  
  48.  }  
  49.  //處理雙指事件  
  50.  - (void)handleDoubleFingerEvent:(UITapGestureRecognizer *)sender  
  51. {  
  52.  if (sender.numberOfTapsRequired == 1) {  
  53.  //雙指單擊  
  54. NSLog(@"雙指單擊");  
  55. }else if(sender.numberOfTapsRequired == 2){  
  56. //雙指雙擊  
  57.  NSLog(@"雙指雙擊");  
  58. }  
  59. }  
  下面這個比較全一下:
Java代碼    收藏代碼
  1. UIKit中包含了UIGestureRecognizer類,用於檢測發生在設備中的手勢。UIGestureRecognizer是一個抽象類,定義了全部手勢的基本行爲,它有下面一些子類用於處理具體的手勢:   
  2.   
  3.     1、拍擊UITapGestureRecognizer (任意次數的拍擊)    
  4.   
  5.     2、向裏或向外捏UIPinchGestureRecognizer (用於縮放)    
  6.   
  7.     3、搖動或者拖拽UIPanGestureRecognizer    
  8.   
  9.     4、擦碰UISwipeGestureRecognizer (以任意方向)    
  10.   
  11.     5、旋轉UIRotationGestureRecognizer (手指朝相反方向移動)    
  12.   
  13.     6、長按UILongPressGestureRecognizer   
  14.   
  15. 對於不一樣類型的手勢識別器,具備不一樣的配置屬性。好比UITapGestureRecognizer,能夠配置拍擊次數。界面接收到手勢以後,能夠發送一個消息,用於處理響應手勢動做後的任務。固然,不一樣的手勢識別器,發送的消息方法也會有所不一樣。下面列舉幾個具體示例代碼:   
  16.   
  17.    
  18.   
  19. 一個手指,拍擊兩次手勢  
  20.   
  21. // 建立一個手勢識別器  
  22.   
  23. UITapGestureRecognizer *oneFingerTwoTaps =  
  24.   
  25.   [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerTwoTaps)] autorelease];  
  26.   
  27. // Set required taps and number of touches  
  28.   
  29. [oneFingerTwoTaps setNumberOfTapsRequired:2];  
  30.   
  31. [oneFingerTwoTaps setNumberOfTouchesRequired:1];  
  32.   
  33. // Add the gesture to the view  
  34.   
  35. [[self view] addGestureRecognizer:oneFingerTwoTaps];  
  36.   
  37. 消息方法oneFingerTwoTaps  
  38.   
  39. - (void)oneFingerTwoTaps  
  40.   
  41. {  
  42.   
  43.   NSLog(@"Action: One finger, two taps");  
  44.   
  45. }  
  46.   
  47. 兩個手指,拍擊兩次手勢  
  48.   
  49.     UITapGestureRecognizer *twoFingersTwoTaps =  
  50.   
  51.       [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingersTwoTaps)] autorelease];  
  52.   
  53.     [twoFingersTwoTaps setNumberOfTapsRequired:2];  
  54.   
  55.     [twoFingersTwoTaps setNumberOfTouchesRequired:2];  
  56.   
  57.     [[self view] addGestureRecognizer:twoFingersTwoTaps];  
  58.   
  59. 消息方法twoFingersTwoTaps  
  60.   
  61.     - (void)twoFingersTwoTaps {  
  62.   
  63.       NSLog(@"Action: Two fingers, two taps");  
  64.   
  65.     }  
  66.   
  67.    
  68.   
  69. 一個手指向上、向下擦碰手勢  
  70.   
  71. // 向上擦碰  
  72.   
  73.     UISwipeGestureRecognizer *oneFingerSwipeUp =  
  74.   
  75.       [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeUp:)] autorelease];  
  76.   
  77.     [oneFingerSwipeUp setDirection:UISwipeGestureRecognizerDirectionUp];  
  78.   
  79.     [[self view] addGestureRecognizer:oneFingerSwipeUp];  
  80.   
  81.        
  82.   
  83.     - (void)oneFingerSwipeUp:(UISwipeGestureRecognizer *)recognizer  
  84.   
  85.     {  
  86.   
  87.       CGPoint point = [recognizer locationInView:[self view]];  
  88.   
  89.       NSLog(@"Swipe up - start location: %f,%f", point.x, point.y);  
  90.   
  91.     }  
  92.   
  93.     // 向下擦碰  
  94.   
  95.     UISwipeGestureRecognizer *oneFingerSwipeDown =  
  96.   
  97.       [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeDown:)] autorelease];  
  98.   
  99.     [oneFingerSwipeDown setDirection:UISwipeGestureRecognizerDirectionDown];  
  100.   
  101.     [[self view] addGestureRecognizer:oneFingerSwipeDown];  
  102.   
  103.        
  104.   
  105.     - (void)oneFingerSwipeDown:(UISwipeGestureRecognizer *)recognizer  
  106.   
  107.     {  
  108.   
  109.       CGPoint point = [recognizer locationInView:[self view]];  
  110.   
  111.       NSLog(@"Swipe down - start location: %f,%f", point.x, point.y);  
  112.   
  113.     }  
  114.   
  115.  旋轉手勢  
  116.   
  117. UIRotationGestureRecognizer *twoFingersRotate =  
  118.   
  119.   [[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingersRotate:)] autorelease];  
  120.   
  121. [[self view] addGestureRecognizer:twoFingersRotate];  
  122.   
  123.    
  124.   
  125. - (void)twoFingersRotate:(UIRotationGestureRecognizer *)recognizer  
  126.   
  127. {  
  128.   
  129.   // Convert the radian value to show the degree of rotation  
  130.   
  131.   NSLog(@"Rotation in degrees since last change: %f", [recognizer rotation] * (180 / M_PI));  
  132.   
  133. }  
  134.   
  135. 向裏或向外捏的手勢  
  136.   
  137. UIPinchGestureRecognizer *twoFingerPinch =  
  138.   
  139.   [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingerPinch:)] autorelease];  
  140.   
  141. [[self view] addGestureRecognizer:twoFingerPinch];  
  142.   
  143.    
  144.   
  145. - (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer  
  146.   
  147. {  
  148.   
  149.   NSLog(@"Pinch scale: %f", recognizer.scale);  
  150.   
  151. }  
相關文章
相關標籤/搜索