如何通过长时间pressGestureRecognizer得到button.tag? [英] How to get button.tag via longPressGestureRecognizer?

查看:254
本文介绍了如何通过长时间pressGestureRecognizer得到button.tag?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我动态地添加图像按钮一些滚动视图。他们都指向一个长pressHandler。现在,我怎么哪个按钮被pressed?在[发件人标签]给我,我加入按钮longGestureRecognizer的标签,我不能手动设置标签。

 的(...){
    * UIButton的按钮= [[UIButton的页头]初始化];
    button.tag = W + H * 3;
    [按钮addTarget:自我行动:@selector(ImageButton的pressed :) forControlEvents:UIControlEventTouchUpInside];
    UILong pressGestureRecognizer *武功= [[UILong pressGestureRecognizer页头]
                initWithTarget:自我行动:@selector(imageButtonLong preSS :)]。
    gest.minimum pressDuration = 1;
    gest.delegate =自我;    [按钮addGestureRecognizer:武功];
    [武功发布]    [滚动视图addSubview:键];
    [按钮释放];
} - (无效)imageButtonLong preSS:(ID)发送{
    //如何在这里得到按钮的标签?
}


解决方案

有一个视图 UIGestureRecognizer财产它返回识别器被附接到图。我认为这是你最好的选择。

   - (无效)imageButtonLong preSS:(ID)发送{
    UIGestureRecognizer *识别=(UIGestureRecognizer *)寄件人;
    INT标记= recognizer.view.tag;
}

I'm dynamically adding image buttons to some scrollview. They all point at one longPressHandler. Now, how do I get which button was pressed? The [sender tag] gives me the tag of longGestureRecognizer that I added to button and I can't manually set that tag.

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?
}

解决方案

There is a view property in the UIGestureRecognizer which returns the view that recognizer is attached to. I think that is your best bet.

- (void) imageButtonLongPress:(id)sender {  
    UIGestureRecognizer *recognizer = (UIGestureRecognizer*) sender;
    int tag = recognizer.view.tag;
}

这篇关于如何通过长时间pressGestureRecognizer得到button.tag?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