缩放UIPageControl的当前点并使其居中 [英] Scaling current dot of UIPageControl and keeping it centered
本文介绍了缩放UIPageControl的当前点并使其居中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为了使当前点更大,我对UIPageControl进行了子类化.
I've subclassed UIPageControl in order to have its current dot bigger.
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
}
}
}
但是转换结果未居中(红点应与其他点对齐):
But the result of the transform isn't centered (the red dot should be aligned with the others):
我已经尝试过(在iOS 12上):
I've tried (on iOS 12):
- 更改
currentDot
的frame
或center
无效. - 将转换更改为包含
translatedBy(x: CGFloat, y: CGFloat)
无效. -
改变约束像这里正在使第一个点跳:
- changing the
frame
orcenter
ofcurrentDot
has no effect. - changing the transform to include a
translatedBy(x: CGFloat, y: CGFloat)
has no effect. changing the constraints like here is making the first dot jumping:
currentDot.translatesAutoresizingMaskIntoConstraints = false
currentDot.centerYAnchor.constraint(equalTo: self.centerYAnchor, constant: 0)
currentDot.centerXAnchor.constraint(equalTo: self.centerXAnchor, constant: 0)
推荐答案
我终于通过自己重写 all 子视图约束来使它正常工作.
I got it finally working by rewriting all the subviews constraints by myself.
// 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点的大小和距离.
System constants in the code (7.0 and 16.0) are respectively the size and the distance found for a default UIPageControl dot on iOS 12.
这篇关于缩放UIPageControl的当前点并使其居中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文