我正在动态地将图像按钮添加到某些滚动视图中。他们都指向一个longPressHandler。现在,我如何获得按下哪个按钮? [sender tag]给了我添加到按钮的longGestureRecognizer标签,我无法手动设置该标签。
for (...) {
UIButton *button = [[UIButton alloc] init];
button.tag = w + h * 3;
[button addTarget:self action:@selector(imageButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
UILongPressGestureRecognizer *gest = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:@selector(imageButtonLongPress:)];
gest.minimumPressDuration = 1;
gest.delegate = self;
[button addGestureRecognizer:gest];
[gest release];
[scrollView addSubview:button];
[button release];
}
- (void) imageButtonLongPress:(id)sender {
// how to get button tag here?
}
答案 0 :(得分:12)
view
中有一个UIGestureRecognizer
属性,它返回识别器附加到的视图。我认为这是你最好的选择。
- (void) imageButtonLongPress:(id)sender {
UIGestureRecognizer *recognizer = (UIGestureRecognizer*) sender;
int tag = recognizer.view.tag;
}
答案 1 :(得分:2)
在您的操作中,您必须在手势中输入发送者,然后键入将其视图转换为按钮,然后将按钮的标记输入为 -
UILongPressGestureRecognizer *gest = (UILongPressGestureRecognizer *)sender;
UIButton *button = (UIButton*)[gest view];
NSLog(@"%d",[button tag]);