【问题标题】:UILongPressGestureRecognizer with different buttons具有不同按钮的 UILongPressGestureRecognizer
【发布时间】:2012-02-13 18:51:30
【问题描述】:

我有 2 个带有“UILongPressGestureRecognizer”的按钮,我这样做:

Fot 按钮 1:

-(IBAction)seleccionar46:(id)sender{
UILongPressGestureRecognizer *longpressGesture =[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressHandler:)];
longpressGesture.minimumPressDuration = 3;
[longpressGesture setDelegate:self];
[self.button1 addGestureRecognizer:longpressGesture];}

对于按钮 2:

    -(IBAction)seleccionar46:(id)sender{
UILongPressGestureRecognizer *longpressGesture =[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressHandler:)];
longpressGesture.minimumPressDuration = 3;
[longpressGesture setDelegate:self];
[self.button2 addGestureRecognizer:longpressGesture];}

在“longpressGesture”中,我需要区分 button1 和 button2,但我做不到...

 - (void)longPressHandler:(UILongPressGestureRecognizer *)gestureRecognizer{//Here i need do the differentation}

谢谢大家!

最好的问候。

【问题讨论】:

    标签: iphone xcode button


    【解决方案1】:

    您可以使用UIGestureRecognizer 的view 属性,因此如果您保存对两个按钮的引用,则可以检查是否相等。

    如果你有

    @interface blah
    {
      UIButton *buttonOne;
      UIButton *buttonTwo;
    }
    

    然后您将识别器添加到您可以在处理程序中执行的按钮

    if(gestureRecognizer.view==buttonOne)
    {
       //do stuff for button one
    }
    else if(gestureRecognizer.view==buttonTwo)
    {
      //do stuff for button two
    }
    

    希望对你有帮助

    【讨论】:

    • 谢谢朋友!我需要在 NSStstring 中保存按钮的名称...我该怎么做?
    • 因为按钮的名称与一个图像的名称相同...当我按下按钮 3 秒时,它显示与按钮关联的图像...我希望你可以理解...感谢所有丹尼尔
    • 好吧,你应该保留某种字典或其他东西来跟踪它,或者如果每个按钮只有一个图像并且它是静态的,则对其进行硬编码
    • 每个按钮只有一个图像,但我无法理解“硬核”...你能给我举个例子吗?谢谢丹尼尔
    猜你喜欢
    • 2018-03-03
    • 1970-01-01
    • 2019-05-13
    • 2014-11-04
    • 2019-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多