繁体   English   中英

iPhone键盘触摸事件

[英]iphone keyboard touch events

我需要能够检测键盘上的触摸事件。 我有一个应用程序,该应用程序显示在一段时间不活动(即没有触摸事件)后出现的屏幕。为解决此问题,我将UIWindow子类化并实现了sendEvent函数,该函数使我可以通过以下方式获取整个应用程序的触摸事件:在一处实施该方法。 除了在显示键盘和用户在键盘上输入内容时,该功能还可以在任何地方使用。 我需要知道的是,有没有一种方法可以检测键盘上的触摸事件,就像sendEvent对uiWindow所做的那样。 提前致谢。

找到了解决问题的办法。 如果您观察到以下通知,则在按下该键时可以得到一个事件。 我在自定义uiwindow类中添加了这些通知,因此在一处进行操作将使我能够在整个应用程序中获取这些触摸事件。

[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(keyPressed:) name: UITextFieldTextDidChangeNotification object: nil];
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(keyPressed:) name: UITextViewTextDidChangeNotification object: nil];

- (void)keyPressed:(NSNotification*)notification
{  [self resetIdleTimer];  }

无论如何,希望它能帮助别人。

iPhoneDev:这是我在做什么。

我有一个自定义的UIWindow对象。 在此对象中,有一个NSTimer,只要有触摸,它就会重置。 为了获得这种效果,您必须重写UIWindow的sendEvent方法。

这是我的自定义窗口类中的sendEvent方法的外观:

- (void)sendEvent:(UIEvent *)event
{
    if([super respondsToSelector: @selector(sendEvent:)])
    {
      [super sendEvent:event];
    }
    else
    {   
        NSLog(@"%@", @"CUSTOM_Window super does NOT respond to selector sendEvent:!");  
        ASSERT(false);
     }

     // Only want to reset the timer on a Began touch or an Ended touch, to reduce the number of timer resets.
     NSSet *allTouches = [event allTouches];
     if ([allTouches count] > 0)
     {
        // anyObject works here.
        UITouchPhase phase = ((UITouch *)[allTouches anyObject]).phase;
        if (phase == UITouchPhaseBegan || phase == UITouchPhaseEnded)
        {
           [self resetIdleTimer];
        }
     }
}

这是resetIdleTimer:

- (void)resetIdleTimer 
{
    if (self.idleTimer)
    {
        [self.idleTimer invalidate];
    }
    self.idleTimer = [NSTimer scheduledTimerWithTimeInterval:PASSWORD_TIMEOUT_INTERVAL target:self selector:@selector(idleTimerExceeded) userInfo:nil repeats:NO];
}

之后,在idleTimerExceeded中,我向窗口委托(在本例中为appDelegate)发送一条消息。

- (void)idleTimerExceeded
{
    [MY_CUSTOM_WINDOW_Delegate idleTimeLimitExceeded];
}

当我在appDelegate中创建此自定义窗口对象时,将appDelegate设置为此窗口的委托。 在appDelegate中,对idleTimeLimitExceeded的定义是在计时器到期时我必须做的事情。 他们的关键是创建自定义窗口并覆盖sendEvent函数。 将此与上面显示的两个键盘通知(我在自定义窗口类的init方法中添加)相结合,您应该能够在应用程序中任何位置的屏幕上获取所有触摸事件的99%。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM