ios 我如何在 Swift 中实现 UIPageControl
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24795722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I implement UIPageControl in Swift
提问by Ross Isdaner
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).
好的,所以我在这里苦苦挣扎,一直无法找到可行的解决方案。我一直在没有 Objective C 经验的情况下自学 Swift(我知道,我知道)。
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. I have paging enabled and it works great but I'd like to add a UIPageControl for each of them. I seriously can't grasp delegates and how to implement the using swift. Any help would be much appreciated!
在我的应用程序中,我有我的 main UIViewController
,一个透明但从屏幕底部滑入的子视图,然后是滑动子视图的 4 个子视图,它们都在工作 UIScrollViews。我启用了分页,效果很好,但我想为每个页面添加一个 UIPageControl。我真的无法掌握委托以及如何实现使用 swift。任何帮助将非常感激!
Also, I'm doing this all programmatically, so no IB please. Happy to provide code if it'll help. Thanks
另外,我正在以编程方式进行所有操作,因此请不要使用 IB。如果有帮助,很高兴提供代码。谢谢
回答by Natalia
I think you and/or anyone else looking for how to do this will find this answerhelpful. The code example enabled me to create a page control indicator on my scrollView, and it was the first time attempting to do this. I found it very clear.
我认为您和/或其他任何寻找如何做到这一点的人都会发现这个答案很有帮助。该代码示例使我能够在我的 scrollView 上创建页面控件指示器,这是我第一次尝试这样做。我发现它很清楚。
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. 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)
2:在类声明中创建一个pageControl变量。您需要调整帧数才能让它出现在您想要的位置。当前的数字为我在窗口中间做了一个。参考数字平均值(指示器左上角的 x 位置,左上角的 y 坐标,页面指示器的宽度,页面指示器的高度)
var pageControl : UIPageControl = UIPageControl(frame: CGRectMake(50, 300, 200, 20))
var pageControl : UIPageControl = UIPageControl(frame: CGRectMake(50, 300, 200, 20))
in viewDidLoad set the scrollView delegate and call `configurePageControl():
override func viewDidLoad() { super.viewDidLoad() scrollView.delegate = self configurePageControl() }
you need to add two methods after viewDidLoad. one is called in 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) }
在 viewDidLoad 中设置 scrollView 委托并调用 `configurePageControl():
override func viewDidLoad() { super.viewDidLoad() scrollView.delegate = self configurePageControl() }
您需要在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) }
和
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. Add UIScollViewDelegate as a protocol that your ViewController class will implement by adding it after the class declaration: 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
. (again see the example I linked for if you need further clarification of where these commands go)
scrollView 委托实际上很容易设置。添加UIScollViewDelegate作为一个协议,你的ViewController类将由类声明之后加入它实现:class YourClassName: UIScrollViewDelegate
。然后在 viewDidLoad() 中,您通过使用行将滚动视图的委托属性分配给您的类来完成委托设置scrollView.delegate = self
。(如果您需要进一步说明这些命令的去向,请再次参见我链接的示例)
回答by Bart?omiej Semańczyk
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])
}
回答by Arpan Shah
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
}
回答by Santosh
Delegates are just methods of a class, that can be palmed off to another class. These methods are usually callbacks.
委托只是一个类的方法,可以交给另一个类。这些方法通常是回调。
e.g. A callback for a TextField, when the user hits return, can be implemented in a Delegate. The delegate can be implemented in the ViewController class.
例如,当用户点击返回时,TextField 的回调可以在 Delegate 中实现。委托可以在 ViewController 类中实现。
Now when the user hits return the TextField Object will call the delegate method in the ViewController object. The great thing about this is, you can access all the variables and methods of the ViewController object from the delegated method. Otherwise you would need a handle to the ViewController object within the TextField object itself.
现在,当用户点击返回时,TextField 对象将调用 ViewController 对象中的委托方法。这样做的好处是,您可以从委托方法访问 ViewController 对象的所有变量和方法。否则,您将需要 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.
我希望这有帮助。