IOS开发之手势——UIGestureRecognizer 共存


IOS开发之手势——UIGestureRecognizer 共存

 在 iPhone 或 iPad 的开发中,除了用 touchesBegan / touchesMoved / touchesEnded 这组方法来控制使用者的手指触控外,也可以用 UIGestureRecognizer 的衍生类別来进行判断。用 UIGestureRecognizer 的好处在于有现成的手势,开发者不用自己计算手指移动轨迹。UIGestureRecognizer的衍生类別有以下几种:

  • UITapGestureRecognizer
  • UIPinchGestureRecognizer
  • UIRotationGestureRecognizer
  • UISwipeGestureRecognizer
  • UIPanGestureRecognizer
  • UILongPressGestureRecognizer


从命名上不难了解這些类別所对应代表的手势,分別是 Tap(点一下)、Pinch(二指往內或往外拨动)、Rotation(旋转)、Swipe(滑动,快速移动)、Pan (拖移,慢速移动)以及 LongPress(长按)。這些手势別在使用上也很简单,只要在使用前定义并添加到对应的视图上即可。

复制代码
复制代码
// 定义一个 recognizer, 并加到需要偵測该手势的 UIView 元件上
- (void)viewDidLoad {
UISwipeGestureRecognizer* recognizer;
// handleSwipeFrom 是偵測到手势,所要呼叫的方法
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:selfaction:@selector(handleSwipeFrom)];
// 不同的 Recognizer 有不同的实体变数
// 例如 SwipeGesture 可以指定方向
// 而 TapGesture 則可以指定次數
recognizer.direction = UISwipeGestureRecognizerDirectionUp
[self.view addGestureRecognizer:recognizer];
[recognizer release];
}

- (void)handleSwipeFrom:(UISwipeGestureRecognizer*)recognizer {
// 触发手勢事件后,在这里作些事情

// 底下是刪除手势的方法
[self.view removeGestureRecognizer:recognizer];
}
复制代码
复制代码

问题來了。有些手势其实是互相关联的,例如 Tap 与 LongPress、Swipe与 Pan,或是 Tap 一次与Tap 兩次。当一個 UIView 同时添加兩个相关联的手势时,到底我这一下手指头按的要算是 Tap 还是 LongPress?如果照預设作法来看,只要「先滿足条件」的就会跳出并呼叫对应方法,举例来说,如果同时注册了 Pan 和 Swipe,只要手指头一移动就会触发 Pan 然后跳出,因而永远都不會发生 Swipe;单点与双点的情形也是一样,永远都只会触发单点,不會有双点。

那么这个问题有解吗?答案是肯定的,UIGestureRecognizer 有个方法叫做requireGestureRecognizerToFail,他可以指定某一个 recognizer,即便自己已经滿足條件了,也不會立刻触发,会等到该指定的 recognizer 确定失败之后才触发。以同时支持单点与双点的手势为例,代码如下:

复制代码
- (void)viewDidLoad {
// 单击的 Recognizer
UITapGestureRecognizer* singleRecognizer;
singleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:selfaction:@selector(handleSingleTapFrom)];
singleTapRecognizer.numberOfTapsRequired = 1; // 单击
[self.view addGestureRecognizer:singleRecognizer];

// 双击的 Recognizer
UITapGestureRecognizer* double;
doubleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:selfaction:@selector(handleDoubleTapFrom)];
doubleTapRecognizer.numberOfTapsRequired = 2; // 双击
[self.view addGestureRecognizer:doubleRecognizer];

// 关键在这一行,如果双击确定偵測失败才會触发单击
[singleRecognizer requireGestureRecognizerToFail:doubleRecognizer];
[singleRecognizer release];
[doubleRecognizer release];
}




用UIGestureRecognizer侦测使用者输入操作

    在3.2以前,我们要拿到UITouch跟使用者互动,大部分都是透过UIResponder的四种methods

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
 - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

    有些人会把UITouch重新wrap丢到自己的queue里面去处理,不然就是直接在这几个function里直接判断。其实都不会差太多。简单的说,就是麻烦

    3.2以后,透过UIGestureRecognizer及其它继承它的UIxxxGestureRecognizer,侦测使用者输入就变的简单许多。

 UILongPressGestureRecognizer
 UIPanGestureRecognizer
 UIPinchGestureRecognizer
 UIRotationGestureRecognizer
 UISwipeGestureRecognizer
 UITapGestureRecognizer

    照名字看大概就知道这是做什么用的,所以就不解释了,直接看用法:

    以UIPanGestureRecognizer为例,这是处理使用者用一只手指(或多只)在屏幕上滑来滑去的动作。要侦测这个动作,只要加下面这段code进viewDidLoad或任何你需要的地方

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanFrom:)];
[self.view addGestureRecognizer:panRecognizer];
panRecognizer.maximumNumberOfTouches = 1;
panRecognizer.delegate = self;
[panRecognizer release];

    第 一个很简单,就是确定要给这个recognizer handle的event,就会去call这个class底下的handlePanFrom: 然后把recognizer加进UIView(addGestureRecognizer)。因为同时间我只想知道一只手指的动作,所以我用 maximumNumberOfTouches=1来限制。

    当然,你可以改变maximumNumberOfTouches跟minimumNumberOfTouches的值来当成filter,接着把 delegate设定成自己(记得header要加上UIGestureRecognizerDelegate)。

    不过这样还没有结束,我们要补上这个delegate method

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {

    里面可以先filter event,决定要不要丢给一开始assign给panRecognizer的selector function,
譬如我只想要看某个subview的事件

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    UIView *aview = [self.view viewWithTag:1000];
    if (touch.view != aview) {
      return NO; // 不理這個event
    }
    return YES;
 }

    接下来就是

