UIBarButtonItem 长按/短按 [英] UIBarButtonItem Long Press / Short Press

查看:30
本文介绍了UIBarButtonItem 长按/短按的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经查看了有关此主题的现有问题,在 iOS 11 之后似乎没有答案(这似乎破坏了手势识别器).

I have looked through the existing questions on this subject matter, and there appears to be no answers post iOS 11 (which appeared to break the gesturerecognizers).

有没有办法检测 UIBarButtonItem 上的短按/长按?Apple 在 Pages、Numbers、Keynote 中使用此功能进行撤消/重做.

Is there a way to detect the short press / long press on a UIBarButtonItem? Apple uses this functionality in Pages, Numbers, Keynote for Undo / Redo.

推荐答案

只需为点击事件替换默认的事件处理程序

Just replace your default event handler for the tap event

-(void)handlerForBarButton:(UIBarButtonItem*)p_Sender

及其扩展版本

-(void)handlerForBarButton:(UIBarButtonItem*)p_Sender forEvent:(UIEvent*)p_Event

通过检查 UIEvent 对象,您可以了解事件是否是长按(第一个 UITouch 对象的点击计数为 0).

By inspecting the UIEvent object you can learn, if the event was a long press (tapCount of first UITouch object is 0).

这是我的播放速率按钮代码,如果条形按钮项目被按下约一秒钟,则将速率返回到1x":

This is my code for a playback rate button that returns the rate to '1x' if the bar button item is pressed longer the about one second:

/*
 rateButtonPressed:forEvent:

 */
- (IBAction)rateButtonPressed:(UIBarButtonItem*)p_Sender
                     forEvent:(UIEvent*)p_Event
{
    UITouch*    firstTouch = nil;
    if (   (nil != ((firstTouch = p_Event.allTouches.allObjects.firstObject)))
        && (0 == firstTouch.tapCount))
    {
        self.avAudioPlayer.rate = 1.0;
    }
    else
    {   // Default tap
        if (2.0 == self.avAudioPlayer.rate)
        {
            self.avAudioPlayer.rate = 0.5;
        }
        else
        {
            self.avAudioPlayer.rate += 0.25;
        }
    }
    [WBSettingsSharedInstance.standardUserDefaults setDouble:self.avAudioPlayer.rate
                                                      forKey:@"audioPlayerRate"];
    [self updateRateBarButton];
}

这篇关于UIBarButtonItem 长按/短按的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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