【问题标题】:UISwitch broken on / off animation while animating 'onTintColor'UISwitch 在动画“onTintColor”时打开/关闭动画
【发布时间】:2017-07-09 23:46:17
【问题描述】:

我想为 UISwitch 的“on”颜色设置动画,并使其保持可交互性,而不影响它的开/关动画。这是我尝试过的:

  1. UIView 的动画持续时间方法不起作用,因为好吧……它 不应该,'onTintColor' 没有被列为动画属性 界面视图。很好。

    mySwitch.onTintColor = UIColor.green
    
    UIView.animate(withDuration: 10) {
        mySwitch.onTintColor = UIColor.red
    }
    
  2. 接下来。 UIView 的视图转换方法提供了一种正确的视觉行为,但是当与开关交互时,默认的开/关动画会受到影响(参见 GIF)。

    UIView.transition(with: mySwitch, duration: 10, options: [.transitionCrossDissolve, .allowUserInteraction], animations: { 
        mySwitch.onTintColor = UIColor.red
    }) { completed in
    }
    
  3. CATransition 给出与 no 相同的行为。 2(见 GIF)。

    var transition = CATransition()
    transition.type = kCATransitionFade
    transition.duration = 10
    mySwitch.layer.add(transition, forKey: kCATransition)
    mySwitch.onTintColor = UIColor.red
    

via GIPHY

有人知道如何在不影响默认开/关动画的情况下为“onTintColor”设置动画吗?

正确执行此操作的唯一方法是创建自定义开关吗?

【问题讨论】:

    标签: ios animation core-animation uiviewanimation ios-animations


    【解决方案1】:

    默认情况下,动画期间会禁用用户交互。尝试使用较长形式的 UIView 动画,

    animate(withDuration:delay:options:animations:completion:),并传入.allowUserInteraction的选项

    【讨论】:

    • 感谢您的建议。将该选项传递给带有视图的过渡确实可以做到它所说的很棒,但它也会影响开关的默认开/关动画。请查看我的编辑和链接的 GIF :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-19
    相关资源
    最近更新 更多