iOS-swift-引导页制作

添加视图

这里写图片描述

编写 GuideViewController

//
//  GuideViewController.swift
//  H56580E2E
//
//  引导页
//

import UIKit
import Spring

class GuideViewController: UIViewController {

    @IBOutlet weak var pageControl: UIPageControl!
    //@IBOutlet weak var startButton: UIButton!
    var startButton = SpringButton() //动画Button
    fileprivate var scrollView: UIScrollView!

    fileprivate let numOfPages = 3
    //屏幕的高
    let Screen_height = UIScreen.main.bounds.size.height
    let Screen_width = UIScreen.main.bounds.size.width

    override func viewDidLoad() {
        super.viewDidLoad()

        let frame = self.view.bounds

        scrollView = UIScrollView(frame: frame)
        scrollView.isPagingEnabled = true
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.showsVerticalScrollIndicator = false
        scrollView.scrollsToTop = false
        scrollView.bounces = false
        scrollView.contentOffset = CGPoint.zero
        // 将 scrollView 的 contentSize 设为屏幕宽度的3倍(根据实际情况改变)
        scrollView.contentSize = CGSize(width: frame.size.width * CGFloat(numOfPages), height: frame.size.height)

        scrollView.delegate = self

        startButton.frame = CGRect(x: Screen_width / 2 - 70, y: Screen_height - 180, width: 150, height: 40)
        //添加引导页的图片
        for index  in 0..<numOfPages {
            let imageView = UIImageView(image: UIImage(named: "yindao\(index + 1)"))
            imageView.frame = CGRect(x: frame.size.width * CGFloat(index), y: 0, width: frame.size.width, height: frame.size.height)
            scrollView.addSubview(imageView)
        }
        //scrollView.addSubview(startButton)
        self.view.addSubview(startButton)
        self.view.insertSubview(scrollView, at: 0)

        startButton.backgroundColor = UIColor.yellow
        startButton.setTitleColor(UIColor.black, for: UIControlState())
        startButton.setTitle("立即体验", for: UIControlState())
        // 给开始按钮设置圆角
        startButton.layer.cornerRadius = 15.0
        // 隐藏开始按钮
        startButton.alpha = 0.0
    }

    // 隐藏状态栏
    override var prefersStatusBarHidden : Bool {
        return true
    }
    //立即体验点击(跳转界面)
   @objc func firstClick(_ sender: UIButton){
    //name的参数值为要跳转的.storyboard 文件的文件名
    let destinationStoryboard = UIStoryboard(name:"Main",bundle:nil)
    //withIdentifier 的参数值为 .storyboard文件中初始化视图所对应的 Controller 的 StoryboardID
    //创建 TableViewController 对象
    let destinationViewController = destinationStoryboard.instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
    self.present(destinationViewController, animated: true, completion: nil)
    }
}

// MARK: - UIScrollViewDelegate
extension GuideViewController: UIScrollViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let offset = scrollView.contentOffset
        // 随着滑动改变pageControl的状态
        pageControl.currentPage = Int(offset.x / view.bounds.width)

        // 因为currentPage是从0开始,所以numOfPages减1
        if pageControl.currentPage == numOfPages - 1 {
            //显示按钮
            self.startButton.alpha = 1.0
            self.startButton.animation = "fadeInDown"
            self.startButton.curve = "easeInOut"
            self.startButton.duration = 1.0
            self.startButton.damping = 0.7
            self.startButton.velocity = 0.7
            self.startButton.force = 0.3
            self.startButton.animate() // 开始动画
            //添加点击事件
            //传递触摸对象(即点击的按钮),需要在定义action参数时,方法名称后面带上冒号
            self.startButton.addTarget(self, action: #selector(firstClick(_:)), for: .touchUpInside)
        } else {
            //隐藏按钮
            self.startButton.alpha = 0.0
        }
    }

}

猜你喜欢

转载自blog.csdn.net/wa172126691/article/details/80341227