亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

iPhone:檢測用戶自上一次屏幕觸摸以來的不活動/空閑時間

iPhone:檢測用戶自上一次屏幕觸摸以來的不活動/空閑時間

心有法竹 2019-07-15 09:33:21
iPhone:檢測用戶自上一次屏幕觸摸以來的不活動/空閑時間是否有人實現了一個功能,如果用戶在某一段時間內沒有觸摸屏幕,您就會采取特定的操作嗎?我想找出最好的辦法。UIApplication中有一個與此相關的方法:[UIApplication sharedApplication].idleTimerDisabled;如果你有這樣的東西,那就太好了:NSTimeInterval timeElapsed = [UIApplication sharedApplication].idleTimeElapsed;然后,我可以設置一個計時器,并定期檢查這個值,并在它超過閾值時采取一些操作。希望這能解釋我要找的東西。有沒有人已經處理過這個問題,或者對你會怎么做有任何想法?謝謝。
查看完整描述

3 回答

?
互換的青春

TA貢獻1797條經驗 獲得超6個贊

以下是我一直在尋找的答案:

讓您的應用程序委托子類UIApplication。在實現文件中,重寫sendEvent:方法,如下所示:

- (void)sendEvent:(UIEvent *)event {
    [super sendEvent:event];

    // 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) {
        // allTouches count only ever seems to be 1, so anyObject works here.
        UITouchPhase phase = ((UITouch *)[allTouches anyObject]).phase;
        if (phase == UITouchPhaseBegan || phase == UITouchPhaseEnded)
            [self resetIdleTimer];
    }}- (void)resetIdleTimer {
    if (idleTimer) {
        [idleTimer invalidate];
        [idleTimer release];
    }

    idleTimer = [[NSTimer scheduledTimerWithTimeInterval:maxIdleTime target:self selector:@selector(idleTimerExceeded) userInfo:
    nil repeats:NO] retain];}- (void)idleTimerExceeded {
    NSLog(@"idle time exceeded");}

其中maxIdleTime和idleTimer是實例變量。

為了使其工作,還需要修改main.m以告訴UIApplicationMain使用您的委托類(在本例中是AppDelegate)作為主體類:

int retVal = UIApplicationMain(argc, argv, @"AppDelegate", @"AppDelegate");


查看完整回答
反對 回復 2019-07-15
  • 3 回答
  • 0 關注
  • 736 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號