经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » iOS » 查看文章
3.iOS14下UIPageControl自定义样式
来源:cnblogs  作者:小小个子大个头  时间:2020/12/8 9:03:25  对本文有异议

3.iOS14下UIPageControl自定义样式

1.概览

首先在iOS14中UIPageControl中增加了几个新属性及方法:

  1. /// 表示当前的背景样式,枚举值
  2. open var backgroundStyle: UIPageControl.BackgroundStyle
  3. /// 只读属性,表示当前处于离散互动还是连续互动
  4. open var interactionState: UIPageControl.InteractionState { get }
  5. /// 表示是否开始连续互动,默认true
  6. open var allowsContinuousInteraction: Bool
  7. /// 指示器样式统一调整,默认nil,样式为圆点
  8. open var preferredIndicatorImage: UIImage?
  9. /// 获取page处对应的图片,没有设置则为nil
  10. open func indicatorImage(forPage page: Int) -> UIImage?
  11. /// 设置page处对应的图片
  12. open func setIndicatorImage(_ image: UIImage?, forPage page: Int)

同时废弃了一个方法和属性:

  1. /// 设置该属性后,延缓更新指示器,直到调用updateCurrentPageDisplay为止
  2. open var defersCurrentPageDisplay: Bool
  3. /// 更新指示器
  4. open func updateCurrentPageDisplay()

2.部分新增属性及方法

2.1 preferredIndicatorImage

  1. let control = UIPageControl()
  2. control.numberOfPages = 10
  3. control.frame = CGRect(x: 0, y: 300, width: 350, height: 30)
  4. control.pageIndicatorTintColor = .systemRed
  5. control.currentPageIndicatorTintColor = .systemGreen
  6. if #available(iOS 14.0, *) {
  7. control.preferredIndicatorImage = UIImage(named:"heart")
  8. }
  9. self.view.addSubview(control)

preferredIndicatorImage可以将指示器图片替换成任意我们想要的图片
preferredIndicatorImage-w171

2.2 setIndicatorImage(UIImage?, forPage: Int)

可以设置任意page对应的图片,如果image为nil的话则显示圆点

  1. let indicatorImages = ["summy", "cloudy", "rainy", "thunder"]
  2. if #available(iOS 14.0, *) {
  3. for (idx, imageName) in indicatorImages.enumerated() {
  4. control.setIndicatorImage(UIImage(named:imageName), forPage: idx)
  5. }
  6. }

setIndicatorImage-w238

2.3 自定义样式

通过iOS14中新增的几个属性,只能实现简单的自定义样式,想要实现完全的自定义仍旧需要采用之前的办法,在这之前先看一下UIPageControl在iOS14中层级的变化

[iOS 13]
[iOS 14]

圆点有原先UIView一个视图变成了由_UIPageControlContentView_UIPageControlIndicatorContentView_UIPageIndicatorView组合的视图,由于_UIPageIndicatorView是一个UIImageView,所以我们可以直接使用这个视图来呈现

  1. if #available(iOS 14.0, *) {
  2. guard let dotContentView = findIndicatorContentView() else {
  3. return
  4. }
  5. for (index, view) in dotContentView.subviews.enumerated() {
  6. if view.isKind(of: UIImageView.self) {
  7. self.currentPageIndicatorTintColor = self.currentTintColor
  8. self.pageIndicatorTintColor = self.inactiveTintColor
  9. let indicatorView = view as! UIImageView
  10. indicatorView.image = nil
  11. if index == self.currentPage {
  12. indicatorView.image = currentImage.withRenderingMode(.alwaysTemplate)
  13. } else {
  14. indicatorView.image = inactiveImage.withRenderingMode(.alwaysTemplate)
  15. }
  16. }
  17. }
  18. }
  19. @available(iOS 14.0, *)
  20. func findIndicatorContentView() -> UIView? {
  21. for contentView in self.subviews {
  22. if let contentViewClass = NSClassFromString("_UIPageControlContentView"), contentView.isKind(of: contentViewClass) {
  23. for indicatorContentView in contentView.subviews {
  24. if let indicatorContentViewClass = NSClassFromString("_UIPageControlIndicatorContentView"), indicatorContentView.isKind(of: indicatorContentViewClass) {
  25. return indicatorContentView
  26. }
  27. }
  28. }
  29. }
  30. return nil
  31. }
[自定义pageControl]

3.代码地址

小小个子大个头

原文链接:http://www.cnblogs.com/4zjq/p/14034333.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号