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

前端 未结 9 1269
我寻月下人不归
我寻月下人不归 2020-11-22 17:10

Has anybody implemented a feature where if the user has not touched the screen for a certain time period, you take a certain action? I\'m trying to figure out the best way t

相关标签:
9条回答
  • 2020-11-22 17:35

    There's a way to do this app wide without individual controllers having to do anything. Just add a gesture recognizer that doesn't cancel touches. This way, all touches will be tracked for the timer, and other touches and gestures aren't affected at all so no one else has to know about it.

    fileprivate var timer ... //timer logic here
    
    @objc public class CatchAllGesture : UIGestureRecognizer {
        override public func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
            super.touchesBegan(touches, with: event)
        }
        override public func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) {
            //reset your timer here
            state = .failed
            super.touchesEnded(touches, with: event)
        }
        override public func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
            super.touchesMoved(touches, with: event)
        }
    }
    
    @objc extension YOURAPPAppDelegate {
    
        func addGesture () {
            let aGesture = CatchAllGesture(target: nil, action: nil)
            aGesture.cancelsTouchesInView = false
            self.window.addGestureRecognizer(aGesture)
        }
    }
    

    In your app delegate's did finish launch method, just call addGesture and you're all set. All touches will go through the CatchAllGesture's methods without it preventing the functionality of others.

    0 讨论(0)
  • 2020-11-22 17:36

    Actually the subclassing idea works great. Just don't make your delegate the UIApplication subclass. Create another file that inherits from UIApplication (e.g. myApp). In IB set the class of the fileOwner object to myApp and in myApp.m implement the sendEvent method as above. In main.m do:

    int retVal = UIApplicationMain(argc,argv,@"myApp.m",@"myApp.m")
    

    et voilà!

    0 讨论(0)
  • 2020-11-22 17:44

    Here's the answer I had been looking for:

    Have your application delegate subclass UIApplication. In the implementation file, override the sendEvent: method like so:

    - (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");
    }
    

    where maxIdleTime and idleTimer are instance variables.

    In order for this to work, you also need to modify your main.m to tell UIApplicationMain to use your delegate class (in this example, AppDelegate) as the principal class:

    int retVal = UIApplicationMain(argc, argv, @"AppDelegate", @"AppDelegate");
    
    0 讨论(0)
提交回复
热议问题