ios 类似于网易的启动页广告
来源:互联网 发布:java常用英文单词 编辑:程序博客网 时间:2024/05/16 16:05
效果图:
1.使用:将广告添加到window上面
UIWindow *_window = [UIApplication sharedApplication].delegate.window; [TFLaunchAd showAdAtPath:@"http://img1.126.net/channel6/2015/ad/2_1224a.jpg" onView:_window timeInterval:10 detailParameters:nil];
2.广告类的源码如下
#import <Foundation/Foundation.h>@import UIKit.UIView;extern NSString *TFLaunchAdDetailDisplayNotification;@interface TFLaunchAd : NSObject+ (void)showAdAtPath:(NSString *)path onView:(UIView *)container timeInterval:(NSTimeInterval)interval detailParameters:(NSDictionary *)param;@end
#import "TFLaunchAd.h"@import UIKit.UIScreen;@import UIKit.UIImage;@import UIKit.UIImageView;@import UIKit.UIButton;@import UIKit.UILabel;@import UIKit.UIColor;@import UIKit.UIFont;@import QuartzCore.CALayer;NSString *TFLaunchAdDetailDisplayNotification = @"TFShowLaunchAdDetailNotification";static TFLaunchAd *monitor = nil;@interface TFLaunchAd()<NSURLConnectionDataDelegate, NSURLConnectionDelegate>{ }@property (nonatomic, assign) BOOL imgLoaded;@property (nonatomic, strong) NSMutableData *imgData;@property (nonatomic, strong) NSURLConnection *conn;@property (nonatomic, strong) NSMutableDictionary *detailParam;@end@implementation TFLaunchAd+ (void)showAdAtPath:(NSString *)path onView:(UIView *)container timeInterval:(NSTimeInterval)interval detailParameters:(NSDictionary *)param{ if (![self validatePath:path]) { return ; } [[self defaultMonitor] loadImageAtPath:path]; while (!monitor.imgLoaded) { [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]; } [monitor.detailParam removeAllObjects]; [monitor.detailParam addEntriesFromDictionary:param]; [self showImageOnView:container forTime:interval];}+ (instancetype)defaultMonitor{ @synchronized (self) { if (!monitor) { monitor = [[TFLaunchAd alloc] init]; monitor.detailParam = [NSMutableDictionary dictionary]; } return monitor; }}+ (BOOL)validatePath:(NSString *)path{ NSURL *url = [NSURL URLWithString:path]; return url != nil;}+ (void)showImageOnView:(UIView *)container forTime:(NSTimeInterval)time{ CGRect f = [UIScreen mainScreen].bounds; NSLog(@"screen size:%@", NSStringFromCGRect(f)); UIView *v = [[UIView alloc] initWithFrame:f]; v.backgroundColor = [UIColor whiteColor]; f.size.height -= 50; UIImageView *iv = [[UIImageView alloc] initWithFrame:f]; iv.image = [UIImage imageWithData:monitor.imgData]; monitor.conn = nil; [monitor.imgData setLength:0]; iv.contentMode = UIViewContentModeScaleAspectFill; iv.clipsToBounds = YES; [v addSubview:iv]; [container addSubview:v]; [container bringSubviewToFront:v]; UIButton *showDetailBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [showDetailBtn setTitle:@"详情>>" forState:UIControlStateNormal]; [showDetailBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [showDetailBtn setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted]; showDetailBtn.titleLabel.font = [UIFont systemFontOfSize:13]; showDetailBtn.frame = CGRectMake(f.size.width - 70, 30, 60, 30); showDetailBtn.layer.borderColor = [UIColor whiteColor].CGColor; showDetailBtn.layer.borderWidth = 1.0f; showDetailBtn.layer.cornerRadius = 3.0f; [showDetailBtn addTarget:self action:@selector(showAdDetail:) forControlEvents:UIControlEventTouchUpInside]; [v addSubview:showDetailBtn]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, f.size.height + 10, f.size.width - 20, 30)]; label.backgroundColor = [UIColor clearColor]; label.text = @"©2016 Robinson_911. All Rights Reserved"; label.textAlignment = NSTextAlignmentCenter; label.font = [UIFont systemFontOfSize:15]; [v addSubview:label]; label = nil; [container addSubview:v]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(time * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ v.userInteractionEnabled = NO; [UIView animateWithDuration:.25 animations:^{ v.alpha = 0.0f; } completion:^(BOOL finished) { [v removeFromSuperview]; }]; });}+ (void)showAdDetail:(id)sender{ UIView *sup = [(UIButton *)sender superview]; sup.userInteractionEnabled = NO; [UIView animateWithDuration:.25 animations:^{ sup.alpha = 0.0f; } completion:^(BOOL finished) { [sup removeFromSuperview]; [[NSNotificationCenter defaultCenter] postNotificationName:TFLaunchAdDetailDisplayNotification object:monitor.detailParam]; [monitor.detailParam removeAllObjects]; }]; }- (void)loadImageAtPath:(NSString *)path{ NSURL *URL = [NSURL URLWithString:path]; NSURLRequest *request = [NSURLRequest requestWithURL:URL]; self.conn = [NSURLConnection connectionWithRequest:request delegate:self]; if (self.conn) { [self.conn start]; }}#pragma mark - NSURLConnectionDataDelegate method- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ NSHTTPURLResponse *resp = (NSHTTPURLResponse *)response; if (resp.statusCode != 200) { self.imgLoaded = YES; return ; } self.imgData = [NSMutableData data];}- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ [self.imgData appendData:data];}- (void)connectionDidFinishLoading:(NSURLConnection *)connection{ self.imgLoaded = YES;}- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ NSLog(@"图片数据获取失败"); self.imgLoaded = YES;}@end
0 0
- ios 类似于网易的启动页广告
- 仿网易新闻广告启动页的实现
- iOS 启动页广告
- iOS广告启动页
- iOS启动页广告JYJAdViewController
- iOS 启动页加入动态的广告实现
- 类似于网易新闻首页的滑动效果
- iOS 录屏大师启动页广告
- ios App启动加载广告页
- iOS开发——启动广告页
- iOS启动页广告swift3.0
- iOS 快速集成启动页广告
- iOS 快速集成启动页广告
- iOS 快速集成启动页广告
- iOS-APP启动页加载广告
- iOS App启动广告
- 启动App的广告页
- ios 类似于网易广告栏许多栏目Button 点击变颜色
- AABB碰撞盒
- nginx+GraphicsMagick实现缩略图、图片实时缩放
- Ccookie被禁用session还能用么?
- svn使用openldap验证apache访问方式
- Hive常用命令
- ios 类似于网易的启动页广告
- 在ubuntu14.04下安装hbase1.1.3与hadoop2.6.4的相关配置
- 解决 Warning: calling DestroyWindow in CWnd::~CWnd OnDestroy or PostNcDestroy in derived class will n
- eclipse 下的 android 模拟器联网
- Linux性能测试 netstat命令
- 设计模式之工厂模式
- reveal 1.6.3 本机破解及使用
- Linux进程间通信
- sqlsever 与 Java 数据类型的对应