ios - uipagecontrol 当前不动

标签 ios swift

这是我第一次使用 uipagecontrol,我在使用它时遇到了问题。问题是即使我在一开始就将它从 0 更改为 1,当前点也没有任何作用,该点仍停留在最左边的点上。为什么会这样?

这是我的代码,collectionview 和 View Controller 在 2 个单独的类上,这是 View Controller 代码

class introViewController: UIViewController {

    let skipBtn:UIButton = {
       let content = UIButton()
        content.titleLabel?.textAlignment = .right
        content.setTitle("Skip", for: .normal)
        content.setTitleColor(.black, for: .normal)
        content.titleLabel?.font = UIFont(name: "copperplate", size: 16)
        content.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
        content.addTarget(self, action: #selector(mainController), for: .touchUpInside)
       return content
    }()

    let contentSlider:introCollectionView = {
       let content = introCollectionView()
       return content
    }()

    let pageLbl:UILabel = {
       let content = UILabel()
       content.font = UIFont(name: "copperplate", size: 16)
       content.font = UIFont.boldSystemFont(ofSize: 16)
       return content
    }()

    lazy var pageView:UIPageControl = {
       let content = UIPageControl()
        content.size(forNumberOfPages: 19)
        content.currentPage = 0
        content.numberOfPages = 3
        content.currentPageIndicatorTintColor = .red
        content.pageIndicatorTintColor = .darkGray
       return content
    }()
    let nextBtn:UIButton = {
       let content = UIButton()
        content.titleLabel?.textAlignment = .right
       content.setTitle("Next", for: .normal)
       content.setTitleColor(.black, for: .normal)
       content.titleLabel?.font = UIFont(name: "copperplate", size: 16)
       content.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
       return content
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        var currentPage = "Page 1"
        var currentTitle = "easy way to organize your life"
        var currentSubtitle = "all you have to do is say the word the app understand your command and it will save and organize the data for you"
        var currentImage = "introNote"

        pageLbl.text = "\(currentPage)"

        skipBtn.translatesAutoresizingMaskIntoConstraints = false
        contentSlider.translatesAutoresizingMaskIntoConstraints = false
        pageLbl.translatesAutoresizingMaskIntoConstraints = false
        pageView.translatesAutoresizingMaskIntoConstraints = false
        nextBtn.translatesAutoresizingMaskIntoConstraints = false

        view.addSubview(skipBtn)
        view.addSubview(contentSlider)
        view.addSubview(pageLbl)
        view.addSubview(pageView)
        view.addSubview(nextBtn)

        skipBtn.topAnchor.constraint(equalTo: view.topAnchor, constant: 20).isActive = true
        skipBtn.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -10).isActive = true
        skipBtn.widthAnchor.constraint(equalToConstant: 50).isActive = true
        skipBtn.heightAnchor.constraint(equalToConstant: 50).isActive = true

        pageLbl.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -10).isActive = true
        pageLbl.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 10).isActive = true
        pageLbl.widthAnchor.constraint(equalToConstant: 100).isActive = true
        pageLbl.heightAnchor.constraint(equalToConstant: 50).isActive = true

        pageView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -18).isActive = true
        pageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        pageView.widthAnchor.constraint(equalToConstant: 100).isActive = true
        pageView.heightAnchor.constraint(equalToConstant: 30).isActive = true

        nextBtn.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -10).isActive = true
        nextBtn.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -10).isActive = true
        nextBtn.widthAnchor.constraint(equalToConstant: 50).isActive = true
        nextBtn.heightAnchor.constraint(equalToConstant: 50).isActive = true

        contentSlider.topAnchor.constraint(equalTo: skipBtn.bottomAnchor, constant: 0).isActive = true
        contentSlider.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        contentSlider.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
        contentSlider.bottomAnchor.constraint(equalTo: pageLbl.topAnchor, constant: 0).isActive = true
    }

    @objc func mainController(){
        let next = self.storyboard?.instantiateViewController(withIdentifier: "mainViewController") as! mainViewController
        self.present(next, animated: true, completion: nil)
    }
    @objc func changeDot(current:Int){
        print("the current is \(current)")

    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

这是collectionview的代码

class introCollectionView: UIView,UICollectionViewDelegateFlowLayout,UICollectionViewDataSource {
    let arrayImg = [
        "introhelo",
        "introhelo",
        "introhelo"]

    let arrayTitle = [
        "hello",
        "hello",
        "hello"]

    let arraySubTitle = [
        "hello",
        "hello",
        "hello"
    ]
    var currentIndex = 0
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return arrayTitle.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! introCollectionViewCell
        cell.contentImg.image = UIImage(named: "\(arrayImg[indexPath.row])")
        cell.contentTitleLbl.text = "\(arrayTitle[indexPath.row])"
        cell.contentSubTitleLbl.text = "\(arraySubTitle[indexPath.row])"
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: self.frame.width, height: self.frame.height)
    }
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        if let indexPath = collectionViews.indexPathsForVisibleItems.first {
            let intro = introViewController()
            intro.pageView.currentPage = indexPath.row + 1
            print(indexPath.row)
        }
    }
