我有一个Viewcontroller(OnboardingMasterViewController.swift),其下部带有UIScrollView,Page Control和2个用于SignIn / SignUp和Not Now的按钮。这里是概述:
我想知道,如何仅当用户到达最后一页时才显示此2按钮。如何传递布尔值使按钮出现?
我已经尝试创建一个名为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,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)!),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
}
}