ios - 我应该如何使用 UIScrollView 中的 UIpageControl

标签 ios iphone swift uiscrollview uipagecontrol

我应该如何使用 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

这些年来情况几乎没有变化:

  1. 将您的 UIScrollView 委托(delegate)设置为您的 View Controller (您所做的);
  2. scrollViewDidScroll 委托(delegate)方法添加代码:
func scrollViewDidScroll(_ scrollView: UIScrollView) 
{
    self.pageControl.currentPage = Int(floorf(Float(scrollView.contentOffset.x / scrollView.width)))
}  
  1. 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/

相关文章:

ios - 使用两个 TableView 时发生竞争条件并且应用程序崩溃

ios - iOS在后台使用多个对象执行任务

ios - 如何解析一个有登录名和密码的网页才能进入查看用户的个人页面? (iPhone)

iphone - 设置 NSInteger 属性时接收器类型无效 'NSInteger'

iphone - IOS 5 和 RemoteIO 的 CABasicAnimation 滞后问题

ios - SwipeCellKit 不滑动

ios - UIButton 中的图像颜色被掩盖为背景颜色

android - 使用 Google Analytics 进行原生移动和网站分析

iphone - xcode iphone 项目和 ipad 项目到通用应用程序

swift - @objc'只能应用于类的扩展,从swift调用扩展方法到objective c