美文网首页
启动页广告设置

启动页广告设置

作者: hoggenWang | 来源:发表于2017-07-15 01:53 被阅读22次

策略

广告显示策略=
   1)只有1张时,显示3秒;
   2)多余1张时,每张显示1秒,最多三张;
   3)首次加载不显示广告,并缓存广告(链接),第二次开始显示广告并缓存图片;
   4)缓存过期时间7天
   5)预留点击响应接口

这么晚了,还是直接上代码吧

class BannerView: UIView {

    private var scrollView: UIScrollView?
    public var currentPageIndex: Int = 0
    fileprivate var animationTimer: Timer?
    fileprivate var pageControl : UIPageControl?
    
    //block
    public var contentViewAtIndex : ((_ pageIndex: Int)->UIImageView)?
    public var tapActionBlock: ((_ pageIndex: Int)-> Void)?
    public var endActionBlock: (() -> Void)?
    //private
    private var contentViews : [UIImageView] = []
    fileprivate var animationInterval : TimeInterval?
    private var totalPages :  Int?
    private var jumpButton = UIButton(type: .custom);
    
    deinit {
        scrollView?.delegate = nil
        if self.animationTimer != nil {
            self.animationTimer?.invalidate();
            self.animationTimer = nil;
        }
    }
    
    public  init(frame: CGRect ,_ duration: TimeInterval,iamgeUrls: [URL]) {
        super.init(frame: frame)
        if duration > 0 {
            animationTimer = Timer.scheduledTimer(timeInterval: duration, target: self, selector: #selector(animationTimerDidFire(timer:)), userInfo: nil, repeats: true)
        }
        animationInterval = duration
        self.clipsToBounds = true
        scrollView = UIScrollView(frame: self.bounds)
        scrollView?.scrollsToTop = true
        scrollView?.isPagingEnabled = true
        scrollView?.delegate = self
        
        scrollView?.contentOffset = CGPoint(x: 0, y: 0)
        scrollView?.contentSize = CGSize(width: CGFloat(iamgeUrls.count) * ScreenWidth, height: self.bounds.size.height)
        self.addSubview(scrollView!)
        pageControl = UIPageControl(frame: CGRect(x: 0, y: self.bounds.size.height - 30, width: ScreenWidth, height: 10))
        pageControl?.isUserInteractionEnabled = false
        self.addSubview(pageControl!)
        for index in 0..<iamgeUrls.count {
            let imageView = UIImageView()
            imageView.setImage(with: iamgeUrls[index] )
            imageView.frame = CGRect(x: 0 + CGFloat(index) * ScreenWidth , y: 0, width: ScreenWidth, height: self.bounds.size.height)
            imageView.isUserInteractionEnabled = true;
            imageView.tag = 200 + index;
            let tapGesture: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(contentViewTapped(sender:)))
            imageView.addGestureRecognizer(tapGesture);
            scrollView?.addSubview(imageView);
            
        }
        jumpButton.frame = CGRect(x: ScreenWidth - 70.0, y: 34, width: 60, height: 30);
        jumpButton.backgroundColor = UIColor.black;
        jumpButton.alpha = 0.4;
        jumpButton.setTitle(String(format: "跳过(%d)", iamgeUrls.count), for: .normal)
        jumpButton.setTitleColor(UIColor.white, for: .normal)
        jumpButton.titleLabel?.font = UIFont.systemFont(ofSize: 13);
        jumpButton.addTarget(self, action: #selector(jumpButttonAction(sender:)), for: .touchUpInside)
        self.addSubview(jumpButton);
        
        
    }
    //MARK:设置总页数后,启动动画
    
    public  func setTotalPagesCount(totalPageCout: (()->(Int))) {
        self.totalPages = totalPageCout()
        if self.totalPages! > 3 {
            self.totalPages = 3;
        }
       // print("totalPages = \(self.totalPages)")
        
        self.pageControl?.numberOfPages = self.totalPages!
        pageControl?.backgroundColor = UIColor.clear
        pageControl?.isUserInteractionEnabled = true
        
        self.currentPageIndex = 0
        if self.totalPages == 1 {
            self.totalPages = 2
            scrollView?.contentSize = CGSize(width: ScreenWidth, height: self.bounds.size.height)
            //configureContentViews()
            self.pageControl?.isHidden = true
            animationTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(countDown(timer:)), userInfo: nil, repeats: true)
            self.animationTimer?.resume()
        }else{
            self.pageControl?.isHidden = false
        }
        if self.totalPages! > 0 && self.totalPages! != 1 {
            //configureContentViews()
            self.animationTimer?.resumeAfter(interval: self.animationInterval!)
        }
        
        
    }
    
