简体   繁体   English

如何在Swift中实现UIPageControl

[英]How do I implement UIPageControl in Swift

Ok so I'm struggling here and haven't been able to find a working solution. 好的,所以我在这里挣扎,但却找不到合适的解决方案。 I've been self learning Swift without Objective C experience (I know, I know). 我一直在自学Swift而没有Objective C经验(我知道,我知道)。

In my app, I have my main UIViewController , a subview that is transparent but slides in from the bottom of the screen, and then 4 subviews of the sliding subview that are all working UIScrollViews. 在我的应用程序中,我有我的主要UIViewController ,一个透明的子视图,但是从屏幕底部滑入,然后滑动子视图的4个子视图都在工作UIScrollViews。 I have paging enabled and it works great but I'd like to add a UIPageControl for each of them. 我启用了分页,它运行良好,但我想为每个分区添加一个UIPageControl。 I seriously can't grasp delegates and how to implement the using swift. 我认真无法掌握代表以及如何实现使用swift。 Any help would be much appreciated! 任何帮助将非常感激!

Also, I'm doing this all programmatically, so no IB please. 另外,我是以编程方式进行的,所以请不要IB。 Happy to provide code if it'll help. 很高兴提供代码,如果它会有所帮助。 Thanks 谢谢

I think you and/or anyone else looking for how to do this will find this answer helpful. 我认为你和/或任何其他寻找如何做到这一点的人都会觉得这个答案很有帮助。 The code example enabled me to create a page control indicator on my scrollView, and it was the first time attempting to do this. 代码示例使我能够在我的scrollView上创建一个页面控件指示器,这是第一次尝试这样做。 I found it very clear. 我发现它很清楚。

The lines you probably need to add to your project are: 您可能需要添加到项目中的行是:

1: add UIScrollViewDelegate as a protocol when you first name your view controller class. 1:首次命名视图控制器类时,将UIScrollViewDelegate添加为协议。

2: in the class declaration create a pageControl variable. 2:在类声明中创建一个pageControl变量。 You will need to play with the frame numbers to get it to appear where you want it. 您需要使用帧编号才能使其显示在您想要的位置。 the current numbers made one in the middle of the window for me. 目前的数字让我在窗口中间。 For reference the numbers mean (x position for top left corner of indicator, y coordinate for top left corner, width of page indicator, height of page indicator) 作为参考,数字表示(指示器左上角的x位置,左上角的y坐标,页面指示器的宽度,页面指示器的高度)

var pageControl : UIPageControl = UIPageControl(frame: CGRectMake(50, 300, 200, 20))

  1. in viewDidLoad set the scrollView delegate and call `configurePageControl(): 在viewDidLoad中设置scrollView委托并调用`configurePageControl():

     override func viewDidLoad() { super.viewDidLoad() scrollView.delegate = self configurePageControl() } 
  2. you need to add two methods after viewDidLoad. 你需要在viewDidLoad之后添加两个方法。 one is called in viewDidLoad 一个在viewDidLoad中调用

     func configurePageControl() { self.pageControl.numberOfPages = <some reference to the number of pages> self.pageControl.currentPage = 0 self.pageControl.tintColor = UIColor.redColor() self.pageControl.pageIndicatorTintColor = UIColor.blackColor() self.pageControl.currentPageIndicatorTintColor = UIColor.greenColor() self.view.addSubview(pageControl) } 

    and

      func scrollViewDidEndDecelerating(scrollView: UIScrollView) { let pageNumber = round(scrollView.contentOffset.x / scrollView.frame.size.width) pageControl.currentPage = Int(pageNumber) } 

The scrollView delegate is actually very simple to set up. scrollView委托实际上很容易设置。 Add UIScollViewDelegate as a protocol that your ViewController class will implement by adding it after the class declaration: class YourClassName: UIScrollViewDelegate . 添加UIScollViewDelegate作为ViewController类将通过在类声明之后添加它来实现的协议: class YourClassName: UIScrollViewDelegate And then in viewDidLoad(), you complete the delegate setup by assigning the scroll view's delegate property to your class with the line scrollView.delegate = self . 然后在viewDidLoad()中,通过使用scrollView.delegate = self滚动视图的委托属性分配给您的类来完成委托设置。 (again see the example I linked for if you need further clarification of where these commands go) (如果您需要进一步说明这些命令的去向,请再次查看我链接的示例)

Just setup it in code like this: 只需在代码中设置它:

private var pageControl = UIPageControl(frame: .zero)

private func setupPageControl() {

    pageControl.numberOfPages = controllers.count
    pageControl.translatesAutoresizingMaskIntoConstraints = false
    pageControl.currentPageIndicatorTintColor = UIColor.orange
    pageControl.pageIndicatorTintColor = UIColor.lightGray.withAlphaComponent(0.8)

    let leading = NSLayoutConstraint(item: pageControl, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1, constant: 0)
    let trailing = NSLayoutConstraint(item: pageControl, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1, constant: 0)
    let bottom = NSLayoutConstraint(item: pageControl, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: 0)

    view.insertSubview(pageControl, at: 0)
    view.bringSubview(toFront: pageControl)
    view.addConstraints([leading, trailing, bottom])
}

Delegates are just methods of a class, that can be palmed off to another class. 代表只是一个类的方法,可以归结为另一个类。 These methods are usually callbacks. 这些方法通常都是回调。

eg A callback for a TextField, when the user hits return, can be implemented in a Delegate. 例如,当用户点击返回时,TextField的回调可以在Delegate中实现。 The delegate can be implemented in the ViewController class. 委托可以在ViewController类中实现。

Now when the user hits return the TextField Object will call the delegate method in the ViewController object. 现在当用户点击返回时,TextField对象将调用ViewController对象中的委托方法。 The great thing about this is, you can access all the variables and methods of the ViewController object from the delegated method. 关于这一点的好处是,您可以从委派方法访问ViewController对象的所有变量和方法。 Otherwise you would need a handle to the ViewController object within the TextField object itself. 否则,您需要TextField对象本身内的ViewController对象的句柄。

Delegates are implemented as protocols, which are just interfaces. 代表实现为协议,它们只是接口。 So if the ViewController implements the TextFieldDelegate protocol, all your textfield callbacks can be called from within the ViewController object. 因此,如果ViewController实现TextFieldDelegate协议,则可以从ViewController对象中调用所有文本字段回调。

I hope this helps. 我希望这有帮助。

UIPageControl Integration using Swift 4 使用Swift 4进行UIPageControl集成

func configurePageControl() {
    self.pageview.numberOfPages = items.count
    self.pageview.currentPage = 0
    self.pageview.tintColor = UIColor.red
    self.pageview.pageIndicatorTintColor = UIColor.black
    self.pageview.currentPageIndicatorTintColor = UIColor.green
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM