作者: 编程  发布:2019-11-29

最近做了一些关于 GestureRecognizer 的工作 ,随笔记录一些需要注意的点:

let panRecognizer = UIPanGestureRecognizer(target: self, action: #selector(URVIEWController.respondsToPenGesture))

iOS  Programming  UIGestureRecognizer and UIMenuController



A UIGestureRecognizer intercepts touches that are on their way to being handled by a view.

  1. PanGestureRecognizer  


一个UIGestureRecognizer拦截touches 在他们被一个view处理的路上。

(1)在使用时 注意在哪个view添加了 手势识别 

func respondsToPenGesture(sender: UIPanGestureRecognizer) {}

When it recognizes a particular gesture, it sends a message to the object of your choice.


if (sender.state == UIGestureRecognizerState.Began) {


不然会出现一些 不必要的识别操作和响应

startPanLocation = sender.locationInView(URVIEW)

There are several types of gesture recognizers built into the SDK.


} else if (sender.state == UIGestureRecognizerState.Changed) {


(2)如果想 把手势控制做成 slider : 

let stopLocation = sender.locationInView(URVIEW)

1  UIGestureRecognizer Subclasses


let abscissaChange = stopLocation.x - startPanLocation!.x

You do not instantiate UIGestureRecognizer itself. Instead, there are a number of subclasses of UIGestureRecognizer, and each one is responsible for recognizing a particular gesture.

I.  state :这里是自带的state有三个 began , changed , ended 

} else if (sender.state == UIGestureRecognizerState.Ended) {


  在began的时候存入 起始点的数据 ,这个点需要是个全局变量,如果是在began方法里设置了变量, 其他两个state的方法是取不到的。(

let stopLocation = sender.locationInView(URVIEW)

To use an instance of a UIGestureRecognizer subclass, you give it a target-action pair and attach it to a view.

startPanLocation = sender.locationInView(self.newEffectView)

let abscissaChange = stopLocation.x - startPanLocation!.x

为了实例化UIGestureRecognizer子类,你给了它一个target-action pair 并把它与一个view 联系在一起。


Whenever the gesture recognizer recognizes its gesture on the view, it will send the action message to its target.

  当changed的时候就存入变更点的数据 (

需要注意的是 startPanLocation 你需要存在一个全局变量里,因为在你state.changed时候,如果不是全剧变量你取不到starPanLocation的值。

当一个gesture recognizer 识别出了在view 上的手势,它将会发送动作信息给它的target。

let changeLocation = sender.locationInView(self.newEffectView)

剩下的事情就比较简单了 你获得了abscissaChange 同理也能简单的到竖直方向的变化值,用这个值来对于你想要改变的值做一个加减计算就可以轻松实现 Slider的功能。

- (void)action:(UIGestureRecognizer *)gestureRecognizer;

Prisma的slider的功能也应该是一个道理来实现的,在view上在设置一个UITextlabel 就能达到一摸一样的功效,(当然还要加上一个延迟消失的效果,可以用dispatch_after来实现)

When recognizing a gesture, the gesture recognizer intercepts the touches destined for the view

  当然ended的时候 也可以存入你需要的其他信息

Good Luck ,tlm .

当鉴别出一个手势后,它的gesture recognizer 为指定的view解析这个touches。

II.  变量转换问题 CGFloat, 如果想变成其他的数字类型, 先转成Float类型 在进行转换 不然得到的效果一般情况下 和你想要的不一样

Thus, a view with gesture recognizers may not receive the typical UIResponder messages like touchesBegian:withEvent:.

III. 要设定 endPoint - startPoint > 一个数据 ,不然识别度太高 会跟button的点按有效冲突

因此一个有gesture recognizers的view 可能不接受典型的UIResponder 消息,比如touchesBegian:withEvent图片 1

  1. swipeGesture 

2 Detecting Taps with UITapGestureRecognizer

(1)right 和 left , 如果不声明 默认是向右的 

The first UIGestureRecognizer subclass you will use is UITapGestureRecognizer.

(2)swipeGesture 没有可以state 所以不能做成slider



When the user taps the screen twice, all of the lines on the screen will be cleared.



In BNRDrawView.m, instantiate a UITapGestureRecognizer that requires two taps to fire in initWithFrame:.

UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self

action:@selector(doubleTap:)]; doubleTapRecognizer.numberOfTapsRequired = 2;

[self addGestureRecognizer:doubleTapRecognizer];


- (void)doubleTap:(UIGestureRecognizer *)gr

NSLog(@"Recognized Double Tap");

[self.linesInProgress removeAllObjects]; [self.finishedLines removeAllObjects]; [self setNeedsDisplay];


Notice that the argument to the action method for a gesture recognizer is the instance of UIGestureRecognizer that sent the message.

注意到一个gesture recognizer  的action方法的参数 是那个发送消息的UIGestureRecognizer实例。

In the case of a double tap, you do not need any information from the recognizer。

在这种两个tap 的情况下,你不需要从recognizer的任何消息。


Gesture recognizers work by inspecting touch events to determine if their particular gesture occurred. Before a gesture is recognized, all UIResponder messages will be delivered to a view as normal.

Before a gesture is recognized, all UIResponder messages will be delivered to a view as normal.


Since a tap gesture recognizer is recognized when a touch begins and ends within a small area in a small amount of time, the UITapGestureRecognizer cannot claim the touch is a tap just yet and touchesBegan:withEvent: is sent to the view.

当一个touch 开始和结束在很短的一段时间内完成时,一个tap gesture recognizer 被识别,UITapGestureRecognizer 不能声明这个touch 就是一个tap,touchesBegan:withEvent:也被发送给这个view。

When the tap is finally recognized, the gesture recognizer claims the touch involved in the tap for itself and no more UIResponder messages will be sent to the view for that particular touch.

一旦一个tap 最终被识别,gesture recognizer 将会声明包含在这个tap 中的touch 。对于这个特殊的touch也不会再发送给这个view 。

In order to communicate this touch take-over to the view, touchesCancelled:withEvent: is sent to the view and the NSSet of touches contains that UITouch instance.

为了与这个touch交流,touchesCancelled:withEvent将会被发送给这个view ,以及包含这个UITouch 实例的NSSet。

To prevent this red dot from appearing temporarily, you can tell a UIGestureRecognizer to delay the sending of touchesBegan:withEvent: to its view if it is still possible for the gesture to be recognized.



doubleTapRecognizer.delaysTouchesBegan = YES;


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



        [self addGestureRecognizer:doubleTapGestureRecognizer];



    NSLog(@"Recognizer Double Tap");

    [self.linesInProgress removeAllObjects];

    [self.finishedLines removeAllObjects ];

    [self setNeedsDisplay];



3.multiple gesture Recognizers 

add another gesture recognizer that allows the user to select a line.


In situations where you have multiple gesture recognizers, it is not uncommon to have a gesture recognizer fire when you really want another gesture recognizer to handle the work.

当你有多个gesture recognizers时,经常看到让一个recognizer 发出激活 当你实际上想让另一个gesture recognizer 来处理这个工作


In these cases, you set up dependencies between recognizers that say, "Just wait a moment before you fire, because this gesture might be mine!"


In initWithFrame:, make it so the tapRecognizer must wait for the doubleTapRecognizer to fail before it can assume that a single tap is not just the first of a double tap.

在它假设是一个single tap而不是一个double tap 的第一个之前,tapRecognizer需要等待doubleTapRecognizer失败。


Now, let's build on the BNRDrawView so that the user can select lines when they are tapped. First, add a property to hold onto a selected line to the class extension in BNRDrawView.m.

@property (nonatomic, weak) BNRLine *selectedLine;


The point you are interested in, of course, is where the tap occurred. You can easily get this information.

你感兴趣的位置应该是tap 的位置。你可以轻松的得到这个消息。

Every UIGestureRecognizer has a locationInView: method. Sending this message to the gesture recognizer will give you the coordinate where the gesture occurred in the coordinate system of the view that is passed as the argument.

每一个UIGestureRecognizer有一个locationInView方法。把这个信息给gesture recognizer将会给你提供你手势发生的在给定view 坐标系中得坐标。


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


        [singleTapGestureRecognizer requireGestureRecognizerToFail:doubleTapGestureRecognizer];

        [self addGestureRecognizer:singleTapGestureRecognizer];



    NSLog(@"Recongizer Single Tap");

    CGPoint point=[gestureRecognzier locationInView:self];

    self.selectedLine=[self lineAtPoint:point];

    [Prisma的slider的功能也应该是一个道理来实现的,为了实例化UIGestureRecognizer子类。self setNeedsDisplay];




   if (self.selectedLine) {

        [[UIColor greenColor] set];

        [self strokeLine:self.selectedLine];




-(LKLine*)lineAtPoint:(CGPoint) point{

    for (LKLine *line in self.finishedLines) {

        CGPoint start=line.begin;

        CGPoint end=line.end;


        for (float t=0.0 ; t<=1.0; t+=0.5) {

            float x=start.x+t*(end.x-start.x);

            float y=start.y+t*(end.y-start.y);


            if (hypot(x-point.x, y-point.y)<20.0) {

                return line;




    return nil;


  1. UIMenuController 

a menu appears right where the user tapped that offers the option to delete that line.


There is a built-in class for providing this sort of menu called UIMenuController


A menu controller has a list of UIMenuItem objects and is presented in an existing view.

一个menu Controller 有一列UIMenuItem对象,被展现在已经存在的view 上。 

Each item has a title (what shows up in the menu) and an action (the message it sends the first responder of the window)

每个item 有一个标题(显示在menu上)和一个action (消息它会发送给window 的first responder)图片 2

There is only one UIMenuController per application.

每个应用只有一个UIMenuController 。

When you wish to present this instance, you fill it with menu items, give it a rectangle to present from, and set it to be visible.

当你希望展现这个实例时,你用menu items 填充它,给它一个矩形来展现并设置它可以看见。


Do this in BNRDrawView.m's tap: method if the user has tapped on a line. If the user tapped somewhere that is not near a line, the currently selected line will be deselected, and the menu controller will hide.



For a menu controller to appear, a view that responds to at least one action message in the UIMenuController's menu items must be the first responder of the window – this is why you sent the message becomeFirstResponder to the BNRDrawView before setting up the menu controller.

为了让一个menu Controller 出现,那个至少响应一个action信息的UIMenuController的菜单item 的view 将会称为first responder of the  window .

If you have a custom view class that needs to become the first responder, you must override canBecomeFirstResponder. In BNRDrawView.m, override this method to return YES.

如果你有一个custom view 类需要称为first responder ,你必须重写canBecomeFirstResponder方法。

- (BOOL)canBecomeFirstResponder

return YES;




When being presented, the menu controller goes through each menu item and asks the first responder if it implements the action message for that item.

当将被展现时,menu controller 遍历每个menu item ,询问first responder 看看是否实现了那个item 的action message .

If the first responder does not implement that method, then the menu controller will not show the associated menu item.

如果first responder 没有实现那个方法,那么menu controller 将不会显示相关的menu item.

If no menu items have their action messages implemented by the first responder, the menu is not shown at all.

如果没有menu items 有他们的action 信息被first responder 实现,它的menu 将根本不会出现。


 if (self.selectedLine) {

        [self becomeFirstResponder];

        UIMenuController *menuController=[UIMenuController sharedMenuController];

        UIMenuItem *deleteMenuItem=[[UIMenuItem alloc] initWithTitle:@"Delete" action:@selector(deleteLine:)];



        [menuController setTargetRect:CGRectMake(point.x, point.y, 2, 2) inView:self];

        [menuController setMenuVisible:YES animated:YES];


        [[UIMenuController sharedMenuController ] setMenuVisible:NO animated:YES];




    return YES;



    [self.finishedLines removeObject:self.selectedLine];

    [self setNeedsDisplay];



5 UILongPressGestureRecognizer

When you hold down on a line (a long press), that line will be selected and you can then drag it around by dragging your finger (a pan).

let's focus on the long press recognizer. In BNRDrawView.m, instantiate a UILongPressGestureRecognizer in initWithFrame: and add it to the BNRDrawView.


UILongPressGestureRecognizer *pressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self

action:@selector(longPress:)]; [self addGestureRecognizer:pressRecognizer];

Now when the user holds down on the BNRDrawView, the message longPress: will be sent to it. By default, a touch must be held 0.5 seconds to become a long press, but you can change the minimumPressDuration of the gesture recognizer if you like.

现在你可以长按,那么longPress 消息就会被发送。默认情况下,一个touch必须保持0.5秒,但是你能够改变手势识别的最小时间。

A tap is a simple gesture. By the time it is recognized, the gesture is over, and the action message has been delivered.


A long press, on the other hand, is a gesture that occurs over time and is defined by three separate events.


when the user touches a view, the long press recognizer notices a possible long press but must wait to see whether the touch is held long enough to become a long press gesture.

当一个用户触摸一个view,long press recognizer 注意到一个可能的long press,但是必须等到看看这个touch 是否维持了做够长的时间成为一个long press gesture .


Once the user holds the touch long enough, the long press is recognized and the gesture has begun. When the user removes the finger, the gesture has ended.

一旦用户hold 足够长时间的touch,long press 就会被识别,gesture 便开始了,当用户移除手指,这个手势就结束了。 

Each of these events causes a change in the gesture recognizer's state property.

这些时间的每一过程都会导致一个gesture recognzier的状态属性的改变。

the state of the long press recognizer described above would be UIGestureRecognizerStatePossible, then UIGestureRecognizerStateBegan, and finally UIGestureRecognizerStateEnded.

long press recognizer的状态可能是UIGestureRecognizerStatePossible,接着UIGestureRecognizerStateBegan,最后UIGestureRecognizerStateEnded。

When a gesture recognizer transitions to any state other than the possible state, it sends its action message to its target.

当处理识别到possible state,gesture recognizer 将会发送一个action 消息给target。 

This means the long press recognizer's target receives the same message when a long press begins and when it ends.

也就是说当 a long press开始和结束的时候,long press recognizer's target都会接受到相同的信息。

Here is the plan for implementing your action method longPress:. When the view receives longPress: and the long press has begun, you will select the closest line to where the gesture occurred.


This allows the user to select a line while keeping the finger on the screen (which is important in the next section when you implement panning).


When the view receives longPress: and the long press has ended, you will deselect the line.

当view 收到longPress 消息时,long press 结束的时候,你会取消选择那个line。



    if (gestureRecognzier.state==UIGestureRecognizerStateBegan) {

        CGPoint point=[gestureRecognzier locationInView:self];

        self.selectedLine=[self lineAtPoint:point];


        if (self.selectedLine) {

            [self.linesInProgress removeAllObjects];


    }else if( gestureRecognzier.state==UIGestureRecognizerStateEnded ){



    [self setNeedsDisplay];



  UILongPressGestureRecognizer *longPressGestureRecognizer=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];

        [self addGestureRecognizer:longPressGestureRecognizer];

6 UIPanGestureRecognizer and Simultaneous Recognizers

So you need a gesture recognizer for a finger moving around the screen.

为了手指能在屏幕上移动,你需要一个gesture recognizer .

This gesture is called panning, and its gesture recognizer subclass is UIPanGestureRecognizer.

这个手势成为panning。 它的gesture recognizer subclass 是UIPanGestureRecognizer。

Normally, a gesture recognizer does not share the touches it intercepts.

一般的,gesture recognizer并不分享这些被拦截的touches。

Once it has recognized its gesture, it "eats" that touch, and no other recognizer gets a chance to handle it.

一旦它被识别它的手势,它会"吃进"这个touch,没有其他的recognizer 有机会处理它。

In your case, this is bad: the entire pan gesture you want to recognize happens within a long press gesture.

在这种情况下,非常不好:真个的pan gesture 你想识别的, 整个的发生在long press gesture下。

You need the long press recognizer and the pan recognizer to be able to recognize their gestures simultaneously.

你需要long press recognizer and the pan recognizer能够同时识别出他们的手势。


First, in the class extension in BNRDrawView.m, declare that BNRDrawView conforms to the UIGestureRecognizerDelegate protocol. Then, declare a UIPanGestureRecognizer as a property so that you have access to it in all of your methods.

@interface BNRDrawView () <UIGestureRecognizerDelegate>

@property (nonatomic, strong) UIPanGestureRecognizer *moveRecognizer;


add code to initWithFrame: to instantiate a UIPanGestureRecognizer, set two of its properties, and attach it to the BNRDrawView.


self.moveRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveLine:)];

self.moveRecognizer.delegate = self; self.moveRecognizer.cancelsTouchesInView = NO; [self addGestureRecognizer:self.moveRecognizer];


There are a number of methods in the UIGestureRecognizerDelegate protocol, but you are only interested in one – gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:.

A gesture recognizer will send this message to its delegate when it recognizes its gesture but realizes that another gesture recognizer has recognized its gesture, too.

一个gesture recognizer 将会发送

gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer消息给它的委托当它识别出它的手势但是也了解到另一个gesture recognize 也已经识别出这个手势了。

If this method returns YES, the recognizer will share its touches with other gesture recognizers.

如果这个手势返回YES,那么recognizer 将会分享它的touches 和其他的gesture recognizers .


- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:

(UIGestureRecognizer *)other

if (gestureRecognizer == self.moveRecognizer) {

return YES; }

return NO; }


Now when the user begins a long press, the UIPanGestureRecognizer will be allowed to keep track of this finger, too.


When the finger begins to move, the pan recognizer will transition to the began state.

如果手指开始移动,pan recognizer将会转成began state。


If these two recognizers could not work simultaneously, the long press recognizer would start, and the pan recognizer would never transition to the began state or send its action message to its target.


a pan gesture recognizer supports the changed state.

一个pan gesture recognizer 支持改变state。

When a finger starts to move, the pan recognizer enters the began state and sends a message to its target.

当手指开始移动时, pan recognizer  进入began state并发送一个消息给它的target。

While the finger moves around the screen, the recognizer transitions to the changed state and sends its action message to its target repeatedly.

当手指在屏幕上移动时,recognizer 转移成 changed state 并反复的 发送action message 到target 。 

Finally, when the finger leaves the screen, the recognizer's state is set to ended, and the final message is delivered to the target.

最后 当手指离开屏幕时,recognzier 的state 设置为ended 。最后的消息被发送给target。


Now you need to implement the moveLine: method that the pan recognizer sends its target.


In this implementation, you will send the message translationInView: to the pan recognizer.

在这个实现中,你将发送消息 translationInView给pan recognizer.


This UIPanGestureRecognizer method returns how far the pan has moved as a CGPoint in the coordinate system of the view passed as the argument.

UIPanGestureRecognizer方法会返回这个pan 作为CGPoint 在给定参数的view中得坐标系中移动了多少。 

When the pan gesture begins, this property is set to the zero point (where both x and y equal zero).

当pan gesture 开始的时候 ,这个属性设置为 zero point。

As the pan moves, this value is updated – if the pan goes very far to the right, it has a high x value; if the pan returns to where it began, its translation goes back to the zero point.

如果pan 移动了,这个value就会被更新。


Notice that because you will send the gesture recognizer a method from the UIPanGestureRecognizer class, the parameter of this method must be a pointer to an instance of UIPanGestureRecognizer rather than UIGestureRecognizer.




- (void)moveLine:(UIPanGestureRecognizer *)gr

// If we have not selected a line, we do not do anything here if (!self.selectedLine) {

return; }

// When the pan recognizer changes its position...
if (gr.state == UIGestureRecognizerStateChanged) {

// How far has the pan moved?
CGPoint translation = [gr translationInView:self];

// Add the translation to the current beginning and end points of the line CGPoint begin = self.selectedLine.begin;

CGPoint end = self.selectedLine.end; begin.x += translation.x;
begin.y += translation.y;
end.x += translation.x;

end.y += translation.y;

// Set the new beginning and end points of the line self.selectedLine.begin = begin; self.selectedLine.end = end;

// Redraw the screen

[self setNeedsDisplay]; }



because you are adding the current translation over and over again to the line's original end points.

你在添加现在的translation 一遍又一遍的到直线最开始的位置。

You really need the gesture recognizer to report the change in translation since the last time this method was called instead.

你其实需要gesture recognizer 报告自从上次方法被调用后在translation  的变化.

You can set the translation of a pan gesture recognizer back to the zero point every time it reports a change.

你可以在每次它报告一个change后可以设置pan gesture recognzier 的translation 返回到零。

        [panGestureRecognizer setTranslation:CGPointZero inView:self];


let's take a look at a property you set in the pan gesture recognizer – cancelsTouchesInView.

让我们看一看你设置的pan gesture recognizer 的属性cancelsTouchesInView.

Every UIGestureRecognizer has this property and, by default, this property is YES.

This means that the gesture recognizer will eat any touch it recognizes so that the view will not have a chance to handle it via the traditional UIResponder methods, like touchesBegan:withEvent:.

这就意味 gesture recognizer 会吃进任何它识别的touch,所以view 将不会有机会通过传统的UIResponder 方法像touchesBegan:withEvent处理了。

Usually, this is what you want, but not always.



When you set cancelsTouchesInView to NO, touches that the gesture recognizer recognizes also get delivered to the view via the UIResponder methods. This allows both the recognizer and the view's UIResponder methods to handle the same touches.











上一篇:Python 面向对象介绍