    func countDown(timer:Timer) {
        self.totalPages = self.totalPages! - 1;
        jumpButton.setTitle(String(format: "跳过(%d)", self.totalPages! ), for: .normal)
        if self.totalPages == 0 {
            if let endActionBlock = self.endActionBlock {
                endActionBlock();
                scrollView?.delegate = nil
                if self.animationTimer != nil {
                    self.animationTimer?.invalidate();
                    self.animationTimer = nil;
                }
            }
        }
    }
    
    func jumpButttonAction(sender: UIButton) {
        if let endActionBlock = self.endActionBlock {
            endActionBlock();
            scrollView?.delegate = nil
            if self.animationTimer != nil {
                self.animationTimer?.invalidate();
                self.animationTimer = nil;
            }
        }
    }
    
    
    //点击事件
    func contentViewTapped(sender: UIGestureRecognizer){
        let index: NSInteger = (sender.view?.tag)! - 200;
        if self.tapActionBlock != nil {
            self.tapActionBlock!(index)
        }
        
    }
    
    @objc fileprivate func animationTimerDidFire(timer:Timer){
        self.currentPageIndex = self.currentPageIndex + 1;
        self.scrollView?.setContentOffset(CGPoint(x: ScreenWidth * CGFloat(currentPageIndex ), y: 0),animated: true)
        self.countDown(timer: timer)
        
    }
    
    public required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}


extension BannerView: UIScrollViewDelegate {
    
    public func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        self.animationTimer?.pause()
    }
    public func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        self.animationTimer?.resumeAfter(interval: self.animationInterval!)
    }
    public func scrollViewDidScroll(_ scrollView: UIScrollView) {
        self.pageControl?.currentPage = self.currentPageIndex
    }
    public func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        scrollView.setContentOffset(CGPoint(x: ScreenWidth, y: 0), animated: true)
    }
    
    
}

contrller的代码

    let urls: [URL] = (imageUrls?.map { (string) -> URL in
            return URL(string: string)!;
            })!;
        let imageView = UIImageView();
        imageView.image = UIImage(named: "Image_ad_log");
        //imageView.frame = CGRect(x: 0, y: ScreenHeight * 0.7, width: ScreenWidth, height: ScreenHeight * 0.3);
        
        self.bannerScroller = BannerView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: ScreenHeight - 170), 1,iamgeUrls: urls)
        self.view.addSubview(bannerScroller!);
        
        self.view.addSubview(imageView);
        imageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.view)
            make.width.equalTo(200)
            make.height.equalTo(170)
            make.centerY.equalTo(ScreenHeight - 85)
        }
        
        guard urls.count > 0 else {
            let appdelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate;
            appdelegate.custmozizeRootViewContrller()
            return
        }
        
        //点击回调响应
        bannerScroller?.tapActionBlock = {(index) in
            print("点击==\(index)")
        }
        
        bannerScroller?.endActionBlock =  { () in
            let appdelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate;
            appdelegate.custmozizeRootViewContrller()
        }

最主要的代码都在了,意会吧

相关文章

  • 启动页广告设置

    策略 这么晚了,还是直接上代码吧 contrller的代码 最主要的代码都在了,意会吧

  • 收集一些好用的第三方库

    1.iOS设置启动页后的广告页: https://github.com/CoderZhuXH/XHLaunchAd...

  • App启动页优化

    功能:启动页展示logo和服务器配置的广告图 app启动方式 冷启动问题 给启动页设置布局,logo闪现问题

  • 启动launchOptions

    给启动页加上一页广告页,在收到通知跳应用是,不走启动页的rootvc设置方法。 做的判断是if(launchOpt...

  • iOS逆向-网易云音乐篇

    照旧去点广告 效果图在下面 1.启动页广告启动页广告 2.歌曲播放界面导航栏右边直播弹框(直接在设置界面有开关)直...

  • 关于启动页时间问题

    需求:启动页显示后显示请求的广告页,显示3.2.1数字倒数之后,跳转到首页。 问题1:直接设置广告页3秒后隐藏,造...

  • iOS设置启动页后的广告页

    很多app(如淘宝、美团等)在启动图加载完毕后,还会显示几秒的广告,一般都有个跳过按钮可以跳过这个广告,有的app...

  • Xcode11设置广告页和launchImage设置启动页

    一、广告页设置 1.SceneDelegate设置window 2.开发一个广告页 3.在SceneDelegat...

  • 启动页设置

    一、添加启动图片 点击Assets.xcassets进入图片管理,右击,弹出"New Launch Image"或...

  • 启动页 设置

    iphone landscape 代表 横屏启动iphone portrait 代表竖屏启动

网友评论

      本文标题:启动页广告设置

      本文链接:https://www.haomeiwen.com/subject/ickahxtx.html