ios - uipagecontrol 当前不动
问题描述
这是我第一次使用 uipagecontrol,我在使用它时遇到了麻烦。问题是,即使我在一开始将它从 0 更改为 1,当前的点也不会保持在最左边的点上。为什么会这样?
这是我的代码,collectionview 和视图控制器在 2 个单独的类上 这是视图控制器代码
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
。
introCollectionView
要解决此问题,您可以采用委托或闭包模式从in更新页码introViewController
。
这是一种通过在内部声明闭包的解决方案,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)
}
}
}
然后在introViewController
'sviewDidLoad
中,你可以注册一个闭包来更新pageView
's 当前页面,如下所示,
override func viewDidLoad() {
super.viewDidLoad()
self.contentSlider.onPageChange = {[weak self] pageNumber in
self?.pageView.currentPage = pageNumber
}
....
}
题外话:您可能来自不同的背景,但在 中Swift
,类(或任何类型)名称应以大写字母开头。
推荐阅读
- javascript - 从行初始化 - Birt
- mysql - 如何在 MySQL 中结合 Max 和 Count 函数
- java - 以新顺序生成已经存在的列表
- swagger - 端点在招摇中按http方法分组
- python - OpenStack Keystone python 客户端,无法联系“example:5000”处的端点进行发现
- flutter - 图像小部件未在 setState 上重建
- c++ - 打印所有乘以等于输入的对,其中至少一个是主要的
- google-apps-script - 关于车辆服务到期的 Google 表格有条件的电子邮件提醒
- excel - 计算对方的清单
- python - 在 Celery 任务中返回 QuerySet 对象(mongo)