前面博客有写到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.
}
}