Skip to content

Instantly share code, notes, and snippets.

@ashikahmad
Forked from KingOfBrian/KSDIdlingWindow.h
Created October 21, 2013 17:23
Show Gist options
  • Save ashikahmad/7087553 to your computer and use it in GitHub Desktop.
Save ashikahmad/7087553 to your computer and use it in GitHub Desktop.
//
// KSDIdlingWindow.h
//
// Created by Brian King on 4/13/10.
// Copyright 2010 King Software Designs. All rights reserved.
//
// Based off:
// http://stackoverflow.com/questions/273450/iphone-detecting-user-inactivity-idle-time-since-last-screen-touch
//
#import <UIKit/UIKit.h>
extern NSString * const KSDIdlingWindowIdleNotification;
extern NSString * const KSDIdlingWindowActiveNotification;
@interface KSDIdlingWindow : UIWindow {
NSTimer *idleTimer;
NSTimeInterval idleTimeInterval;
}
@property (assign) NSTimeInterval idleTimeInterval;
@property (nonatomic, retain) NSTimer *idleTimer;
@end
//
// KSDIdlingWindow.m
//
// Created by Brian King on 4/13/10.
// Copyright 2010 King Software Designs. All rights reserved.
//
// Based off:
// http://stackoverflow.com/questions/273450/iphone-detecting-user-inactivity-idle-time-since-last-screen-touch
//
#import "KSDIdlingWindow.h"
NSString * const KSDIdlingWindowIdleNotification = @"KSDIdlingWindowIdleNotification";
NSString * const KSDIdlingWindowActiveNotification = @"KSDIdlingWindowActiveNotification";
@interface KSDIdlingWindow (PrivateMethods)
- (void)windowIdleNotification;
- (void)windowActiveNotification;
@end
@implementation KSDIdlingWindow
@synthesize idleTimer, idleTimeInterval;
- (id) initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.idleTimeInterval = 0;
}
return self;
}
#pragma mark activity timer
- (void)sendEvent:(UIEvent *)event {
[super sendEvent:event];
NSSet *allTouches = [event allTouches];
if ([allTouches count] > 0) {
// To reduce timer resets only reset the timer on a Began or Ended touch.
UITouchPhase phase = ((UITouch *)[allTouches anyObject]).phase;
if (phase == UITouchPhaseBegan || phase == UITouchPhaseEnded) {
if (!idleTimer) {
[self windowActiveNotification];
} else {
[idleTimer invalidate];
}
if (idleTimeInterval != 0) {
self.idleTimer = [NSTimer scheduledTimerWithTimeInterval:idleTimeInterval
target:self
selector:@selector(windowIdleNotification)
userInfo:nil repeats:NO];
}
}
}
}
- (void)windowIdleNotification {
NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter];
[dnc postNotificationName:KSDIdlingWindowIdleNotification
object:self
userInfo:nil];
self.idleTimer = nil;
}
- (void)windowActiveNotification {
NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter];
[dnc postNotificationName:KSDIdlingWindowActiveNotification
object:self
userInfo:nil];
}
- (void)dealloc {
if (self.idleTimer) {
[self.idleTimer invalidate];
self.idleTimer = nil;
}
[super dealloc];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment