的多少个注意点,Pan手势识别

By admin in 4858.com on 2019年4月27日

近年做了有的有关 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

一个UIGestureRecognizer拦截touches
在他们被一个view管理的中途。

(壹)在采用时 注意在哪些view添加了 手势识别 

(一)在应用时 注目的在于哪个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个音讯给你选中的奇怪的对象。

再不会现出局地 不须求的分辨操作和响应

要不然会产出有的 不须求的鉴定分别操作和响应

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 并把它与3个view 联系在一同。

4858.com,)

}

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

  当changed的时候就存入改动点的数据 (

  当changed的时候就存入改造点的数据 (

亟需注意的是 startPanLocation
你必要存在1个全局变量里,因为在您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上在安装一个UITextlabel
就能达标壹摸同样的成效,(当然还要加上3个延缓消失的功效,能够用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 > 2个数据 ,不然识别度太高
会跟button的点按有效争执

由此1个有gesture
recognizers的view 大概不收受独立的UIResponder
音信,举个例子touchesBegian:with伊芙nt4858.com 1

  1. swipeGesture 
  1. swipeGesture 

2 Detecting Taps with
UITapGestureRecognizer

(一)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.

留意到3个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.

当3个touch
早先和终结在十分的短的一段时间内做到时,叁个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,借使它依然恐怕手势被辨认。

的多少个注意点,Pan手势识别。 

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
的第3个从前,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有3个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 有壹列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)4858.com 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.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
消息就会被发送。暗中认可情形下,3个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.

三个tap是粗略地手势。当它被辨认后,手势就得了了,action音讯就会被产生。

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

一个长按,是3个生出在经过的手势,被定义了多少个分其余轩然大波。

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个用户触摸三个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).

那允许用户选拔1个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音讯给它的嘱托当它识别出它的手势不过也询问到另3个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
1遍又三次的到直线最起先的职位。

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.

你能够在每一遍它报告1个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.

 

   

 

 

 

 

 

 

发表评论

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

网站地图xml地图
Copyright @ 2010-2019 美高梅手机版4858 版权所有