IOS-开屏广告页

  • 思路
    在一个新的UIWindow的最上层添加一个控制器 用SDWebImage异步加载从接口获取的图片,把图片平铺在这个控制器上。最后配合UIView的渐隐动画再几秒后移除这个控制器

  • 注意
    为了不对Appdelegate有更多的入侵 我们写了一个UIWindow的Category,命名为Expand

  • 在Appdelegate的中

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[UIWindow alloc] initWithFrame:kScreenBounds];
    
    self.mainViewController = [[MainController alloc] init];

    self.window.rootViewController = self.mainViewController;
    [self.window makeKeyAndVisible];
    
    [self.window showLanuchPageAndSetUmeng];
    
    return YES;
}
  • 在UIWindow+Expand中
- (void)showLanuchPageAndSetUmeng{

    LaunchViewController *launchVC = [[LaunchViewController alloc] init];
    //在新的windon上添加一个控制器LaunchViewController
    [self addSubview:launchVC.view];

}
  • 在LaunchViewController.m中
#import "LaunchViewController.h"
#import "UIImageView+WebCache.h"

static CGFloat const animationDuration = 3.0;

@interface LaunchViewController ()

@property (nonatomic, strong) UIImageView *launchImage;

@end

@implementation LaunchViewController

#pragma mark - life cycle

- (void)viewDidLoad{
    [super viewDidLoad];
    
    [self.view addSubview:self.launchImage];
//    把开屏广告页置于这个控制器的最上层
    [self.view bringSubviewToFront:self.launchImage];
    [self.view addSubview:self.imageTitle];
//    获得启动动画图片
    [self updateLanunchImage];

}

#pragma mark - private method
- (void)updateLanunchImage{
    
    [HttpTool get:@"http://news-at.zhihu.com/api/4/start-image/1080*1776" params:nil success:^(id json) {
       //从接口获取图片转为model赋值给LaunchImage这个类里的img
        LaunchImage *lanunchImage = [LaunchImage mj_objectWithKeyValues:json];
        
        SDWebImageManager *manger = [SDWebImageManager sharedManager];
        
        [manger downloadImageWithURL:[NSURL URLWithString:lanunchImage.img] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            
            nil;
            
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
     
            self.launchImage.image = image;

            [UIView animateWithDuration:animationDuration animations:^{
                _launchImage.transform = CGAffineTransformMakeScale(1.2, 1.2);
            } completion:^(BOOL finished) {
                  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.7 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self.view removeFromSuperview];
                });         
         }];
        }];
        
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark - getter and setter

- (UIImageView *)launchImage{
    if (_launchImage == nil) {
        _launchImage = [[UIImageView alloc] initWithFrame:kScreenBounds];
        _launchImage.image = [UIImage imageNamed:@"Default"];
    }
    return _launchImage;
}

你可能感兴趣的:(IOS-开屏广告页)