如何在iPhone中的手势识别器中获取原始附加视图?

时间:2013-10-23 07:48:43

标签: ios iphone xcode

通过以下代码,我在手势识别器中附加了一个按钮:

UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(addLongpressGesture:)];
[longPress setDelegate:self];
[BUTTON addGestureRecognizer:longPress];

这是我的addLongpressGesture方法:

- (void)addLongpressGesture:(UILongPressGestureRecognizer *)sender {

UIView *view = sender.view;

CGPoint point = [sender locationInView:view.superview];

if (sender.state == UIGestureRecognizerStateBegan){ 

      // GESTURE STATE BEGAN

}
}

通过此代码sender.view我将附加视图作为UIView但我希望视图附加(UIButton),如何将UIView作为UIButton?

3 个答案:

答案 0 :(得分:12)

更改此

UIView *view = sender.view;

到这个

UIButton *btn = (UIButton*)sender.view;

答案 1 :(得分:5)

像这样:

UIButton *button = (UIButton*)sender.view;

UIButtonUIView。如果您知道手势识别器已附加到按钮上,则此演员表是安全的。

答案 2 :(得分:2)

UIView* yourView = yourGestureRecogniser.view;

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