xcode 如何在iphone的手势识别器中获取原始附加视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19535830/
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
How to get the original attached view in gesture recognizer in iphone?
提问by Reyjohn
By the following code I attatched a button in a gesture recognizer:
通过以下代码,我在手势识别器中附加了一个按钮:
UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(addLongpressGesture:)];
[longPress setDelegate:self];
[BUTTON addGestureRecognizer:longPress];
Here is my addLongpressGesture method:
这是我的 addLongpressGesture 方法:
- (void)addLongpressGesture:(UILongPressGestureRecognizer *)sender {
UIView *view = sender.view;
CGPoint point = [sender locationInView:view.superview];
if (sender.state == UIGestureRecognizerStateBegan){
// GESTURE STATE BEGAN
}
}
by this code sender.view
I am getting the attached view as UIView
But I want the view as it was attached (UIButton), how do I get the UIView as UIButton?
通过这段代码,sender.view
我获得了附加的视图,UIView
但是我想要附加的视图(UIButton),如何将 UIView 获取为 UIButton?
回答by Patel
change this
改变这个
UIView *view = sender.view;
to this
对此
UIButton *btn = (UIButton*)sender.view;
回答by dasblinkenlight
Like this:
像这样:
UIButton *button = (UIButton*)sender.view;
UIButton
is a UIView
. If you know that your gesture recognizer is attached to a button, this cast is safe.
UIButton
是一个UIView
。如果您知道您的手势识别器附加到按钮上,则此转换是安全的。
回答by n00bProgrammer
UIView* yourView = yourGestureRecogniser.view;
Since each gestureRecogniser
has only one view
property, this explains why a gesture recogniser can be added to 1 view only.
由于每个gestureRecogniser
都只有一个view
属性,这就解释了为什么只能将手势识别器添加到 1 个视图中。