iOS? PRogramming? UIGestureRecognizer and UIMenuController
A UIGestureRecognizer intercepts touches that are on their way to being handled by a view.
一個(gè)UIGestureRecognizer攔截touches 在他們被一個(gè)view處理的路上。
When it recognizes a particular gesture, it sends a message to the object of your choice.
當(dāng)它檢測(cè)到一個(gè)特殊的手勢(shì)時(shí),它就會(huì)發(fā)送一個(gè)消息給你選中的特殊的對(duì)象。
There are several types of gesture recognizers built into the SDK.
在內(nèi)部的SDK中,有一些特殊的手勢(shì)類(lèi)型。
1 ?UIGestureRecognizer Subclasses
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.
你并不會(huì)實(shí)例化UIGestureRecognizer自身,而是有一些UIGestureRecognizer的子類(lèi),這些子類(lèi)負(fù)責(zé)鑒別一個(gè)特殊的手勢(shì)。
To use an instance of a UIGestureRecognizer subclass, you give it a target-action pair and attach it to a view.
為了實(shí)例化UIGestureRecognizer子類(lèi),你給了它一個(gè)target-action pair 并把它與一個(gè)view 聯(lián)系在一起。
Whenever the gesture recognizer recognizes its gesture on the view, it will send the action message to its target.
當(dāng)一個(gè)gesture recognizer 識(shí)別出了在view 上的手勢(shì),它將會(huì)發(fā)送動(dòng)作信息給它的target。
- (void)action:(UIGestureRecognizer *)gestureRecognizer;
When recognizing a gesture, the gesture recognizer intercepts the touches destined for the view
當(dāng)鑒別出一個(gè)手勢(shì)后,它的gesture recognizer 為指定的view解析這個(gè)touches。
Thus, a view with gesture recognizers may not receive the typical UIResponder messages like touchesBegian:withEvent:.
因此一個(gè)有g(shù)esture recognizers的view 可能不接受典型的UIResponder 消息,比如touchesBegian:withEvent
2 Detecting Taps with UITapGestureRecognizer
The first UIGestureRecognizer subclass you will use is UITapGestureRecognizer.
第一個(gè)你將會(huì)使用的UIGestureRecognizer子類(lèi)是UITapGestureRecognizer。
When the user taps the screen twice, all of the lines on the screen will be cleared.
當(dāng)用戶點(diǎn)擊屏幕兩次的時(shí)候,所有的線就會(huì)被清除掉。
?
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.
注意到一個(gè)gesture recognizer? 的action方法的參數(shù) 是那個(gè)發(fā)送消息的UIGestureRecognizer實(shí)例。
In the case of a double tap, you do not need any information from the recognizer。
在這種兩個(gè)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.
當(dāng)一個(gè)touch 開(kāi)始和結(jié)束在很短的一段時(shí)間內(nèi)完成時(shí),一個(gè)tap gesture recognizer 被識(shí)別,UITapGestureRecognizer 不能聲明這個(gè)touch 就是一個(gè)tap,touchesBegan:withEvent:也被發(fā)送給這個(gè)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.
一旦一個(gè)tap 最終被識(shí)別,gesture recognizer 將會(huì)聲明包含在這個(gè)tap 中的touch 。對(duì)于這個(gè)特殊的touch也不會(huì)再發(fā)送給這個(gè)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.
為了與這個(gè)touch交流,touchesCancelled:withEvent將會(huì)被發(fā)送給這個(gè)view ,以及包含這個(gè)UITouch 實(shí)例的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.
為了阻止這個(gè)紅色的點(diǎn)臨時(shí)出現(xiàn),你能告訴UIGestureRecognizer延遲發(fā)送touchesBegan:withEvent給它的view,如果它仍然可能手勢(shì)被識(shí)別。
?
doubleTapRecognizer.delaysTouchesBegan = YES;
?
?UITapGestureRecognizer *doubleTapGestureRecognizer=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
? ? ? ? doubleTapGestureRecognizer.numberOfTapsRequired=2;
? ? ? ? doubleTapGestureRecognizer.delaysTouchesBegan=YES;
? ? ? ? [self addGestureRecognizer:doubleTapGestureRecognizer];
?
-(void)doubleTap:(UIGestureRecognizer*)gestureRecognzier{
? ? 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.
當(dāng)你有多個(gè)gesture recognizers時(shí),經(jīng)常看到讓一個(gè)recognizer 發(fā)出激活 當(dāng)你實(shí)際上想讓另一個(gè)gesture recognizer 來(lái)處理這個(gè)工作
?
In these cases, you set up dependencies between recognizers that say, "Just wait a moment before you fire, because this gesture might be mine!"
在這種情況下,你在兩個(gè)recognzier之間設(shè)置依賴關(guān)系:"在你fire之前先等一下,因?yàn)檫@個(gè)手勢(shì)可能是我的。"
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.
在它假設(shè)是一個(gè)single tap而不是一個(gè)double tap 的第一個(gè)之前,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.
你感興趣的位置應(yīng)該是tap 的位置。你可以輕松的得到這個(gè)消息。
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.
每一個(gè)UIGestureRecognizer有一個(gè)locationInView方法。把這個(gè)信息給gesture recognizer將會(huì)給你提供你手勢(shì)發(fā)生的在給定view 坐標(biāo)系中得坐標(biāo)。
?
UITapGestureRecognizer *singleTapGestureRecognizer=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)];
? ? ? ? singleTapGestureRecognizer.delaysTouchesBegan=YES;
? ? ? ? [singleTapGestureRecognizer requireGestureRecognizerToFail:doubleTapGestureRecognizer];
? ? ? ? [self addGestureRecognizer:singleTapGestureRecognizer];
?
-(void)singleTap:(UIGestureRecognizer*)gestureRecognzier{
? ? NSLog(@"Recongizer Single Tap");
? ? CGPoint point=[gestureRecognzier locationInView:self];
? ? self.selectedLine=[self lineAtPoint:point];
? ? [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;
}
4. 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
有一個(gè)內(nèi)部構(gòu)建的類(lèi)來(lái)提供這種菜單,稱為UIMenuController
A menu controller has a list of UIMenuItem objects and is presented in an existing view.
一個(gè)menu Controller 有一列UIMenuItem對(duì)象,被展現(xiàn)在已經(jīng)存在的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)
每個(gè)item 有一個(gè)標(biāo)題(顯示在menu上)和一個(gè)action (消息它會(huì)發(fā)送給window 的first responder)
There is only one UIMenuController per application.
每個(gè)應(yīng)用只有一個(gè)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.
當(dāng)你希望展現(xiàn)這個(gè)實(shí)例時(shí),你用menu items 填充它,給它一個(gè)矩形來(lái)展現(xiàn)并設(shè)置它可以看見(jiàn)。
?
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.
為了讓一個(gè)menu Controller 出現(xiàn),那個(gè)至少響應(yīng)一個(gè)action信息的UIMenuController的菜單item 的view 將會(huì)稱為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.
如果你有一個(gè)custom view 類(lèi)需要稱為first responder ,你必須重寫(xiě)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.
當(dāng)將被展現(xiàn)時(shí),menu controller 遍歷每個(gè)menu item ,詢問(wèn)first responder 看看是否實(shí)現(xiàn)了那個(gè)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 沒(méi)有實(shí)現(xiàn)那個(gè)方法,那么menu controller 將不會(huì)顯示相關(guān)的menu item.
If no menu items have their action messages implemented by the first responder, the menu is not shown at all.
如果沒(méi)有menu items 有他們的action 信息被first responder 實(shí)現(xiàn),它的menu 將根本不會(huì)出現(xiàn)。
?
?if (self.selectedLine) {
? ? ? ? [self becomeFirstResponder];
? ? ? ? UIMenuController *menuController=[UIMenuController sharedMenuController];
? ? ? ? UIMenuItem *deleteMenuItem=[[UIMenuItem alloc] initWithTitle:@"Delete" action:@selector(deleteLine:)];
? ? ? ? menuController.menuItems=@[deleteMenuItem];
?? ? ? ??
? ? ? ? [menuController setTargetRect:CGRectMake(point.x, point.y, 2, 2) inView:self];
? ? ? ? [menuController setMenuVisible:YES animated:YES];
? ? }else{
? ? ? ? [[UIMenuController sharedMenuController ] setMenuVisible:NO animated:YES];
? ? }
?
-(BOOL)canBecomeFirstResponder{
? ? return YES;
}
-(void)deleteLine:(id)sender{
? ? [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.
現(xiàn)在你可以長(zhǎng)按,那么longPress 消息就會(huì)被發(fā)送。默認(rèn)情況下,一個(gè)touch必須保持0.5秒,但是你能夠改變手勢(shì)識(shí)別的最小時(shí)間。
A tap is a simple gesture. By the time it is recognized, the gesture is over, and the action message has been delivered.
一個(gè)tap是簡(jiǎn)單地手勢(shì)。當(dāng)它被識(shí)別后,手勢(shì)就結(jié)束了,action消息就會(huì)被發(fā)出。
A long press, on the other hand, is a gesture that occurs over time and is defined by three separate events.
一個(gè)長(zhǎng)按,是一個(gè)發(fā)生在過(guò)程的手勢(shì),被定義了三個(gè)分別的事件。
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.
當(dāng)一個(gè)用戶觸摸一個(gè)view,long press recognizer 注意到一個(gè)可能的long press,但是必須等到看看這個(gè)touch 是否維持了做夠長(zhǎng)的時(shí)間成為一個(gè)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 足夠長(zhǎng)時(shí)間的touch,long press 就會(huì)被識(shí)別,gesture 便開(kāi)始了,當(dāng)用戶移除手指,這個(gè)手勢(shì)就結(jié)束了。?
Each of these events causes a change in the gesture recognizer's state property.
這些時(shí)間的每一過(guò)程都會(huì)導(dǎo)致一個(gè)gesture recognzier的狀態(tài)屬性的改變。
the state of the long press recognizer described above would be UIGestureRecognizerStatePossible, then UIGestureRecognizerStateBegan, and finally UIGestureRecognizerStateEnded.
long press recognizer的狀態(tài)可能是UIGestureRecognizerStatePossible,接著UIGestureRecognizerStateBegan,最后UIGestureRecognizerStateEnded。
When a gesture recognizer transitions to any state other than the possible state, it sends its action message to its target.
當(dāng)處理識(shí)別到possible state,gesture recognizer 將會(huì)發(fā)送一個(gè)action 消息給target。?
This means the long press recognizer's target receives the same message when a long press begins and when it ends.
也就是說(shuō)當(dāng) a long press開(kāi)始和結(jié)束的時(shí)候,long press recognizer's target都會(huì)接受到相同的信息。
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.
當(dāng)view接受到longPress消息時(shí),他就會(huì)選擇最近選擇的line。
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).
這允許用戶選擇一個(gè)line,當(dāng)手指一直在屏幕上的時(shí)候。
When the view receives longPress: and the long press has ended, you will deselect the line.
當(dāng)view 收到longPress 消息時(shí),long press 結(jié)束的時(shí)候,你會(huì)取消選擇那個(gè)line。
?
-(void)longPress:(UIGestureRecognizer*)gestureRecognzier{
? ? 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.selectedLine=nil;
? ? }
? ? [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.
為了手指能在屏幕上移動(dòng),你需要一個(gè)gesture recognizer .
This gesture is called panning, and its gesture recognizer subclass is UIPanGestureRecognizer.
這個(gè)手勢(shì)成為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.
一旦它被識(shí)別它的手勢(shì),它會(huì)"吃進(jìn)"這個(gè)touch,沒(méi)有其他的recognizer 有機(jī)會(huì)處理它。
In your case, this is bad: the entire pan gesture you want to recognize happens within a long press gesture.
在這種情況下,非常不好:真?zhèn)€的pan gesture 你想識(shí)別的, 整個(gè)的發(fā)生在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能夠同時(shí)識(shí)別出他們的手勢(shì)。
?
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.
一個(gè)gesture recognizer 將會(huì)發(fā)送
gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer消息給它的委托當(dāng)它識(shí)別出它的手勢(shì)但是也了解到另一個(gè)gesture recognize 也已經(jīng)識(shí)別出這個(gè)手勢(shì)了。
If this method returns YES, the recognizer will share its touches with other gesture recognizers.
如果這個(gè)手勢(shì)返回YES,那么recognizer 將會(huì)分享它的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.
現(xiàn)在當(dāng)用戶開(kāi)始長(zhǎng)按,UIPanGestureRecognizer將會(huì)被允許跟蹤手指。
When the finger begins to move, the pan recognizer will transition to the began state.
如果手指開(kāi)始移動(dòng),pan recognizer將會(huì)轉(zhuǎn)成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.
一個(gè)pan gesture recognizer 支持改變state。
When a finger starts to move, the pan recognizer enters the began state and sends a message to its target.
當(dāng)手指開(kāi)始移動(dòng)時(shí), pan recognizer? 進(jìn)入began state并發(fā)送一個(gè)消息給它的target。
While the finger moves around the screen, the recognizer transitions to the changed state and sends its action message to its target repeatedly.
當(dāng)手指在屏幕上移動(dòng)時(shí),recognizer 轉(zhuǎn)移成 changed state 并反復(fù)的 發(fā)送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.
最后 當(dāng)手指離開(kāi)屏幕時(shí),recognzier 的state 設(shè)置為ended 。最后的消息被發(fā)送給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.
在這個(gè)實(shí)現(xiàn)中,你將發(fā)送消息 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方法會(huì)返回這個(gè)pan 作為CGPoint 在給定參數(shù)的view中得坐標(biāo)系中移動(dòng)了多少。?
When the pan gesture begins, this property is set to the zero point (where both x and y equal zero).
當(dāng)pan gesture 開(kāi)始的時(shí)候 ,這個(gè)屬性設(shè)置為 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 移動(dòng)了,這個(gè)value就會(huì)被更新。
?
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.
你在添加現(xiàn)在的translation 一遍又一遍的到直線最開(kāi)始的位置。
You really need the gesture recognizer to report the change in translation since the last time this method was called instead.
你其實(shí)需要gesture recognizer 報(bào)告自從上次方法被調(diào)用后在translation? 的變化.
You can set the translation of a pan gesture recognizer back to the zero point every time it reports a change.
你可以在每次它報(bào)告一個(gè)change后可以設(shè)置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.
讓我們看一看你設(shè)置的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 會(huì)吃進(jìn)任何它識(shí)別的touch,所以view 將不會(huì)有機(jī)會(huì)通過(guò)傳統(tǒng)的UIResponder 方法像touchesBegan:withEvent處理了。
Usually, this is what you want, but not always.
一般情況下這是你想要的結(jié)果。
?
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.
?
??
?
?
?
?
?
?
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注