的几个注意点,Pan手势识别

近来做了一些有关 GestureRecognizer 的工作 ,小说记录一些亟需小心的点:

近年做了一部分有关 GestureRecognizer 的职业 ,随笔记录一些必要小心的点:

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

iOS 
Programming  UIGestureRecognizer and UIMenuController

 

 

self.view.addGestureRecognizer(panRecognizer)

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

  1. PanGestureRecognizer  
  1. PanGestureRecognizer  

安装前边调用的诀窍:respondsToPenGesture

1个UIGestureRecognizer拦截touches
在她们被二个view管理的中途。

(一)在动用时 注目的在于哪些view加多了 手势识别 

(1)在应用时 注意在哪些view增添了 手势识别 

func respondsToPenGesture(sender: UIPanGestureRecognizer) {}

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

self.view.addGestureRecognizer(panRecognizer)

self.view.addGestureRecognizer(panRecognizer)

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

当它检查评定到3个出色的手势时,它就会发送3个音信给你选中的离奇的目标。

要不会出现有的 不供给的辨别操作和响应

不然会并发部分 不需求的甄别操作和响应

startPanLocation = sender.locationInView(URVIEW)

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

 

 

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

在内部的SDK中,有一些非同一般的手势类型。

(二)假设想 把手势调控做成 slider : 

(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 

I.  state :那里是自带的state有八个 began , changed , ended 

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

你并不会实例化UIGestureRecognizer本身,而是有部分UIGestureRecognizer的子类,那一个子类负担鉴定区别三个例外的手势。

  在began的时候存入 初阶点的数据
,那几个点需借使个全局变量,要是是在began方法里设置了变量,
其余多个state的章程是取不到的。(

  在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)

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的时候就存入改变点的数据 (

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

亟需留意的是 startPanLocation
你必要存在二个全局变量里,因为在你state.changed时候,如若不是全剧变量你取不到starPanLocation的值。

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

let changeLocation = sender.locationInView(self.newEffectView)

let changeLocation = sender.locationInView(self.newEffectView)

余下的业务就相比较轻便了 你获取了abscissaChange
同理也能简单的到竖直方向的变化值,用这些值来对于你想要改造的值做一个加减总括即可轻巧实现Slider的机能。


(void)action:(UIGestureRecognizer *)gestureRecognizer;

Prisma的slider的作用也应有是二个道理来达成的,在view上在设置3个UITextlabel
就能完结一摸一样的成效,(当然还要加上三个延迟消失的功效,能够用dispatch_after来实现)

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

  当然ended的时候 也足以存入你要求的其他新闻

  当然ended的时候 也足以存入你要求的任何音信

Good Luck ,tlm .

当鉴定区别出二个手势后,它的gesture
recognizer 为钦定的view解析那些touches。

II.  变量转变难题 CGFloat, 假如想成为任何的数字类型, 先转成Float类型
在开始展览转移 不然获得的功能一般景色下 和您想要的不均等

II.  变量转变难题 CGFloat, 假使想产生任何的数字类型, 先转成Float类型
在拓展转移 不然获得的效益一般景色下 和您想要的不相同等

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

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

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

由此八个有gesture
recognizers的view 可能不接受独立的UIResponder
消息,比如touchesBegian:with伊芙nt澳门葡京备用网址 1

  1. swipeGesture 
  1. swipeGesture 

2 Detecting Taps with
UITapGestureRecognizer

的几个注意点,Pan手势识别。(一)right 和 left , 假设不表明 默许是向右的 

(一)right 和 left , 假诺不注明 暗中认可是向右的 

The
first UIGestureRecognizer subclass you will use is
UITapGestureRecognizer.

(贰)swipeGesture 未有得以state 所以不能够做成slider

(二)swipeGesture 没有能够state 所以无法做成slider

第多个你将会利用的UIGestureRecognizer子类是UITapGestureRecognizer。

 

 

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.

当2个touch
开始和了结在相当短的1段时间内到位时,四个tap gesture recognizer
被辨认,UITapGestureRecognizer 无法宣称这一个touch
正是1个tap,touchesBegan:with伊芙nt:也被发送给那一个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:with伊夫nt将会被发送给那些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.

为了阻止那一个革命的点一时出现,你能告诉UIGestureRecognizer延迟发送touchesBegan:with伊夫nt给它的view,假诺它照旧恐怕手势被辨认。

 

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.

当你有八个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!”

在那种情况下,你在五个recognzier之间设置信赖关系:”在您fire在此以前先等一下,因为那些手势恐怕是自身的。”

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.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;

}

  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

有叁个里头构建的类来提供那种菜单,称为UIMenuController

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

二个menu
Controller 有1列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
有3个标题(突显在menu上)和2个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.

澳门葡京备用网址,为了让1个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.

要是你有1个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.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.

未来你能够长按,那么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.

1个tap是简单地手势。当它被辨认后,手势就终止了,action音信就会被发生。

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.

当3个用户触摸2个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.

那一个时间的每一进度都会招致1个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.

当view接受到longPress音讯时,他就会挑选目前甄选的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).

这允许用户选拔3个line,当手指一向在显示屏上的时候。

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

当view
收到longPress 新闻时,long press 甘休的时候,你会收回采取那几个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.

为了手指能在荧屏上移步,你要求七个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.

近期当用户发轫长按,UIPanGestureRecognizer将会被允许追踪手指。

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:with伊夫nt管理了。

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.

 

   

 

 

 

 

 

 

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

*
*
Website