ios 手势识别器由于某种原因无法正常工作,在 iphone 上
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18915471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Gesture recognizer not working for some reason, on iphone
提问by Horton Hymanson
This is in a UILabel subclass I made, but it doesn't seem to work. It doesn't call "tappedOn" for some reason.
这是在我制作的 UILabel 子类中,但它似乎不起作用。由于某种原因,它不会调用“tappedOn”。
gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedOn)];
[self addGestureRecognizer:gesture];
[self setUserInteractionEnabled:YES];
Do you know why it might be?
你知道为什么会这样吗?
Here is the tappedOn method:
这是 tappedOn 方法:
-(void)tappedOn
{
NSLog(@"tapped");
if ([self.type isEqualToString: @"load"])
{
[self.manager.manager loadSavedGameInRow:self.name];
}
if ([self.type isEqualToString: @"save"])
{
[self.manager.manager saveGameInRow:self.name];
}
}
回答by Irfa
For me it works:
对我来说它有效:
first set this : [self setUserInteractionEnabled:YES];
首先设置这个: [self setUserInteractionEnabled:YES];
then:
然后:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(method:)];
tap.numberOfTapsRequired = 1;
[self addGestureRecognizer:tap];
回答by Zev Eisenberg
In the off chance that your label is outside the bounds of its superview, touch handling will likely not work. Worth a check, since your gesture recognizer code seems correct.
如果您的标签超出其超级视图的范围,则触摸处理可能不起作用。值得一试,因为您的手势识别器代码似乎是正确的。
One more thing to try: are you sure your code to add the gesture recognizer to the view is being run? What method are you in when you call it?
还有一件事要尝试:您确定正在运行将手势识别器添加到视图的代码吗?当你调用它时,你在什么方法中?
回答by Manthan
I tried this code for my project and its working fine for me.
我为我的项目尝试了这段代码,它对我来说工作正常。
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureUpdated:)];
tapGesture.delegate = self;
tapGesture.numberOfTapsRequired = 1;
tapGesture.numberOfTouchesRequired = 1;
tapGesture.cancelsTouchesInView = NO;
[[self view] addGestureRecognizer:tapGesture];
- (IBAction)tapGestureUpdated:(UIGestureRecognizer *)recognizer
{
// Code to respond to gesture here
}
回答by incmiko
I found a good example for you: stackoverflow link
我为你找到了一个很好的例子:stackoverflow 链接
This is a step by step guide on how to implement gesture recognizers in your class:
Conform your class to
UIGestureRecognizerDelegate
protocol.Instantiate the gesture recognizer. For example, to instantiate a
UITapGestureRecognizer
, we will do:
这是有关如何在您的班级中实施手势识别器的分步指南:
使您的课程符合
UIGestureRecognizerDelegate
协议。实例化手势识别器。例如,要实例化 a
UITapGestureRecognizer
,我们将执行以下操作:
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)];
Here, action is the selector which will handle the gesture. Here, our selector handleTapFrom will look something like:
- (void) handleTapFrom: (UITapGestureRecognizer *)recognizer { //Code to handle the gesture }
The argument to the selector is the gesture recognizer. We can use this gesture recognizer to access its properties, for example, we can find the state of the gesture recognizer, like,
UIGestureRecognizerStateBegan
,UIGestureRecognizerStateEnded
, etc.Set the desired properties on the instantiated gesture recognizer. For example, for a
UITapGestureRecognizer
, we can set the propertiesnumberOfTapsRequired
, andnumberOfTouchesRequired
.Add the gesture recognizer to the view you want to detect gestures for. In our sample code (I will be sharing that code for your reference), we will add gesture recognizers to an imageView with the following line of code:
[self.imageView addGestureRecognizer:tapGestureRecognizer];
After adding the gesture recognizer to the view, set the delegate for the gesture recognizer, i.e. the class which will handle all the gesture recognizer stuff. In our sample code, it would be like:
tapGestureRecognizer.delegate = self;
Note: Assign the delegate after adding the gesture recognizer to the view. Otherwise, the action method won't be called.
在这里,动作是将处理手势的选择器。在这里,我们的选择器 handleTapFrom 看起来像:
- (void) handleTapFrom: (UITapGestureRecognizer *)recognizer { //Code to handle the gesture }
选择器的参数是手势识别器。我们可以使用这个手势识别器来访问它的属性,例如,我们可以发现手势识别的,喜欢的状态
UIGestureRecognizerStateBegan
,UIGestureRecognizerStateEnded
等等。在实例化的手势识别器上设置所需的属性。例如,对于 a
UITapGestureRecognizer
,我们可以设置属性numberOfTapsRequired
、 和numberOfTouchesRequired
。将手势识别器添加到要检测手势的视图中。在我们的示例代码中(我将分享该代码供您参考),我们将使用以下代码行将手势识别器添加到 imageView:
[self.imageView addGestureRecognizer:tapGestureRecognizer];
将手势识别器添加到视图后,设置手势识别器的委托,即将处理所有手势识别器内容的类。在我们的示例代码中,它类似于:
tapGestureRecognizer.delegate = self;
注意:在将手势识别器添加到视图后分配委托。否则,不会调用 action 方法。
回答by ilovecomputer
Try this:
尝试这个:
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
I use Swift because I'm not familiar with ObjC. Return true
is guaranteed to allow simultaneous recognition.
我使用 Swift 是因为我不熟悉 ObjC。返回true
保证允许同时识别。
Sometimes you may want to set tup some constraints to avoid conflict, like:
有时您可能希望设置一些约束以避免冲突,例如:
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
if gestureRecognizer == singleTapShowHideRecognizer {
return true
}
return false
}
回答by siege_Perilous
Looks like it should be in order... but have you forgotten your type in front of "gesture"? See below, and note the added UITapGestureRecognizerbefore your code:
看起来应该是有序的......但是你是否忘记了“手势”前的类型?请参阅下文,并注意在您的代码之前添加的UITapGestureRecognizer:
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedOn)];
[self addGestureRecognizer:gesture];
[self setUserInteractionEnabled:YES];
For more infos, see this answer about how to make a label clickable.
有关更多信息,请参阅有关如何使标签可点击的答案。
回答by Jitendra
gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedOn:)];
[self.view addGestureRecognizer:gesture];
self.UserInteractionEnabled=TRUE;
Add your method like this
像这样添加你的方法
- (void) tappedOn:(UIGestureRecognizer*)sender
{
}
Try this one
试试这个
回答by βhargav?
Not sure if this helps but to receive touch events exclusively use setExclusiveTouch
:
不确定这是否有帮助,但仅接收触摸事件使用setExclusiveTouch
:
gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedOn)];
[self addGestureRecognizer:gesture];
[self setUserInteractionEnabled:YES];
[self setExclusiveTouch:YES];
回答by viggio24
It should work. It seems that you added the gesture inside the label subclass code. Are you sure you put in both initWithFrame: and initWithCoder: methods? because the two inits could be called independently based on the fact that you instantiate the label using a XIB or programatically.
它应该工作。您似乎在标签子类代码中添加了手势。你确定你同时加入了 initWithFrame: 和 initWithCoder: 方法吗?因为可以根据您使用 XIB 或以编程方式实例化标签的事实独立调用这两个初始化。
Example:
例子:
@implementation MyLabel
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self addGesture];
}
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if(self) {
[self addGesture];
}
return self;
}
-(void)addGesture {
UITapGestureRecognizer *_g = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myTap:)];
[self addGestureRecognizer:_g];
[self setUserInteractionEnabled:YES];
}
回答by ArturT
I've had a similar frustrating problem - the solution was to make sure that the parent views of your view also have their "User interaction enabled" property set to true. You can set that through IB under "View" => "Interaction".
我遇到了类似的令人沮丧的问题 - 解决方案是确保视图的父视图也将其“启用用户交互”属性设置为 true。您可以通过“查看”=>“交互”下的 IB 进行设置。