缩放UIPageControl的当前点并使其保持居中

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了缩放UIPageControl的当前点并使其保持居中相关的知识,希望对你有一定的参考价值。

我已经将UIPageControl子类化,以使其当前点更大。

class CustomPageControl: UIPageControl {
    override var currentPage: Int {
        didSet {
            updateDots()
        }
    }

    func updateDots() {
        let currentDot = subviews[currentPage]
        let largeScaling = CGAffineTransform(scaleX: 3, y: 3)

        subviews.forEach {
            // apply the large scale of newly selected dot
            // restore the normal scale of previously selected dot
            $0.transform = $0 == currentDot ? largeScaling : .identity
        }
    }
}

但是变换的结果不是居中的(红点应该与其他点对齐): enter image description here

我试过(在ios 12上):

  • 改变framecentercurrentDot没有效果。
  • 改变变换以包括translatedBy(x: CGFloat, y: CGFloat)没有效果。
  • 更改约束like here正在制作第一个跳点: currentDot.translatesAutoresizingMaskIntoConstraints = false currentDot.centerYAnchor.constraint(equalTo: self.centerYAnchor, constant: 0) currentDot.centerXAnchor.constraint(equalTo: self.centerXAnchor, constant: 0) enter image description here
答案

我最终通过自己重写所有子视图约束来完成它。

// https://stackoverflow.com/a/55063316/1033581
class DefaultPageControl: UIPageControl {

    override var currentPage: Int {
        didSet {
            updateDots()
        }
    }

    override func sendAction(_ action: Selector, to target: Any?, for event: UIEvent?) {
        super.sendAction(action, to: target, for: event)
        updateDots()
    }

    private func updateDots() {
        let currentDot = subviews[currentPage]
        let largeScaling = CGAffineTransform(scaleX: 3.0, y: 3.0)
        let smallScaling = CGAffineTransform(scaleX: 1.0, y: 1.0)

        subviews.forEach {
            // Apply the large scale of newly selected dot.
            // Restore the small scale of previously selected dot.
            $0.transform = $0 == currentDot ? largeScaling : smallScaling
        }
    }

    override func updateConstraints() {
        super.updateConstraints()
        // We rewrite all the constraints
        rewriteConstraints()
    }

    private func rewriteConstraints() {
        let systemDotSize: CGFloat = 7.0
        let systemDotDistance: CGFloat = 16.0

        let halfCount = CGFloat(subviews.count) / 2
        subviews.enumerated().forEach {
            let dot = $0.element
            dot.translatesAutoresizingMaskIntoConstraints = false
            NSLayoutConstraint.deactivate(dot.constraints)
            NSLayoutConstraint.activate([
                dot.widthAnchor.constraint(equalToConstant: systemDotSize),
                dot.heightAnchor.constraint(equalToConstant: systemDotSize),
                dot.centerYAnchor.constraint(equalTo: centerYAnchor, constant: 0),
                dot.centerXAnchor.constraint(equalTo: centerXAnchor, constant: systemDotDistance * (CGFloat($0.offset) - halfCount))
            ])
        }
    }
}

代码(7.0和16.0)中的系统常量分别是iOS 12上默认UIPageControl点的大小和距离。

result

以上是关于缩放UIPageControl的当前点并使其保持居中的主要内容,如果未能解决你的问题,请参考以下文章

如何删除粘性标题并使其保持静态

使用 PHP 将值插入到可编辑的 PDF 中,并使其保持可编辑状态

如何从球面上的当前点(纬度/经度)找到线段上最近的点

具有装饰器的模拟功能。再次使用相同的装饰器来装饰 Mock 对象并使其保持为 Mock

调整图像大小并使其适合画布大小

缩放会弄乱 uipagecontrol 的当前页面