通过以下代码,我在手势识别器中附加了一个按钮:
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?
答案 0 :(得分:12)
更改此
UIView *view = sender.view;
到这个
UIButton *btn = (UIButton*)sender.view;
答案 1 :(得分:5)
像这样:
UIButton *button = (UIButton*)sender.view;
UIButton
是UIView
。如果您知道手势识别器已附加到按钮上,则此演员表是安全的。
答案 2 :(得分:2)
UIView* yourView = yourGestureRecogniser.view;
由于每个gestureRecogniser
只有一个view
属性,这就解释了为什么手势识别器只能添加到1个视图中。