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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 04:09:53  来源:igfitidea点击:

How to get the original attached view in gesture recognizer in iphone?

iosiphonexcode

提问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.viewI am getting the attached view as UIViewBut 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;

UIButtonis 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 gestureRecogniserhas only one viewproperty, this explains why a gesture recogniser can be added to 1 view only.

由于每个gestureRecogniser都只有一个view属性,这就解释了为什么只能将手势识别器添加到 1 个视图中。