ios 类似于网易的启动页广告

效果图:


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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值