Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 当用户处于UIPageControl的最后一页时,设置登录/注册按钮的可见性_Arrays_Swift_Button_Show Hide_Uipagecontrol - Fatal编程技术网

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

Arrays 当用户处于UIPageControl的最后一页时,设置登录/注册按钮的可见性,arrays,swift,button,show-hide,uipagecontrol,Arrays,Swift,Button,Show Hide,Uipagecontrol,我有一个viewcontroller(OnboardingMasterViewController.swift),带有UIScrollView、页面控件和2个用于登录/注册的按钮,现在不在下部。以下是概述: 我想知道,如何让这个2按钮只在用户到达最后一页时出现。如何传递使按钮显示的布尔值 我已经尝试创建一个名为lastpagereach()的func,并在viewDidLoad()中调用它。它可以隐藏按钮,但在到达最后一个数组时无法取消隐藏 这是我用于OnboardingMasterViewC

我有一个viewcontroller(OnboardingMasterViewController.swift),带有UIScrollView、页面控件和2个用于登录/注册的按钮,现在不在下部。以下是概述:

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

我已经尝试创建一个名为lastpagereach()的func,并在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
    }
}
//
类OnboardingMasterViewController:UIViewController、UIScrollViewDelegate{
@iBCROLLVIEW:UIScrollView!
@IBVAR弱页面控制:UIPageControl!
@IBOUTLE弱var btnSignUp:UIButton!
@IBOutlet弱var BTNOWN:UIButton!
var scrollWidth:CGFloat!=0.0
变量scrollHeight:CGFloat!=0.0
//幻灯片数据
var titles=[“欢迎来到Sistakedi!”、“易于输入”、“易于访问”、“易于看到趋势”]
var descs=[“此应用程序将帮助您记录和管理您的健康状况。”,“Sistakedi有一种简单的方法来记录您的血压、血糖和胆固醇。”,“不仅易于记录,您还可以在我们的日志中轻松找到您的健康数据。”,“您还可以通过查看图表中的健康记录了解您的健康状况。”]
变量imgs=[“简介1”、“简介2”、“简介3”、“简介4”]
//获取scrollview的动态宽度和高度并保存它
重写func viewdilayoutsubviews(){
scrollWidth=scrollView.frame.size.width
scrollHeight=scrollView.frame.size.height
}
重写func viewDidLoad(){
super.viewDidLoad()
self.view.layoutifneed()
//调用viewDidLayoutSubviews()并获取scrollview的动态宽度和高度
self.scrollView.delegate=self
scrollView.isPaginEnabled=true
scrollView.showsHorizontalScrollIndicator=false
scrollView.showsVerticalScrollIndicator=false
//单击幻灯片并添加它们
var frame=CGRect(x:0,y:0,宽度:0,高度:0)

对于0..中的索引,我认为最简单的方法是在
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
    }
}

谢谢@Screerspeak。我已经尝试了你的建议。它的工作方式和我希望的一样。我必须先将故事板中的按钮条件更改为隐藏状态,以使其更好地工作,并根据Xcode建议更改一些代码。非常感谢你的帮助和知识。