UIImageView響應點擊事件
-
- UIImageView *imageView = [self getYourImageView];
- imageView.image = [self getYourImage];
- imageView.userInteractionEnabled = YES;
- UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(yourHandlingCode:)];
- [imageView addGestureRecognizer:singleTap];
- [singleTap release];
UITapGestureRecognizer 的用法
- //單指單擊
- UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self
- action:@selector(handleSingleFingerEvent:)];
- singleFingerOne.numberOfTouchesRequired = 1; //手指數
- singleFingerOne.numberOfTapsRequired = 1; //tap次數
- singleFingerOne.delegate = self;
-
- //單指雙擊
- UITapGestureRecognizer *singleFingerTwo = [[UITapGestureRecognizer alloc] initWithTarget:self
- action:@selector(handleSingleFingerEvent:)];
- singleFingerTwo.numberOfTouchesRequired = 1;
- singleFingerTwo.numberOfTapsRequired = 2;
- singleFingerTwo.delegate = self;
- //雙指單擊
- UITapGestureRecognizer *doubleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self
- action:@selector(handleDoubleFingerEvent:)];
- doubleFingerOne.numberOfTouchesRequired = 2;
- doubleFingerOne.numberOfTapsRequired = 1;
- doubleFingerOne.delegate = self;
- UITapGestureRecognizer *doubleFingerTwo = [[UITapGestureRecognizer alloc] initWithTarget:self
- action:@selector(handleDoubleFingerEvent:)];
- doubleFingerTwo.numberOfTouchesRequired = 2;
- doubleFingerTwo.numberOfTapsRequired = 2;
- doubleFingerTwo.delegate = self;
- //若是不加下面的話,當單指雙擊時,會先調用單指單擊中的處理,再調用單指雙擊中的處理
- [singleFingerOne requireGestureRecognizerToFail:singleFingerTwo];
- //同理雙指亦是如此
- [doubleFingerOne requireGestureRecognizerToFail:doubleFingerTwo];
- [self.view addGestureRecognizer:singleFingerOne];
- [self.view addGestureRecognizer:singleFingerTwo];
- [self.view addGestureRecognizer:doubleFingerOne];
- [self.view addGestureRecognizer:doubleFingerTwo];
- [singleFingerOne release];
- [singleFingerTwo release];
- [doubleFingerOne release];
- [doubleFingerTwo release];
- 處理事件的方法,代碼:
- //處理單指事件
- - (void)handleSingleFingerEvent:(UITapGestureRecognizer *)sender
- {
- if (sender.numberOfTapsRequired == 1) {
- //單指單擊
- NSLog(@"單指單擊");
- }else if(sender.numberOfTapsRequired == 2){
- //單指雙擊
- NSLog(@"單指雙擊");
- }
- }
- //處理雙指事件
- - (void)handleDoubleFingerEvent:(UITapGestureRecognizer *)sender
- {
- if (sender.numberOfTapsRequired == 1) {
- //雙指單擊
- NSLog(@"雙指單擊");
- }else if(sender.numberOfTapsRequired == 2){
- //雙指雙擊
- NSLog(@"雙指雙擊");
- }
- }
下面這個比較全一下:
- UIKit中包含了UIGestureRecognizer類,用於檢測發生在設備中的手勢。UIGestureRecognizer是一個抽象類,定義了全部手勢的基本行爲,它有下面一些子類用於處理具體的手勢:
-
- 1、拍擊UITapGestureRecognizer (任意次數的拍擊)
-
- 2、向裏或向外捏UIPinchGestureRecognizer (用於縮放)
-
- 3、搖動或者拖拽UIPanGestureRecognizer
-
- 4、擦碰UISwipeGestureRecognizer (以任意方向)
-
- 5、旋轉UIRotationGestureRecognizer (手指朝相反方向移動)
-
- 6、長按UILongPressGestureRecognizer
-
- 對於不一樣類型的手勢識別器,具備不一樣的配置屬性。好比UITapGestureRecognizer,能夠配置拍擊次數。界面接收到手勢以後,能夠發送一個消息,用於處理響應手勢動做後的任務。固然,不一樣的手勢識別器,發送的消息方法也會有所不一樣。下面列舉幾個具體示例代碼:
-
-
-
- 一個手指,拍擊兩次手勢
-
- // 建立一個手勢識別器
-
- UITapGestureRecognizer *oneFingerTwoTaps =
-
- [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerTwoTaps)] autorelease];
-
- // Set required taps and number of touches
-
- [oneFingerTwoTaps setNumberOfTapsRequired:2];
-
- [oneFingerTwoTaps setNumberOfTouchesRequired:1];
-
- // Add the gesture to the view
-
- [[self view] addGestureRecognizer:oneFingerTwoTaps];
-
- 消息方法oneFingerTwoTaps
-
- - (void)oneFingerTwoTaps
-
- {
-
- NSLog(@"Action: One finger, two taps");
-
- }
-
- 兩個手指,拍擊兩次手勢
-
- UITapGestureRecognizer *twoFingersTwoTaps =
-
- [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingersTwoTaps)] autorelease];
-
- [twoFingersTwoTaps setNumberOfTapsRequired:2];
-
- [twoFingersTwoTaps setNumberOfTouchesRequired:2];
-
- [[self view] addGestureRecognizer:twoFingersTwoTaps];
-
- 消息方法twoFingersTwoTaps
-
- - (void)twoFingersTwoTaps {
-
- NSLog(@"Action: Two fingers, two taps");
-
- }
-
-
-
- 一個手指向上、向下擦碰手勢
-
- // 向上擦碰
-
- UISwipeGestureRecognizer *oneFingerSwipeUp =
-
- [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeUp:)] autorelease];
-
- [oneFingerSwipeUp setDirection:UISwipeGestureRecognizerDirectionUp];
-
- [[self view] addGestureRecognizer:oneFingerSwipeUp];
-
-
-
- - (void)oneFingerSwipeUp:(UISwipeGestureRecognizer *)recognizer
-
- {
-
- CGPoint point = [recognizer locationInView:[self view]];
-
- NSLog(@"Swipe up - start location: %f,%f", point.x, point.y);
-
- }
-
- // 向下擦碰
-
- UISwipeGestureRecognizer *oneFingerSwipeDown =
-
- [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeDown:)] autorelease];
-
- [oneFingerSwipeDown setDirection:UISwipeGestureRecognizerDirectionDown];
-
- [[self view] addGestureRecognizer:oneFingerSwipeDown];
-
-
-
- - (void)oneFingerSwipeDown:(UISwipeGestureRecognizer *)recognizer
-
- {
-
- CGPoint point = [recognizer locationInView:[self view]];
-
- NSLog(@"Swipe down - start location: %f,%f", point.x, point.y);
-
- }
-
- 旋轉手勢
-
- UIRotationGestureRecognizer *twoFingersRotate =
-
- [[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingersRotate:)] autorelease];
-
- [[self view] addGestureRecognizer:twoFingersRotate];
-
-
-
- - (void)twoFingersRotate:(UIRotationGestureRecognizer *)recognizer
-
- {
-
- // Convert the radian value to show the degree of rotation
-
- NSLog(@"Rotation in degrees since last change: %f", [recognizer rotation] * (180 / M_PI));
-
- }
-
- 向裏或向外捏的手勢
-
- UIPinchGestureRecognizer *twoFingerPinch =
-
- [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingerPinch:)] autorelease];
-
- [[self view] addGestureRecognizer:twoFingerPinch];
-
-
-
- - (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer
-
- {
-
- NSLog(@"Pinch scale: %f", recognizer.scale);
-
- }
歡迎關注本站公眾號,獲取更多信息