您可以将 UIGestureRecognizer 附加到多个视图吗?
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTapTap:)];
[self.view1 addGestureRecognizer:tapGesture];
[self.view2 addGestureRecognizer:tapGesture];
[tapGesture release];
在上面的代码中,仅识别 view2
上的点击。如果我注释掉第三行,则可以识别 view1
上的点击。如果我是对的,并且您只能使用手势识别器一次,我不确定这是否是一个错误,或者只是需要更多文档。
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTapTap:)];
[self.view1 addGestureRecognizer:tapGesture];
[self.view2 addGestureRecognizer:tapGesture];
[tapGesture release];
In the above code only taps on view2
are recognized. If I comment out the third line then taps on view1
are recognized. If I'm right and you can only use a gesture recognizer once, I'm not sure if this is a bug or it just needs some more documentation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
UIGestureRecognizer
将与单个视图一起使用。我同意文档参差不齐。UIGestureRecognizer
有一个view
属性将其泄露:A
UIGestureRecognizer
is to be used with a single view. I agree the documentation is spotty. ThatUIGestureRecognizer
has a singleview
property gives it away:我通过使用下面的方法解决了这个问题。
然后在我的 handleLongPress 方法中,我只需设置一个 UIButton 等于手势识别器的视图,并根据该按钮分支我所做的事情
I got around it by using the below.
Then in my handleLongPress method I just set a UIButton equal to the view of the gesture recognizer and branch what I do based upon that button
对于 Swift 3,如果有人需要这个:
基于上面 Bhavik Rathod 的回答。
For Swift 3 in case anyone requires this:
Based on Bhavik Rathod Answer above.
不,您不应将手势识别器附加到多个视图。
苹果文档中有这样明确的信息:
iOS 事件处理指南 - 手势识别器 Apple Developer Library
虽然正如其他人提到的,它们可能在某些情况下工作,但这显然违反了文档,并且可能在任何未来的 iOS 版本中发生变化。
您可以做的是将单独的手势识别器添加到您想要监视的视图中,并且它们可以共享共同的操作。
No you should not attach gesture recognizers to more than one view.
There is this explicit information in the Apple documentation:
Event Handling Guide for iOS - Gesture Recognizers Apple Developer Library
While as others mention they might work in some cases it is clearly against the documentation and could change in any future iOS version.
What you can do is add separate gesture recognisers to the views you want to monitor and they can share a common action.
我们可以做这样的事情,它很简单
1)在控制器中创建如下函数(该函数将返回GestureRecognizer)
2)现在在多个视图中设置此识别器
We can do something Like this, it's easy and simple
1) create function as below in your controller (this function will return GestureRecognizer)
2) now set this recognizer in multiple views
好吧,如果有人不想编写为多个按钮添加手势视图的代码,例如 kwalker 已经在上面回答了,并且想要这样做通过 Interface Builder 这可能会帮助你。
1)您可以从对象库添加长按手势识别器,就像添加 UIButtons 和 UILabels 等其他对象一样。
最初我最终使用的是我只使用了一个
2) 将引用出口设置为
UIButton
并与文件所有者发送操作。Well if someone does not want to code for adding gesture view for multiple buttons like kwalker has answered above, and want to do it via Interface Builder this may help you.
1) You can add Long Press gesture Recognizer from Object Library like you add other objects like UIButtons and UILabels.
Initially what I ended up using was I took only one
2) Set referencing outlets to
UIButton
and sent actions with File's Owner.如果你有固定的视图,我建议你做这样的事情
,这样会减少多个不同的无用变量
if you have fixed view I suggest you doing something like this
that way will reduce multiple different useless variable
您可以在视图上创建通用扩展以轻松添加手势识别器。
这只是一个示例,但它可能看起来像这样
在您只需调用的视图上添加 2 个点击识别器:
您还可以轻松添加滑动识别器
等。
请记住,目标必须链接到选择器。
You could create a generic extension on view to add gesture recognizers easily.
This is just an example but it could look like this
To add a 2 tap recognizer on a view you would just call:
You could also easily add a swipe recognizer
and so on.
Just remember that the target must be linked to the selector.
通过 '
' 覆盖类并在 .m 类中使用此方法:
此方法将帮助您在单个视图上启用多次滑动。
Override class by '
<UIScrollViewDelegate>
'And use this method in .m class:
This method will help you to enable multiple swipe on a single view..
每次添加指向同一函数的手势识别器时,重新编写(重新创建)您的 GestureRecognize 怎么样?
在下面的情况下它有效。我正在使用 IBOutletCollection
Swift 2:
What about re write (recreate) your GestureRecognize every time that you add a gesture recognizer pointing to the same func.
In below case it works. I am using IBOutletCollection
Swift 2:
我知道这是一篇旧文章,但我想到了类似的东西,希望对其他人有用。我只是将 imageView 存储在一个数组中,并将其分配给函数中的同一手势识别器来设置每个图像视图。
在我的 viewDidLoad():
设置图像视图的函数中:
在操作选择器 imageTapped() 中,您可以为点击的任何图像视图提供相应的代码。
I know this is an old post but I figured something similar and hopefully it's useful someone else. I simply stored my imageViews in an array and assigned it to to the same gesture recognizer in a function to set up each image view.
In my viewDidLoad():
Function to setup image views:
And in the action selector imageTapped(), you can have corresponding code for whichever image view tapped.
要在 Swift 中向多个视图添加点击手势,您可以使用循环迭代视图并向每个视图添加手势识别器。这是一个例子:
To add a tap gesture to multiple views in Swift, you can use a loop to iterate through the views and add the gesture recognizer to each one. Here's an example:
您可以使用此代码来完成此操作,我的视图是 xib 中的图像视图。
You can do it using this code my views which are imageviews in the xib.