当用户位于 UIPageControl 的最后一页时设置登录/注册按钮的可见性

Set Visibility of SignIn / SignUp button when User is in the last page of UIPageControl

我有一个 viewcontroller (OnboardingMasterViewController.swift),带有 UIScrollView、页面控件和 2 个用于 SignIn/SignUp 和 Not Now 的按钮,位于下部。这里是概述:

Onboarding Storyboard

我想知道,如何让这个 2 按钮只在用户到达最后一页时出现。如何传递使按钮出现的 bool 值?

我已经尝试创建一个名为 lastPageReached() 的函数并在 viewDidLoad() 中调用它。它可以隐藏按钮,但当它到达最后一个数组时不能取消隐藏。

这是我用于 OnboardingMasterViewController 的完整代码。

//
class OnboardingMasterViewController: UIViewController, UIScrollViewDelegate {

@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var pageControl: UIPageControl!
@IBOutlet weak var btnSignUp: UIButton!
@IBOutlet weak var btnNotNow: UIButton!

var scrollWidth: CGFloat! = 0.0
var scrollHeight: CGFloat! = 0.0

// data for slides
var titles = ["Welcome to Sistakedi!", "Easy to input", "Easy to access", "Easy to see the trend"]
var descs = ["This application will help you to record and manage your health condition.","Sistakedi have a simple way to record your blood pressure, blood glucose and cholesterol.","Not just easy to record, you can easilly find your health data in our journal.", "And you can also understand your health condition by see your health history in graphic."]
var imgs = ["intro1","intro2","intro3","intro4"]

// get dynamic width and height of scrollview and save it
override func viewDidLayoutSubviews() {
    scrollWidth = scrollView.frame.size.width
    scrollHeight = scrollView.frame.size.height
}

override func viewDidLoad() {
    super.viewDidLoad()
    self.view.layoutIfNeeded()
    //to call viewDidLayoutSubviews() and get dynamic width and height of scrollview

    self.scrollView.delegate = self
    scrollView.isPagingEnabled = true
    scrollView.showsHorizontalScrollIndicator = false
    scrollView.showsVerticalScrollIndicator = false

    //crete the slides and add them
    var frame = CGRect(x: 0, y: 0, width: 0, height: 0)

    for index in 0..<titles.count {
        frame.origin.x = scrollWidth * CGFloat(index)
        frame.size = CGSize(width: scrollWidth, height: scrollHeight)

        let slide = UIView(frame: frame)

        //subviews
        let imageView = UIImageView.init(image: UIImage.init(named: imgs[index]))
        imageView.frame = CGRect(x:0,y:0,width:300,height:300)
        imageView.contentMode = .scaleAspectFit
        imageView.center = CGPoint(x:scrollWidth/2,y: scrollHeight/2 - 50)

        let txt1 = UILabel.init(frame: CGRect(x:32,y:imageView.frame.maxY+10,width:scrollWidth-64,height:80))
        txt1.textAlignment = .center
        txt1.font = UIFont.boldSystemFont(ofSize: 20.0)
        txt1.text = titles[index]

        let txt2 = UILabel.init(frame: CGRect(x:32,y:txt1.frame.maxY+10,width:scrollWidth-64,height:80))
        txt2.textAlignment = .center
        txt2.numberOfLines = 4
        txt2.font = UIFont.systemFont(ofSize: 18.0)
        txt2.text = descs[index]

        slide.addSubview(imageView)
        slide.addSubview(txt1)
        slide.addSubview(txt2)
        scrollView.addSubview(slide)

        lastPageReached()

    }

    scrollView.contentSize = CGSize(width: scrollWidth * CGFloat(titles.count), height: scrollHeight)

    //disable vertical scroll/bounce
    self.scrollView.contentSize.height = 1.0

    //initial state
    pageControl.numberOfPages = titles.count
    pageControl.currentPage = 0

}


//indicator
@IBAction func pageChanged(_ sender: Any) {
    scrollView!.scrollRectToVisible(CGRect(x: scrollWidth * CGFloat ((pageControl?.currentPage)!), y: 0, width: scrollWidth, height: scrollHeight), animated: true)
}

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    setIndiactorForCurrentPage()
}

func setIndiactorForCurrentPage()  {
    let page = (scrollView?.contentOffset.x)!/scrollWidth
    pageControl?.currentPage = Int(page)
}

func lastPageReached() {
    if titles.count == 3 {
        btnNotNow.isHidden = false
        btnNotNow.isHidden = false
    } else {
        btnNotNow.isHidden = true
        btnSignUp.isHidden = true
    }
}

我能想到的最简单的方法是在您的 setIndicatorForCurrentPage 方法中包含逻辑。

func setIndiactorForCurrentPage()  {
    let page = Int((scrollView?.contentOffset.x)!/scrollWidth)
    pageControl?.currentPage = page
    if page == titles.count - 1 {
        //This is the last page
        btnSignUp.isHidden = false
        btnNotNow.isHidden = false
    } else {
        //If you want to hide the button again in case the user goes backward through your pages
        btnSignUp.isHidden = true
        btnNotNow.isHidden = true
    }
}