有没有办法检测屏幕键盘上的触摸?

时间:2012-09-11 01:08:27

标签: ios xcode

我不关心按下哪个键,或者多长时间,或者类似的东西。我只需要一种检测用户触摸屏幕的方法,即使这恰好是键盘覆盖的屏幕部分。

我的目标是检测缺少互动,以便将应用程序“重置”为默认状态(如果留下足够长的时间) - 请考虑“自助服务终端模式”应用程序。问题是我无法检测键盘何时被触摸,因为键盘显然拦截了所有触摸事件,甚至在我的客户窗口可以处理它们之前。

编辑:

考虑(和解雇)是仅使用键盘显示和隐藏通知 - 如果用户正在主动键入,我们需要延长屏幕显示。由于我们使用UIWebViews来显示某些内容,因此我们也无法使用UITextViews或UITextFields的委托方法。

2 个答案:

答案 0 :(得分:3)

听起来像检测键盘和其他地方的所有触摸就足够了。我们可以通过继承UIApplication来覆盖sendEvent:

来实现

我们将使用新消息UIApplicationDelegate扩展application:willSendTouchEvent:协议,并且我们将使UIApplication子类在处理任何触摸事件之前将消息发送给其委托。< / p>

MyApplication.h

@interface MyApplication : UIApplication
@end

@protocol MyApplicationDelegate <UIApplicationDelegate>
- (void)application:(MyApplication *)application willSendTouchEvent:(UIEvent *)event;
@end

MyApplication.m

@implementation MyApplication

- (void)sendEvent:(UIEvent *)event {
    if (event.type == UIEventTypeTouches) {
        id<MyApplicationDelegate> delegate = (id<MyApplicationDelegate>)self.delegate;
        [delegate application:self willSendTouchEvent:event];
    }
    [super sendEvent:event];
}

@end

我们需要让我们的app委托符合MyApplicationDelegate协议:

AppDelegate.h

#import "MyApplication.h"

@interface AppDelegate : UIResponder <MyApplicationDelegate>
// ...

AppDelegate.m

@implementation AppDelegate

- (void)application:(MyApplication *)application willSendTouchEvent:(UIEvent *)event {
    NSLog(@"touch event: %@", event);
    // Reset your idle timer here.
}

最后,我们需要让应用使用新的MyApplication类:

的main.m

#import "AppDelegate.h"
#import "MyApplication.h"

int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv,
            NSStringFromClass([MyApplication class]),
            NSStringFromClass([AppDelegate class]));
    }
}

答案 1 :(得分:2)

UITextField或UITextView有一个委托方法来检测用户输入内容的时间:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
   // user tapped a key
   //
   // reset your "idle" time variable to say user has interacted with your app
}

请记住符合UITextField或UITextView协议,具体取决于您使用的协议(如果您同时拥有文本字段和文本视图,则可能都是这两种协议)。还要记住将每个文本字段或文本视图的委托标记为视图控制器。

<UITextFieldDelegate,UITextViewDelegate>

更新答案

罗恩,不确定你是否真的使用Google搜索,但我发现了这个:

iPhone: Detecting user inactivity/idle time since last screen touch

相关问题