//    func scrollViewDidScroll(_ scrollView: UIScrollView) {
//        currentIndex = Int((collectionViews.contentOffset.x ?? 0.0) / (collectionViews.frame.size.width ?? 0.0))
//        let intro = introViewController()
//        intro.pageView.currentPage = currentIndex
//        print(currentIndex)
//    }

    lazy var collectionViews: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
        cv.isPagingEnabled = true
        cv.backgroundColor = UIColor.clear
        cv.dataSource = self
        cv.delegate = self
        return cv
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
    }

    func setupViews(){
        collectionViews.register(introCollectionViewCell.self, forCellWithReuseIdentifier: "cell")
        collectionViews.translatesAutoresizingMaskIntoConstraints = false
        backgroundColor = UIColor.clear
        addSubview(collectionViews)
        collectionViews.topAnchor.constraint(equalTo: topAnchor, constant: 0).isActive = true
        collectionViews.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 0).isActive = true
        collectionViews.centerYAnchor.constraint(equalTo: centerYAnchor, constant: 0).isActive = true
        collectionViews.centerXAnchor.constraint(equalTo: centerXAnchor, constant: 0).isActive = true
        collectionViews.widthAnchor.constraint(equalTo: widthAnchor, constant: 0).isActive = true
    }

    @objc func scrolling(){

        //collectionViews.scrollToItem(at: 1, at: .centeredHorizontally, animated: true)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

最佳答案

实际上问题是分配给pageView.currentPage与错误的对象。您正在创建 introViewController 的新对象分配 pageView.currentPage这与当前呈现的对象不同,因此您不会在呈现的 introViewController 中看到任何变化。 .

要解决此问题,您可以采用委托(delegate)或闭包模式来更新页码 introCollectionViewintroViewController .

这是一个解决方案,在 introCollectionView 中声明一个闭包如下所示,

class introCollectionView: UIView,UICollectionViewDelegateFlowLayout,UICollectionViewDataSource {

  var onPageChange: ((Int) ->Void)?

  ....

  func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    if let indexPath = collectionViews.indexPathsForVisibleItems.first {
        self.onPageChange?(indexPath.row + 1)
    }
  }
}

然后在introViewControllerviewDidLoad , 你可以注册一个闭包来更新 pageView的当前页面如下,

override func viewDidLoad() {
    super.viewDidLoad()

    self.contentSlider.onPageChange = {[weak self] pageNumber in
       self?.pageView.currentPage = pageNumber
    }

   ....
}

题外话:你可能来自不同的背景,但在 Swift , 类(或任何类型)名称应以大写字母开头。

关于ios - uipagecontrol 当前不动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51461420/

相关文章:

arrays - 将 var 存储在数组中

ios - 在 iOS 8 共享扩展和主应用程序之间共享数据

ios - 我在 xcworkspace 中收到大量关于丢失文件的警告

ios - 检测更改所选注释或取消选择的点击

ios - alamofire,下载多张图片

ios - 如何在 Xcode 6.1 中获取 iPod Touch 模拟器

iphone - 如何解析数组?

ios - 无法使用 Xcode 9.4、Swift 4.1 在 KASlideShow pod 中加载图像

ios - 最小化 iOS 应用程序大小

ios - 在 UITableView 中显示 2D 数组