iOS 笔记五:手势识别 UIGestureRecognizer

手势识别
1.UIView中可以获取到触摸的原始事件,如touch down,moved, up.
也可以对特定的、已定义的“手势”作出反应。
手势由UIGestureRecognizer识别,这是个“抽象”类,我们只使用它的具体子类。

使用手势识别器(gesture recognizer)要注意2个方面:

A.向UIView中添加一个gesture recognizer,让UIView识别这个手势;

B.当手势发生时,提供一个处理方法(a method to handle)来处理这个事件(手势)。

通常#A有控制器完成,偶尔也会由UIView自己完成;
通常#B由UIView自己提供,但是由控制器提供也是合理的,
或者由控制器决定是否要以与UIView不同的方式来处理这个手势。

2.在Controller中向UIView添加手势识别器:
-(void)setPannableView:(UIView *)pannableView
{
 pannableView = pannableView;
 UIPanGestureRecongnizer * pangr = 
  [[UIPanGestureRecongnizer alloc] initWithTarget:pannableView action:@selector(pan:)];
 [pannableView addGestureRecongnizer:pangr];
}
A.UIPanGestureRecongnizer是UIGestureRecongnizer的一个子类,
识别"panning"(跟着手指的移动,移动UI元素).当然,还有很多其它的具体子类。

B.注意我们在initWithTarget消息中把pannableView作为处理pan手势的目标传给了手势识别器,
那么这个视图既是识别手势又处理手势。

C.UIView不需要一定要处理手势,也可以是Controller去处理。
View通常为修改视图的绘制而处理手势;Controller通常为修改Model而处理手势。

D.action:@selector pan:(注意冒号,表示带一个参数?), 当手势识别后需要处理时,
这个action方法将被发送给目标(target:pannableView).
这个版本的的action 消息需要一个参数(发送这个消息的UIGestureRecognizer),
也有不需要参数的另一个版本的方法。

E.如果不执行[pannableView addGestureRecognizer:pangr];这句话,
那么即使pannableView实现了pan:方法,它也不会被调用,
因为我们没有把手势识别器添加到视图的手势识别列表中。
可以把这个方法当作“打开手势识别处理”.

F.只有UIView实例可以识别手势(因为UIView处理所有的触摸输入).
但是任何对象都可以告诉UIView去识别一个手势(添加一个识别器到UIView中).
任何对象都可以处理手势识别结果(作为手势action的目标).


3.怎么实现一个手势识别器的目标处理方法(target:to handle action).
每个具体的手势识别器类都会提供一些方法来实现action处理,
如UIPanGestureRecognizer提供了3个方法:
-(CGPoint)translationInView:(UIView *)aView;
-(CGPoint)velocityInView:(UIView *)aView;
-(void)setTranslation:(CGPoint)translation inView:(UIView *)aView;
另外,基类UIGestureRecognizer提供了这个属性
@property (readonly) UIGestureRecognizerState state;
A.手势识别器开始时处于Possible状态,直到开始识别(触摸开始).
B.如果手势是不连续的,如点击,那么手势识别器进入到Rocognizer状态。
C.如果是连续的手势,如pan 手势,那么手势识别器进入到Began状态。
D.注意:任何时候都可能进入到Failed状态。
E.如果手势是连续的,那么接下来手势识别器会进入到Changed状态,
最后进入到Ended状态。
F.对于连续手势,如果手势识别器发现当前发生不是自己要识别的手势,
那么会进入到Cancelled状态。

那么有了以上方法和状态后,pan:手势处理方法怎么处理识别结果呢?
-(void)pan:(UIPanGestureRecognizer * )recognizer
{
 if((recognizer.state == UIGestureRecognizerStateChanged) ||
  (recognizer.state == UIGestureRecognizerStateEnded))
  {
   CGPoint translation = [recognizer translationInView:self];
   // move something in myself(I'm a UIView)by translation.x and translation.y
   // for example,if I were a graph and my origin was set by an @property called origin
   self.origin = CGPointMake(self.origin.x + translation.x, self.origin.y+translation.y);
   [recognizer setTranslation:CGPointZero inView:self];
  }
}

4.其它具体手势
A.UIPinchGestureRecognizer
@property CGFloat scale; // note that this is not readonly
@property (readonly) CGFloat velocity; // readonly;scale factor per second

B.UIRotateGestureRecognizer
@property CGFloat rotation; // in radians(弧度)
@property (readonly) CGFloat velocity; // readonly; radians per second

C.UISwipGestureRecognizer
设置以下参数来找出确切的滑动类型,然后找出识别状态
@property UISwipeGestureRecognizerDirection direction; // 
@property NSUInteger numberOfTouchesRequired; // e.g., require two finger tap?

D.UITapGestureRecognizer
@property NSUInteger numberOfTapsRequired; // 一次、两次还是三次点击?
@property NSUInteger numberOfTouchesRequired; //e.g., require two finger tap?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值