- (void)handlePanFrom:(UIPanGestureRecognizer *)recognizer {        
         //拿到手指目前的位置
         CGPoint location = [recognizer locationInView:self.view];
         UIView *aview = [self.view viewWithTag:1000];

         // 如果UIGestureRecognizerStateEnded的話...你是拿不到location的
         // 不判斷的話,底下改frame會讓這個subview消失,因為origin的x和y就不見了!!!
         if(recognizer.state != UIGestureRecognizerStateEnded)
         {
                 aview.frame = CGRectMake(location.x, location.y, aview.frame.size.width, aview.frame.size.height);
         }
 }

    不 同的UIGestureRecognizer subclass都会有不同特点。譬如说Pinch的scale、velocity和Swipe的direction,直接简化了处理UITouch的步 骤。大家只要知道这些特点,处理使用者输入就会得心应手了。遇到问题记得先看看有没有判断UIGestureRecognizer的state。

UITapGestureRecognizerUIPanGestureRecognizer应用:
    
一:UITapGestureRecognizer应用:

- (void)singleOne:(UITapGestureRecognizer *)sender { }//将相应事件发生时的动作写入函数

- (void)singleTwo:(UITapGestureRecognizer *)sender { }

- (void)doubleOne:(UITapGestureRecognizer *)sender { }

- (void)doubleTwo:(UITapGestureRecognizer *)sender { }

UITapGestureRecognizer *singleOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleOne:)];  

singleOne.numberOfTouchesRequired = 1//触摸点个数,另作:[singleOne setNumberOfTouchesRequired:1];

singleOne.numberOfTapsRequired = 1//点击次数,另作:[singleOne setNumberOfTapsRequired:1];


UITapGestureRecognizer *singleTwo = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTwo:)];  

singleTwo.numberOfTouchesRequired = 2

singleTwo.numberOfTapsRequired = 1


UITapGestureRecognizer *doubleOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleOne:)];  

doubleOne.numberOfTouchesRequired = 1

doubleOne.numberOfTapsRequired = 2


UITapGestureRecognizer *doubleTwo = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTwo:)];  

doubleTwo.numberOfTouchesRequired = 2

doubleTwo.numberOfTapsRequired = 2;


[singleOne requireGestureRecognizerToFail:doubleOne]; //防止:双击被单击拦截[singleTwo requireGestureRecognizerToFail:doubleTwo]; //防止:双击被单击拦截


[self.view addGestureRecognizer:singleOne];  

[self.view addGestureRecognizer:singleTwo];

[self.view addGestureRecognizer:doubleOne];

[self.view addGestureRecognizer:doubleTwo];

[singleOne release];

[singleTwo release];

[doubleOne release];

[doubleTwo release];


uiimageview 点击事件 UITapGestureRecognizer
2012-05-22 14:22

 1.绑定点击事件 

 [BTBooksetUserInteractionEnabled:YES]; 

    UITapGestureRecognizer *singleTap3 = [[UITapGestureRecognizerallocinitWithTarget:selfaction:@selector(GoThisPic:)];

    [BTBookaddGestureRecognizer:singleTap3];

    [singleTap3 release];



2,获得事件源

-(void)GoThisPic:(UIGestureRecognizer *)gestureRecognizer

{

     UIImageView *view = [gestureRecognizer view];

    

//  [BigScrollsetContentOffset:CGPointMake(1024*view.tag,0animated:YES ];

}



----------------------------------------------



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); 
 
}


注意 注意-------------------------------------------------------------------------------------

为什么我设置了UITapGestureRecognizer以后我的UIImageView还是无法响应touch事件?

如题,我的代码如下:
- (void)viewDidLoad {
. . . . . . . .
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
[singleTap setNumberOfTapsRequired:1];
singleTap.delegate=self;
[startView addGestureRecognizer:singleTap];//startView 是一个UIImageView的实例,已经在Interface Builder中连接过了
[singleTap release];
}

设置ImageView的User Interaction Enabled属性和Multiple Touch属性才能接受到touch事件


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值