开屏广告简单例子

1.打开终端pod ‘XHLaunchAd’
2.导入头文件
3.拖入几个文件
SCGuideViewController.h

#import <UIKit/UIKit.h>

@interface SCGuideViewController : UIViewController

- (id)initWithViewController:(UIViewController *)controller;

@end

SCGuideViewController.m


#import "SCGuideViewController.h"
#import "CALayer+Transition.h"
#import "UIView+Uitls.h"

@interface SCGuideViewController () <UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *pageScroll;
@property (nonatomic, strong) UIViewController *con;

@end

@implementation SCGuideViewController

- (id)initWithViewController:(UIViewController *)controller
{
    if (self = [super init]) {
        _con = controller;
    }
    return self;
}

- (void)loadView
{
    [super loadView];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSArray *imageNameArray = [NSArray arrayWithObjects:@"引导1",@"引导2", @"引导3" ,nil];

    _pageScroll = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    _pageScroll.bounces = NO;
    _pageScroll.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    _pageScroll.showsHorizontalScrollIndicator = NO;
    _pageScroll.delegate = self;
    _pageScroll.pagingEnabled = YES;
    _pageScroll.contentSize = CGSizeMake(self.view.frame.size.width * imageNameArray.count, self.view.frame.size.height);
    [self.view addSubview:_pageScroll];


    NSString *imageName = nil;
    for (int i = 0; i < imageNameArray.count; i++) {
        imageName = [imageNameArray objectAtIndex:i];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
        imageView.frame = [UIScreen mainScreen].bounds;
        imageView.origin = CGPointMake(i * imageView.width, 0);
        imageView.contentMode = UIViewContentModeScaleToFill;
        imageView.userInteractionEnabled = YES;
        [self.pageScroll addSubview:imageView];

        UIButton *enterBtn = nil;
        if (i == imageNameArray.count - 1) {

            enterBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, kDeviceWidth, kDeviceHeight)];

            [enterBtn setBackgroundImage:[UIImage imageNamed:@"entry"] forState:UIControlStateNormal];
            [enterBtn setBackgroundImage:[UIImage imageNamed:@"entry"] forState:UIControlStateHighlighted];
            [enterBtn addTarget:self action:@selector(enter) forControlEvents:UIControlEventTouchUpInside];
            [imageView addSubview:enterBtn];
        }

    }
    [_pageScroll setContentOffset:CGPointMake(0.f, 0.f)];
}

- (BOOL)prefersStatusBarHidden
{
    return YES; // 返回NO表示要显示,返回YES将hiden
}

- (void)enter
{
//    [self transition:TransitionAnimTypeRippleEffect];
      [UIApplication sharedApplication].delegate.window.rootViewController = _con;
}


- (void)transition:(TransitionAnimType)type
{
    [UIApplication sharedApplication].delegate.window.rootViewController = _con;
    [[UIApplication sharedApplication].delegate.window.layer transitionWithAnimType:type subType:TransitionSubtypesFromRight curve:TransitionCurveLinear duration:0.6f];
}

@end

CALayer_Transition.h


#import <QuartzCore/QuartzCore.h>

@interface CALayer (Transition)

/*
 *  动画类型
 */
typedef enum{

    //水波
    TransitionAnimTypeRippleEffect=0,

    //吸走
    TransitionAnimTypeSuckEffect,

    //翻开书本
    TransitionAnimTypePageCurl,

    //正反翻转
    TransitionAnimTypeOglFlip,

    //正方体
    TransitionAnimTypeCube,

    //push推开
    TransitionAnimTypeReveal,

    //合上书本
    TransitionAnimTypePageUnCurl,

    //镜头开
    TransitionAnimTypeCameraIrisHollowOpen,

    //镜头关
    TransitionAnimTypeCameraIrisHollowClose,

    //随机
    TransitionAnimTypeRamdom,

}TransitionAnimType;




/*
 *  动画方向
 */
typedef enum{

    //从上
    TransitionSubtypesFromTop=0,

    //从左
    TransitionSubtypesFromLeft,

    //从下
    TransitionSubtypesFromBotoom,

    //从右
    TransitionSubtypesFromRight,

    //随机
    TransitionSubtypesFromRamdom,

}TransitionSubType;


/*
 *  动画曲线
 */
typedef enum {

    //默认
    TransitionCurveDefault,

    //缓进
    TransitionCurveEaseIn,

    //缓出
    TransitionCurveEaseOut,

    //缓进缓出
    TransitionCurveEaseInEaseOut,

    //线性
    TransitionCurveLinear,

    //随机
    TransitionCurveRamdom,

}TransitionCurve;




