我有一个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,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
}
}
我认为您正在寻找的正是您的代码。您可以使用scrollViewDidScroll
功能查看当前位于哪个页面:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let currentPage = scrollView.contentOffset.x / UIScreen.main.bounds.size.width
pageControl.currentPage = Int(currentPage)
if currentPage == yourLastPage {
btnNotNow.isHidden = false
btnNotNow.isHidden = false
} else {
btnNotNow.isHidden = true
btnSignUp.isHidden = true
}
}
我希望这会有所帮助。如果没有,请更详细地说明您要寻找什么,我在这里为您提供帮助。