gpt4 book ai didi

ios - uipagecontrol 当前不动

转载 作者:行者123 更新时间:2023-11-28 07:44:25 25 4
gpt4 key购买 nike

这是我第一次使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com