IOS 使用UIScrollview实现引导页(swift)

前面博客有写到android使用ViewPage创建引导页的最简单方法,这次要写一个UIScrollview创建引导页的最简单方法。话不多说进客有正题。

一、创建GuideController.swift

创建GuideController.swift当做引导页的载体。

二、初始化UIScrollview

 

//页面数量
    let numOfPages = 3
let frame = self.view.bounds
        //scrollView的初始化
        let scroll = UIScrollView()
        scroll.frame = self.view.bounds
       
        //为了能让内容横向滚动,设置横向内容宽度为3个页面的宽度总和
        scroll.contentSize = CGSize(width:frame.size.width * CGFloat(numOfPages),
                                        height:frame.size.height)
        
        
        scroll.isPagingEnabled = true
        scroll.showsHorizontalScrollIndicator = false
        scroll.showsVerticalScrollIndicator = false

三、加载引导页的图片以及最后一页的进入按钮

 for i in 0..<numOfPages{
            let img1 = UIImageView(image: UIImage(named:"image\(i+1)"))
            img1.frame = CGRect(x:frame.size.width*CGFloat(i), y:CGFloat(0),
                                width:frame.size.width, height:frame.size.height)
            scroll.addSubview(img1)
        }
        
        let button = UIButton()
        button.frame = CGRect(x:frame.size.width*2.6, y:frame.size.height*0.9,
                              width:frame.size.width/2, height:frame.size.height/8)
        button.setImage(UIImage(named:"next"), for: .normal)
         scroll.addSubview(button)
        button.addTarget(self, action:#selector(tapped), for:.touchUpInside)
       
        self.view.addSubview(scroll)

四、设置按钮的点击事件,进入首页

 @objc func tapped(){
        let sb = UIStoryboard(name: "Main", bundle:nil)
        let vc = sb.instantiateViewController(withIdentifier: "VC") as! ViewController
        self.present(vc, animated: true, completion: nil)
    }

五、在AppDelegate.swift判断第一次启动

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        //用于判断是否是第一次启动应用
        if (!(UserDefaults.standard.bool(forKey: "first"))) {
            UserDefaults.standard.set(true, forKey:"first")
            let guideViewController = GuideController()
            self.window!.rootViewController=guideViewController;
        }
        return true
    }

好了,到此结束,最后贴出GuideController.swift完整代码:

//
//  GuideController.swift
//  SamrtToilet
//
//  Created by 唐 on 2018/3/6.
//  Copyright © 2018年 唐. All rights reserved.
//

import UIKit

class GuideController: UIViewController{
   
   
    
    //页面数量
    let numOfPages = 3
   
    override func viewDidLoad() {
        super.viewDidLoad()
        let frame = self.view.bounds
        //scrollView的初始化
        let scroll = UIScrollView()
        scroll.frame = self.view.bounds
       
        //为了能让内容横向滚动,设置横向内容宽度为3个页面的宽度总和
        scroll.contentSize = CGSize(width:frame.size.width * CGFloat(numOfPages),
                                        height:frame.size.height)
        
        
        scroll.isPagingEnabled = true
        scroll.showsHorizontalScrollIndicator = false
        scroll.showsVerticalScrollIndicator = false
       
        
        
         for i in 0..<numOfPages{
            let img1 = UIImageView(image: UIImage(named:"image\(i+1)"))
            img1.frame = CGRect(x:frame.size.width*CGFloat(i), y:CGFloat(0),
                                width:frame.size.width, height:frame.size.height)
            scroll.addSubview(img1)
        }
        
        let button = UIButton()
        button.frame = CGRect(x:frame.size.width*2.6, y:frame.size.height*0.9,
                              width:frame.size.width/2, height:frame.size.height/8)
        button.setImage(UIImage(named:"next"), for: .normal)
         scroll.addSubview(button)
        button.addTarget(self, action:#selector(tapped), for:.touchUpInside)
       
        self.view.addSubview(scroll)
        
        
        
    }
    
    @objc func tapped(){
        let sb = UIStoryboard(name: "Main", bundle:nil)
        let vc = sb.instantiateViewController(withIdentifier: "VC") as! ViewController
        self.present(vc, animated: true, completion: nil)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值