Skip to content

@KingOfBrian /KSDIdlingWindow.h
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
//
// 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
@astralbodies

Thanks for the code snippet - definitely made it easy to see where I needed to start.

@raunakp

Thanks for the snippet - very helpful!

@swalehapatanwala

But how can we use it further? I have set my rootviewcontrollers to this customwindow object. But it doesn't give any results?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.