1

どのキーが押されたか、どのくらい押されたか、またはそのようなことは気にしません。必要なのは、ユーザーが画面に触れたことを検出する方法だけです。それがたまたまキーボードで覆われた画面の一部にあったとしてもです。

ここでの私の目標は、相互作用の欠如を検出して、十分に長く放置された場合にアプリをデフォルトの状態に「リセット」することです。「キオスクモード」アプリケーションを考えてみてください。問題は、キーボードがタッチされたときを検出できないことです。これは、キーボードが明らかにすべてのタッチ イベントを傍受し、顧客ウィンドウがそれらを処理できるようになる前でもあります。

編集:

考慮された(そして却下された)のは、キーボードの表示と非表示の通知のみの使用です。ユーザーがアクティブに入力している場合は、画面表示を延長する必要があります。UIWebViews を使用して特定のものを表示するため、UITextViews または UITextFields のデリゲート メソッドも使用できません。

4

3 に答える 3

3

キーボードやその他の場所でのすべてのタッチを検出するだけで十分なようです。UIApplicationoverrideにサブクラス化することでそれを行うことができますsendEvent:

UIApplicationDelegateプロトコルを新しいメッセージ で拡張し、タッチ イベントを処理する前にapplication:willSendTouchEvent:UIApplicationサブクラスがデリゲートにメッセージを送信するようにします。

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

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]));
    }
}
于 2012-09-11T01:40:39.867 に答える
2

タイマーを設定するために、keyboardShow および keyBoardHide 通知を使用するのはどうですか? x 秒後、アプリを必要な状態に戻します。

必要に応じて、scrollview デリゲートまたは textField デリゲートでタイマーをリセットすることもできます。

を見てみましょう:

[[NSNotificationCenter defaultCenter] addObserver:self 
                                  selector:@selector(keyboardDidShow:) 
                                      name:UIKeyboardDidShowNotification 
                                    object:nil];    

そして、このような方法:

- (void)keyboardDidShow:(NSNotification *)note {
    idleTimer = [NSTimer scheduledTimerWithTimeInterval:120 
                         target:nil 
                       selector:@selector(yourMethodHere) 
                       userInfo:nil 
                        repeats:NO]; 
}

.h および .m ファイルでタイマーを宣言し、@synthesize してください。

それが役に立てば幸い。

于 2012-09-11T01:29:47.287 に答える
2

UITextField or UITextView has a delegate method to detect when user types something:

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

Remember to conform to the UITextField or UITextView protocol depending which one you use (maybe both if you've got both text field and text views). Also remember to mark each of your text field or text view's delegate as your view controller.

<UITextFieldDelegate,UITextViewDelegate>

Updated Answer

Ron, not sure if you actually googled but I found this:

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

于 2012-09-11T01:32:29.857 に答える