iOS 自定义自动锁屏时间

转载自:http://www.cnblogs.com/kelisi-king/archive/2012/12/03/2800215.html

 加一条:老大提醒,在面向对象的编程语言里面最好不要有无从属关系的全局变量,被各处引用~不好!

   如何自定义一个time interval 在用户停止任何操作后延迟触发锁屏操作,跳转到自定义的锁屏界面,蛋痛中。。。

思路:

1)- (void)applicationWillResignActive:(UIApplication *)application //(现在了解到 此为错误想法)
说明:当应用程序将要入非活动状态执行,在此期间,应用程序不接收消息或事件,比如来电话了 (但还未找到如何设置从active到inactive的时间间隔!!!)

2)首先:设置一个NSTimer 定时器。

  然后:设置一个UIResponder 捕获各个View 下面的

  - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
     //NSTime 置零
}

  最后:在一个主线程里(还不确定在哪,初步定在Appdelegate.m中)根据NSTime的值判断跳转至自己写的锁屏界面。

  总结:可行性高,设计复杂。

  实现:未完待续。。。。

  结果:搞定~~ 哈哈哈

http://stackoverflow.com/questions/9994230/recognise-all-touches-in-interface APPDELEGATE 替换UIWindow

总结下实现过程,为后人提供些许思路也好~(菜鸟一枚)

1)首先想到的是,定义一个全局的计时器NSTimer, 然后在进入各个view视图时计时开始,然后捕获USER 的触摸和输入操作,将计时器清零。可惜,经查证,NSTimer只能在一个Thread里面进行计时的开始与停止的操作。跨线程无效。然后就想在main thread里面起一个计时器,在各线程下对该NSTimer进行操作,又可惜。。。太菜,不会弄。

定时器的具体实现:在Appdelegate里面,添加startTimer、zeroTimer、gotoLockScreen的方法。

复制代码
- (void)lockSecBrowserScreen
{
    LOGNOTICE(@"go to lock screen ");
    if (currentViewController) {
        ScreenLockViewController *lockScreen = [[ScreenLockViewController alloc] init];
        
        LOGNOTICE(@"appdelegate.delegate : %@",currentViewController);
        
        [currentViewController presentModalViewController:lockScreen animated:YES];
    }
}


//tap gesture  action
- (void)zeroTimer{
    LOGNOTICE(@"capture gesture & set timer zero");
    
    if (!g_timeCount) {
        g_timeCount = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:nil userInfo:nil repeats:NO];
    }
    
    [g_timeCount invalidate];
    g_timeCount = nil;
    
}

//start the NSTimer
- (void)startTimer {
    LOGNOTICE(@"NSTimer start");
    BOOL enableSecBrowserScreen = YES;
    if (enableSecBrowserScreen) {
        if (!g_timeCount) {
            screenLockTimeInterval =  15;
            g_timeCount = [NSTimer scheduledTimerWithTimeInterval:screenLockTimeInterval target:self selector:@selector(lockSecBrowserScreen) userInfo:nil repeats:YES];
        }
    }
}
复制代码

然后在进入每个视图后,自己是添加在判断是否横竖屏切换的函数里(只要进视图,无论以任何方式都会走该函数(popover除外)),创建 appdelegate的实例,赋值为UIApplication的delegate,调用zeroTimer并startTimer(程序架构已经写 好,所以说这边封装的不好,频繁调用重复的操作),并将appdelegate的实例的currentviewcontroller设为self。至此, 计时器部分完成。

然后,就是用户交互的识别。

  1.Touch:捕获用户的touch操作,可以在appdelegate里面subclass系统的UIWindow类,重写- (void)sendEvent:(UIEvent *)event系统函数

  实现:

复制代码
@iinterface myWindow:UIWindow
@end

@implementation myWindow

- (void)sendEvent:(UIEvent *)event
{
    AppDelegate *appdelegate = [UIApplication sharedApplication].delegate;
    if (appdelegate.isInBrowser) {
        if (event.type == UIEventTypeTouches || event.type == UIEventTypeMotion || event.type == UIEventTypeRemoteControl) {
            //set NSTimer zero and count again
            LOGNOTICE(@"Set Timer Zero & Fire Again According USER TAPS");
            [appdelegate zeroTimer];
            [appdelegate startTimer];
            }
    }
    [super sendEvent:event];
}
复制代码

 2) 键盘的input:当用户在UITextfield里面输入字符,触摸键盘时,是不会被识别为Window的手势操作。就需要在消息机制里捕获键盘输入。然后在selector自己的方法,实现想要进行的操作(stopTime,startTime)

复制代码
  [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textfieldChanged) name:UITextFieldTextDidChangeNotification object:_nameTextField];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textfieldChanged) name:UITextFieldTextDidBeginEditingNotification object:_nameTextField];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textfieldChanged) name:UITextFieldTextDidEndEditingNotification object:_nameTextField];
 
复制代码

OVER~!! 
(水平有限,仅以提供思路为准则

原文地址:https://www.cnblogs.com/zhangsongbai/p/3102007.html