/**
 *  转场动画
 *
 *  @param animType 转场动画类型
 *  @param subType  转动动画方向
 *  @param curve    转动动画曲线
 *  @param duration 转动动画时长
 *
 *  @return 转场动画实例
 */
-(CATransition *)transitionWithAnimType:(TransitionAnimType)animType subType:(TransitionSubType)subType curve:(TransitionCurve)curve duration:(CGFloat)duration;

@end

CALayer_Transition.m

#import "CALayer+Transition.h"

@implementation CALayer (Transition)


/**
 *  转场动画
 *
 *  @param animType 转场动画类型
 *  @param subtype  转动动画方向
 *  @param curve    转动动画曲线
 *  @param duration 转动动画时长
 *
 *  @return 转场动画实例
 */
-(CATransition *)transitionWithAnimType:(TransitionAnimType)animType subType:(TransitionSubType)subType curve:(TransitionCurve)curve duration:(CGFloat)duration{

    NSString *key = @"transition";

    if([self animationForKey:key]!=nil){
        [self removeAnimationForKey:key];
    }


    CATransition *transition=[CATransition animation];

    //动画时长
    transition.duration=duration;

    //动画类型
    transition.type=[self animaTypeWithTransitionType:animType];

    //动画方向
    transition.subtype=[self animaSubtype:subType];

    //缓动函数
    transition.timingFunction=[CAMediaTimingFunction functionWithName:[self curve:curve]];

    //完成动画删除
    transition.removedOnCompletion = YES;

    [self addAnimation:transition forKey:key];

    return transition;
}



/*
 *  返回动画曲线
 */
-(NSString *)curve:(TransitionCurve)curve{

    //曲线数组
    NSArray *funcNames=@[kCAMediaTimingFunctionDefault,kCAMediaTimingFunctionEaseIn,kCAMediaTimingFunctionEaseInEaseOut,kCAMediaTimingFunctionEaseOut,kCAMediaTimingFunctionLinear];

    return [self objFromArray:funcNames index:curve isRamdom:(TransitionCurveRamdom == curve)];
}



/*
 *  返回动画方向
 */
-(NSString *)animaSubtype:(TransitionSubType)subType{

    //设置转场动画的方向
    NSArray *subtypes=@[kCATransitionFromTop,kCATransitionFromLeft,kCATransitionFromBottom,kCATransitionFromRight];

    return [self objFromArray:subtypes index:subType isRamdom:(TransitionSubtypesFromRamdom == subType)];
}




/*
 *  返回动画类型
 */
-(NSString *)animaTypeWithTransitionType:(TransitionAnimType)type{

    //设置转场动画的类型
    NSArray *animArray=@[@"rippleEffect",@"suckEffect",@"pageCurl",@"oglFlip",@"cube",@"reveal",@"pageUnCurl",@"cameraIrisHollowOpen",@"cameraIrisHollowClose"];

    return [self objFromArray:animArray index:type isRamdom:(TransitionAnimTypeRamdom == type)];
}



/*
 *  统一从数据返回对象
 */
-(id)objFromArray:(NSArray *)array index:(NSUInteger)index isRamdom:(BOOL)isRamdom{

    NSUInteger count = array.count;

    NSUInteger i = isRamdom?arc4random_uniform((u_int32_t)count) : index;

    return array[i];
}



@end

UIView_Uitls.h

//
//  UIView+Uitls.h
//  SCAutoMove
//
//  Created by show class on 15/11/25.
//  Copyright © 2015年 show class. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface UIView (Uitls)

- (void)setBorderWidth:(CGFloat)with;

- (CGFloat)borderWidth;

- (void)setBorderColor:(UIColor *)color;

- (UIColor *)borderColor;

- (CGFloat)x;

- (CGFloat)y;

- (CGFloat)width;

- (CGFloat)height;

- (CGPoint)origin;

- (CGSize)size;

- (void)setX:(CGFloat)x;

- (void)setY:(CGFloat)y;

- (void)setWidth:(CGFloat)width;

- (void)setHeight:(CGFloat)height;

- (void)setOrigin:(CGPoint)origin;

- (void)setSize:(CGSize)size;

- (CGFloat)left;

- (CGFloat)right;

- (CGFloat)top;

- (CGFloat)bottom;
@end

@interface UIView(FindFirstResponder)
- (UIView *)findFirstResponder;
@end

UIView_Uitls.m
//
// UIView+Uitls.m
// SCAutoMove
//
// Created by show class on 15/11/25.
// Copyright © 2015年 show class. All rights reserved.
//

import “UIView+Uitls.h”

