標籤:
有許多應用程式在開啟的時候,歡迎介面會載入一張連網擷取的廣告圖片或者顯示一組動畫,這樣的效果是如何做到的呢?下面給大家介紹一種簡單的實現載入廣告的方式。
程式運行起來,歡迎介面之後,會進入AppDelegate,因此我們可以在application: didFinishLaunchingWithOptions:添加程式碼完成想要的效果。連網擷取圖片可以用第三方SDWebImage實現,所以需要先將第三方檔案夾匯入。因為顯示廣告的頁面是在歡迎介面基礎上顯示的,因此可以直接利用LaunchScreen.xib中得view,在上面添加一個UIImageView顯示圖片,然後將其加在window上,並顯示在最上層。廣告圖片顯示之後,再將view移除掉,顯示程式的主介面。代碼如下所示:
#import "AppDelegate.h"#import "UIImageView+WebCache.h"@interface AppDelegate ()@property (strong, nonatomic) UIView *lunchView;@end@implementation AppDelegate@synthesize lunchView;- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [self.window makeKeyAndVisible]; lunchView = [[NSBundle mainBundle ]loadNibNamed:@"LaunchScreen" owner:nil options:nil][0]; lunchView.frame = CGRectMake(0, 0, self.window.screen.bounds.size.width, self.window.screen.bounds.size.height); [self.window addSubview:lunchView]; UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 50, 320, 300)]; NSString *str = @"http://www.jerehedu.com/images/temp/logo.gif"; [imageV sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"default1.jpg"]]; [lunchView addSubview:imageV]; [self.window bringSubviewToFront:lunchView]; [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(removeLun) userInfo:nil repeats:NO]; return YES;}-(void)removeLun{ [lunchView removeFromSuperview];}
iOS歡迎介面Launch Screen動態載入廣告