如何为分段控件选择设置动画?

时间:2018-09-01 03:19:19

标签: ios swift uisegmentedcontrol

我有一个.r { } ,其中有3个可能的选择,并且我在第一个选择中添加了彩色下划线。我试图弄清楚如何对下划线进行动画处理,以使其突出显示新选择,但是下划线栏没有移动。

我不希望通过动画更改索引。我试图弄清楚如何使下划线栏UIView保持在所选的同一段中。我已经看到了这种实现方式。

以下是当前情况的屏幕录像-https://imgur.com/a/jTQ8z1M

我通过UISegmentedControl添加了UISegmentedControl,然后在代码中使用view.addSubview(underlineBar)添加了下划线。这就是我所拥有的:

Storyboard

正在调用 class ViewController: UIViewController { @IBOutlet weak var segmentedControl: UISegmentedControl! override func viewDidLoad() { super.viewDidLoad() setSegmentedControlStyle() } func setSegmentedControlStyle() { segmentedControl.backgroundColor = .clear segmentedControl.tintColor = .clear segmentedControl.setTitleTextAttributes([NSAttributedStringKey.font : UIFont.systemFont(ofSize: 16), NSAttributedStringKey.foregroundColor: UIColor.lightGray ], for: .normal) segmentedControl.setTitleTextAttributes([NSAttributedStringKey.font : UIFont.systemFont(ofSize: 16), NSAttributedStringKey.foregroundColor: UIColor.blue ], for: .selected) let underlineBar = UIView() underlineBar.translatesAutoresizingMaskIntoConstraints = false // false since we are using auto layout constraints underlineBar.backgroundColor = UIColor.blue view.addSubview(underlineBar) underlineBar.topAnchor.constraint(equalTo: segmentedControl.bottomAnchor).isActive = true underlineBar.heightAnchor.constraint(equalToConstant: 3).isActive = true underlineBar.leftAnchor.constraint(equalTo: segmentedControl.leftAnchor).isActive = true underlineBar.widthAnchor.constraint(equalTo: segmentedControl.widthAnchor, multiplier: 1 / CGFloat(segmentedControl.numberOfSegments)).isActive = true UIView.animate(withDuration: 0.3) { underlineBar.frame.origin.x = (self.segmentedControl.frame.width / CGFloat(self.segmentedControl.numberOfSegments)) * CGFloat(self.segmentedControl.selectedSegmentIndex) } } } ,但是什么也没发生。

1 个答案:

答案 0 :(得分:1)

问题是您要为 underlineBar 设置约束,这些约束将更新X,Y和width,height属性,因此当您尝试设置位置变化的动画时,视图不会移动,因为约束具有更高的优先级。总之,您将需要设置约束动画。

我创建了一个全局 leftAnchor ,您每次按下该细分时都会对其进行修改。

var underlineBarLeftAnchor: NSLayoutConstraint!

在设置约束的地方,使用以下命令更新功能:

underlineBar.topAnchor.constraint(equalTo: segmentedControl.bottomAnchor).isActive = true
underlineBar.heightAnchor.constraint(equalToConstant: 3).isActive = true
underlineBar.widthAnchor.constraint(equalTo: segmentedControl.widthAnchor, multiplier: 1 / CGFloat(segmentedControl.numberOfSegments)).isActive = true

underlineBarLeftAnchor = underlineBar.leftAnchor.constraint(equalTo: segmentedControl.leftAnchor, constant: 0)
underlineBarLeftAnchor.isActive = true

现在,您将需要使用 valueChanged 常量为分段控件注册目标操作方法,如下所示,更新 viewDidLoad()

override func viewDidLoad() {
    super.viewDidLoad()

    setSegmentedControlStyle()
    segmentedControl.addTarget(self, action: #selector(updateSegmentedControl), for: .valueChanged)

}

最后一步,创建 updateSegmentedControl()函数,在其中为运动设置动画:

  

每次更改分段控件的值时都会调用此函数

@objc func updateSegmentedControl() {
    let padding = underlineBar.frame.width * CGFloat(self.segmentedControl.selectedSegmentIndex)
    underlineBarLeftAnchor.constant = padding

    UIView.animate(withDuration: 0.3) {
        self.view.layoutIfNeeded()
    }
}

您需要计算所需的左填充量,然后在对更改进行动画处理的同时更新ui layoutIfNeeded()