我应该如何使用 UIScrollView
中的 UIpageControl
?我被卡住了,帮帮我。
这是我的代码,请帮忙;
class home: UIViewController, UIScrollViewDelegate {
@IBOutlet weak var mainScrollView: UIScrollView!
@IBOutlet weak var pagecontrol: UIPageControl!
var imageArray = [UIImage]()
var pageControl : UIPageControl!
override func viewDidLoad() {
super.viewDidLoad()
mainScrollView.delegate = self
imageArray = [#imageLiteral(resourceName: "sliderImage"), #imageLiteral(resourceName: "clothing"), #imageLiteral(resourceName: "sports")]
for i in 0..<imageArray.count{
let imageView = UIImageView()
imageView.image = imageArray[i]
imageView .contentMode = .scaleToFill
let xPosition = self.view.frame.width * CGFloat(i)
imageView.frame = CGRect(x: xPosition, y: 0, width: self.mainScrollView.frame.width, height: self.mainScrollView.frame.height)
mainScrollView.contentSize.width = mainScrollView.frame.width * CGFloat(i + 1)
mainScrollView.addSubview(imageView)
}
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
最佳答案
看看这个 answer
这些年来情况几乎没有变化:
- 将您的
UIScrollView
委托(delegate)设置为您的 View Controller (您所做的); - 为
scrollViewDidScroll
委托(delegate)方法添加代码:
func scrollViewDidScroll(_ scrollView: UIScrollView)
{
self.pageControl.currentPage = Int(floorf(Float(scrollView.contentOffset.x / scrollView.width)))
}
- 在
scrollViewDidEndDecelerating
方法中添加相同的内容:
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView)
{
self.pageControl.currentPage = Int(floorf(Float(scrollView.contentOffset.x / scrollView.width)))
}
不过,这取决于您的需求 - 尝试使用和不使用它们中的每一个如何工作。
关于ios - 我应该如何使用 UIScrollView 中的 UIpageControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43675408/