swift--加载动态广告页+倒计时按钮

先上个效果图吧

点击右上角可以直接跳转,大体思路就是创建一个控制器,设置为windown的跟视图,然后在这个空气器里面动态加载一张图片,绑定一个倒计时按钮,可以点击直接跳转,等倒计时为0时自动跳转到首页!

具体代码如下:

//
//  AdvertisingViewController.swift
//  启动页Test
//
//  Created by 伯驹网络 on 2017/10/21.
//  Copyright © 2017年 Nicholas. All rights reserved.
//

import UIKit

class AdvertisingViewController: UIViewController {
    @IBOutlet weak var bigImg: UIImageView!
    @IBOutlet weak var timeButton: UIButton!
    //延迟15s
    private var time:TimeInterval = 15.0
    
    private var cuntdownTimer:Timer?
    
    override func viewWillAppear(_ animated: Bool) {
        //异步加载图片
        let url = URL(string:"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1508567340719&di=b280ea59ea0257d1b0f32ef50595486b&imgtype=0&src=http%3A%2F%2Fimg.tupianzj.com%2Fuploads%2Fallimg%2F160124%2F9-160124110337.jpg")
        let request = URLRequest(url:url!)
        let session = URLSession.shared
        let dataTask = session.dataTask(with: request,completionHandler:{
            (data,response,error) -> Void in
            let img = UIImage(data:data!)
            self.bigImg.image = img
        }) as URLSessionTask
        
        //启动任务
        dataTask.resume()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.globalRedColor()
        // Do any additional setup after loading the view.
        
//        timeButton.setTitle("5s后跳过", for: .normal)
        timeButton.backgroundColor = UIColor.lightGray
        timeButton.setTitleColor(UIColor.white, for: .normal)
//        timeButton.clipsToBounds = true
//        timeButton.layer.cornerRadius = 20
    
        //这里加个延迟参数
        DispatchQueue.main.asyncAfter(deadline:DispatchTime.now() + time){
            let sb = UIStoryboard(name:"Main",bundle:nil)
            let rootVC = sb.instantiateInitialViewController()
            UIApplication.shared.keyWindow?.rootViewController = rootVC
        }

        //倒计时
        cuntdownTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTime), userInfo: nil, repeats: true)
        
    }

    @objc func updateTime(){
        time -= 1
        timeButton.setTitle(String(format:"%.fs 后跳过",time), for: .normal)
    }
    
    //点击直接跳转
    @IBAction func pushAction(_ sender: Any) {
        let sb = UIStoryboard(name:"Main",bundle:nil)
        let rootVC = sb.instantiateInitialViewController()
        UIApplication.shared.keyWindow?.rootViewController = rootVC
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

AppDelegate里面的代码:

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        
        //改为从storyboard启动
        //创建窗口
        window = UIWindow(frame:UIScreen.main.bounds)
        let advertiseVC = AdvertisingViewController()
        window?.rootViewController = advertiseVC
        window?.makeKeyAndVisible()
        
        return true
    }

主页面里面的代码:

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.view.backgroundColor = UIColor.orange
        let label = UILabel(frame:CGRect(x:10,y:70,width:self.view.frame.size.width-20,height:45))
        label.text = "欢迎来到首页"
        label.textColor = UIColor.white
        label.backgroundColor = UIColor.blue
        self.view.addSubview(label)
    }
}

广告页里面的内容是拉了一个xib文件,上面的是完整的代码,可以直接复制粘贴使用!

 

 

加载

posted @ 2017-10-21 14:19  稻草人11223  阅读(1383)  评论(0编辑  收藏  举报
返回顶部