@implementation UIView (Uitls)

  • (void)setBorderWidth:(CGFloat)with
    {
    self.layer.borderWidth = with;
    }

  • (CGFloat)borderWidth
    {
    return self.layer.borderWidth;
    }

  • (void)setBorderColor:(UIColor *)color
    {
    self.layer.borderColor = color.CGColor;
    }

  • (UIColor *)borderColor
    {
    return [UIColor colorWithCGColor:self.layer.borderColor];
    }

  • (CGFloat)x
    {
    return self.frame.origin.x;
    }

  • (CGFloat)y
    {
    return self.frame.origin.y;
    }

  • (CGFloat)width
    {
    return self.frame.size.width;
    }

  • (CGFloat)height
    {
    return self.frame.size.height;
    }

  • (CGPoint)origin
    {
    return self.frame.origin;
    }

  • (CGSize)size
    {
    return self.frame.size;
    }

  • (void)setX:(CGFloat)x
    {
    self.frame = (CGRect){
    .origin = {.x = x, .y = self.y},
    .size = {.width = self.width, .height = self.height}
    };
    }

  • (void)setY:(CGFloat)y
    {
    self.frame = (CGRect){
    .origin = {.x = self.x, .y = y},
    .size = {.width = self.width, .height = self.height}
    };
    }

  • (void)setWidth:(CGFloat)width
    {
    self.frame = (CGRect){
    .origin = {.x = self.x, .y = self.y},
    .size = {.width = width, .height = self.height}
    };
    }

  • (void)setHeight:(CGFloat)height
    {
    self.frame = (CGRect){
    .origin = {.x = self.x, .y = self.y},
    .size = {.width = self.width, .height = height}
    };
    }

  • (void)setOrigin:(CGPoint)origin
    {
    self.frame = (CGRect){
    .origin = {.x = origin.x, .y = origin.y},
    .size = {.width = self.width, .height = self.height}
    };
    }

  • (void)setSize:(CGSize)size
    {
    self.frame = (CGRect){
    .origin = {.x = self.x, .y = self.y},
    .size = {.width = size.width, .height = size.height}
    };
    }

  • (CGFloat)left
    {
    return self.frame.origin.x;
    }

  • (CGFloat)right
    {
    return self.frame.origin.x + self.frame.size.width;
    }

  • (CGFloat)top
    {
    return self.frame.origin.y;
    }

  • (CGFloat)bottom
    {
    return self.frame.origin.y + self.frame.size.height;
    }

@end

@implementation UIView(FindFirstResponder)

  • (UIView *)findFirstResponder
    {
    if (self.isFirstResponder) {
    return self;
    }
    for (UIView *subView in self.subviews) {
    UIView *responder = [subView findFirstResponder];
    if (responder) return responder;
    }
    return nil;
    }

@end

这里写代码片

4.//appdelegate中加载广告页

