简体   繁体   English

如何停止UISegmentedControl段动画

[英]How to stop UISegmentedControl segments animation

Is there any way to stop the animation of the method insertSegment(withTitle:at:animated) ? 有什么办法可以停止方法insertSegment(withTitle:at:animated)吗? Even if I set animated:false the segment animates, look this: 即使我将animated:false设置为animated:false片段,也请看以下内容:

错误的GIF动画

Please ignore the red ball. 请忽略红球。

The code that I am using to create the component is: 我用来创建组件的代码是:

let segmentedControl = UISegmentedControl()
segmentedControl.sendActions(for: UIControlEvents.valueChanged)
segmentedControl.tintColor = UIColor.white
segmentedControl.selectedSegmentIndex = 0
view.addSubview(segmentedControl)

The constraint setup is ok, the frame width (100% - 16 pixels both sides) is the same all the time. 约束设置可以,帧宽度(两侧均为100%-16像素)始终相同。 The problem is related of the segments, not the parent view. 问题与细分有关,而不是父视图。

To add segments (this happens after a settings check): 要添加细分(在设置检查后会发生这种情况):

segmentedControl.insertSegment(withTitle: "Bolinha 1", at: 0, animated: false)

Setting the frame doesn't work, since the problem is related to the inner views. 设置框架不起作用,因为问题与内部视图有关。

I tried to remove it using setAnimationsEnabled(false) but doesn't work. 我试图使用setAnimationsEnabled(false)删除它,但是不起作用。

Edit 1: Using segmentedControl.setWidth(segmentedControl.frame.width/3, forSegmentAt: 0) doesn't work. 编辑1:使用segmentedControl.setWidth(segmentedControl.frame.width/3, forSegmentAt: 0)不起作用。

Edit 2: Starting the view with UISegmentedControl(items: ["", "", ""]) does it replacing the whole component, this is a very heavy solution for this problem btw... 编辑2:从UISegmentedControl(items: ["", "", ""])开始视图是否替换了整个组件,这是一个非常沉重的解决此问题的方法...

The project uses Swift 3.1. 该项目使用Swift 3.1。

Thank you. 谢谢。

I discovered that a animation has modifying the behaviour of the inner components. 我发现动画可以修改内部组件的行为。

Removing this animation fixed the problem: 删除此动画可解决问题:

view.setNeedsUpdateConstraints()
view.updateConstraintsIfNeeded()

UIView.animate(
  withDuration: 0.5,
  delay: 0.5,
  usingSpringWithDamping: 1.0,
  initialSpringVelocity: 2,
  animations: { [weak self] in
    self?.view.layoutIfNeeded()
  }, completion: nil)

The correct solution for this is to avoid any animation during the build of constraints. 正确的解决方案是在构建约束时避免任何动画。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM