iOS开发-自定义后台显示图片(iOS7-Background Fetch的应用)

来源:互联网 发布:mysql 5.5.37 for mac 编辑:程序博客网 时间:2024/06/11 10:04

本文转自http://blog.csdn.net/hitwhylz/article/details/25557725


之前在用电池医生的时候, 发现它有这样一个功能:当应用进入后台的时候, 会显示另外一张图片覆盖App Switcher显示的界面。

效果如下:

变成---->


而这样的一个功能, 对于保护用户隐私还是挺有用的。

这就涉及到了Background Fetch的使用。当然, Background Fetch有更多,更有用的功能, 

详见-->iOS 7学习:多任务处理之Background Fetch


下面就介绍下后台图片切换的实现。


1.程序配置后台模式

操作如下:


2. AppDelegate.h添加代码:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. @property (strong, nonatomic) UIImageView *splashView;  

3.AppDelegate.m添加代码:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  AppDelegate.m  
  3. //  FuckTest  
  4. //  
  5. //  Created by Colin on 14-4-5.  
  6. //  Copyright (c) 2014年 icephone. All rights reserved.  
  7. //  
  8.   
  9. #import "AppDelegate.h"  
  10.   
  11. @implementation AppDelegate  
  12. @synthesize splashView;  
  13.   
  14. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
  15. {  
  16.     // Override point for customization after application launch.  
  17.       
  18.     [application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];  
  19.   
  20.     [self.window makeKeyAndVisible];  
  21.     splashView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 568)];  
  22.     [splashView setImage:[UIImage imageNamed:@"Default-568h.png"]];  
  23.       
  24.     return YES;  
  25. }  
  26.   
  27. - (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler  
  28. {  
  29.     NSLog(@"后台获取");  
  30.     completionHandler(UIBackgroundFetchResultNewData);  
  31. }  
  32.                               
  33. - (void)applicationWillResignActive:(UIApplication *)application  
  34. {  
  35.     // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.  
  36.     // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.  
  37. }  
  38.   
  39. - (void)applicationDidEnterBackground:(UIApplication *)application  
  40. {  
  41.       
  42.     [self.window addSubview:splashView];  
  43.     [self.window bringSubviewToFront:splashView];  
  44.     // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.   
  45.     // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.  
  46. }  
  47.   
  48. - (void)applicationWillEnterForeground:(UIApplication *)application  
  49. {  
  50.   
  51.     [self.splashView removeFromSuperview];  
  52.     // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.  
  53. }  
  54.   
  55. - (void)applicationDidBecomeActive:(UIApplication *)application  
  56. {  
  57.     // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.  
  58. }  
  59.   
  60. - (void)applicationWillTerminate:(UIApplication *)application  
  61. {  
  62.     // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.  
  63. }  
  64.   
  65. @end  

0 0
原创粉丝点击