“`
[self loadData];

if(![[NSUserDefaults standardUserDefaults] boolForKey:@"firstStart"]){
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstStart"];
    NSLog(@"第一次启动");
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    RootViewController *loginVC = [[RootViewController alloc] init];
    self.window.rootViewController = [[SCGuideViewController alloc] initWithViewController:loginVC];
    self.isReachable = YES;
}else{
    RootViewController *loginVC = [[RootViewController alloc]init];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    self.window.rootViewController = loginVC;
    self.isReachable = YES;
}
[XHLaunchAd setWaitDataDuration:3];
//加载数据源并展示

- (void)getGuangGaoData{
//横幅
NSString *LM_ID = [_APPDataSourceArr[5] objectForKey:@”LM_ID”];
NSString *urlStr =[NSString stringWithFormat:@”%@%@?LM_ID=%@”,[UPConfig getServerURL] ,@”/cmsfw/selectCMS_LM_NR”,LM_ID];
[NetEngine GetRequestWithUrlstring:urlStr paramater:nil progress:^(NSProgress *uploadProgress) {
} success:^(id responseObject) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves | NSJSONReadingMutableContainers error:nil];
NSLog(@”开屏广告———-%@———-%@”,urlStr,dic);
if ([dic[@”code”] integerValue] == 0) {
NSMutableArray *imageArr = [[NSMutableArray alloc]init];//广告图片
_urlArr = [[NSMutableArray alloc]init];//图片链接
if ([dic[@”data”] count] == 0) {

        }else{
            for (NSDictionary *dict in dic[@"data"]) {
                [imageArr addObject:dict[@"IMG_PATH"]];
                [_urlArr addObject:dict];
            }
        }
            XHLaunchImageAdConfiguration *imageAdconfiguration = [XHLaunchImageAdConfiguration defaultConfiguration];
            if (imageArr.count >1) {
                //隐藏跳过倒计时按钮
                imageAdconfiguration.skipButtonType = SkipTypeNone;
                imageAdconfiguration.duration = imageArr.count *5;
                UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kDeviceWidth, kDeviceHeight)];
                //设置ScrollView的整体触摸与显示区域
                //注意 宽 高不要超过 320X480
                //否则会出现无法滚动的情况
                _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kDeviceWidth,kDeviceHeight)];

                //设置ScrollView滚动内容的区域
                //它通常是需要大于ScrollerView的显示区域的
                //这样才有必要在ScrollerView中滚动它
                [_scrollView setContentSize:CGSizeMake(kDeviceWidth * imageArr.count, kDeviceHeight)];

                //开启滚动分页功能,如果不需要这个功能关闭即可
                [_scrollView setPagingEnabled:YES];

                //隐藏横向与纵向的滚动条
                [_scrollView setShowsVerticalScrollIndicator:NO];
                [_scrollView setShowsHorizontalScrollIndicator:NO];

                //在本类中代理scrollView的整体事件
                [_scrollView setDelegate:self];


                for (int i =0; i<imageArr.count; i++)
                {

                    //在这里给每一个ScrollView添加一个图片 和一个按钮
                    UIImageView *imageView= [[UIImageView alloc] initWithFrame:CGRectMake(i * kDeviceWidth,0,kDeviceWidth,kDeviceHeight)];
                    NSLog(@"-------------%@",imageArr[i]);
                    [imageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",[UPConfig getServerURL],imageArr[i]]]];

                    UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                    button.frame = CGRectMake(i * kDeviceWidth, 10, kDeviceWidth, kDeviceHeight);
                    button.tag = i;

                    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

                    //把每页需要显示的VIEW添加进ScrollerView中
                    [_scrollView addSubview:imageView];
                    [_scrollView addSubview:button];
                }
                _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, kDeviceHeight-140,kDeviceWidth, 20)];
                [_pageControl setPageIndicatorTintColor:[UIColor blackColor]];
                [_pageControl setCurrentPageIndicatorTintColor:[UIColor redColor]];
                //设置页面的数量
                [_pageControl setNumberOfPages:imageArr.count];
                [view addSubview:_scrollView];
                [view addSubview:_pageControl];
                //整体再将ScrollerView显示在窗口中
                imageAdconfiguration.subViews = @[view];

            } else {
                //广告图片URLString/或本地图片名(.jpg/.gif请带上后缀)
                imageAdconfiguration.imageNameOrURLString =[NSString stringWithFormat:@"%@%@",[UPConfig getServerURL], dic[@"data"][0][@"IMG_PATH"]];
            }
            //广告点击打开链接
        NSString *str = [[dic[@"data"][0] allValues] componentsJoinedByString:@""];
        if ([str containsString:@"http"]) {
                NSLog(@"111==1=1=1=1=1=1=1=1==1");

// imageAdconfiguration.openURLString= dic[@”data”][0][@”NR_URL”];
imageAdconfiguration.openModel = dic[@”data”][0][@”NR_URL”];
}
//页面控制小工具
//它会在底部绘制小圆点标志当前显示页面
//显示开屏广告
[XHLaunchAd imageAdWithImageAdConfiguration:imageAdconfiguration delegate:self];
}else{
}
} failed:^(NSError *error) {
}];
}
4.scrollview协议方法
-(void)scrollViewDidScroll:(UIScrollView*)scrollView

{
//页面滚动时调用,设置当前页面的ID
[_pageControl setCurrentPage:fabs(scrollView.contentOffset.x/kDeviceWidth)];
NSLog(@”页数==========%f”,fabs(scrollView.contentOffset.x/kDeviceWidth));
NSLog(@”偏移量==========%f”,scrollView.contentOffset.x);
if (scrollView.frame.size.width>kDeviceWidth) {
// [XHLaunchAd skipAction];
[XHLaunchAd removeAndAnimated:YES];
}
NSLog(@”视图滚动中X轴坐标%f”,scrollView.contentOffset.x);
NSLog(@”视图滚动中X轴坐标%f”,scrollView.contentOffset.y);
}
-(void)buttonClick:(UIButton *)btn
{
// NSDictionary *dict = [[NSDictionary alloc]initWithDictionary:_urlArr[0]];
// if ([[dict allKeys] containsObject:@”url”]){
// WebLoginVC *login = [[WebLoginVC alloc]init];
// login.webURL = dict[@”url”];
// [self.window.rootViewController.navigationController pushViewController:login animated:YES];
// }
}
-(void)scrollViewDidEndDragging:(UIScrollView*)scrollView willDecelerate:(BOOL)decelerate
{
NSLog(@”滚动视图结束滚动,它只调用一次”);

}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSLog(@”%f”,_scrollView.frame.size.width);

}“`

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值