I have a viewcontroller (OnboardingMasterViewController.swift) with a UIScrollView, Page Control, and 2 button for SignIn / SignUp and Not Now in the lower part. Here the overview :
I wonder, how to make this 2 button only appear if the user reaches the last page. How do I pass the bool value that will make the buttons appear?
I already try by create a func called lastPageReached() and called it in viewDidLoad(). It can hide the button but cannot be unhide when it reach the last array.
This is the full code i use for my 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
}
}
The simplest way I can think to do this is to have the logic inside your setIndicatorForCurrentPage
method.
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
}
}