我正在尝试为我在屏幕上创建的Switch设置过渡动画选项。
所以我使用了下面的实例方法。
func setOn(_ on: Bool, animated: Bool)
我在Apple开发人员文档中看到了描述,第二个参数决定了动画的差异,但实际上,对true和false都适用于动画。https://developer.apple.com/documentation/uikit/uiswitch/1623686-seton
我用真实的手机测试过,但这是同样的问题。我检查的环境:
这是我的代码:(唯一的区别是“动画:布尔”的值)
self.switch1 = UISwitch()
self.switch1.frame = CGRect(x: 120, y: 150, width: 50, height: 30)
self.switch1.setOn(true, animated: true)
self.view.addSubview(switch1)
self.switch2 = UISwitch()
self.switch2.frame = CGRect(x: 120, y: 250, width: 50, height: 30)
self.switch2.setOn(true, animated: false)
self.view.addSubview(switch2)
怎么了?我该如何关闭动画选项?
该动画是UISwitch
用户交互的默认反馈,您不能从直接交互中禁用该动画,但是我们可以防止其超越控件并使用自己的交互(并且仍然可以通过编程方式对其进行动画处理)。
经过Xcode 12.1 / iOS 14.1测试
这是可能的方法:
class MySwitch: UISwitch {
override init(frame: CGRect) {
super.init(frame: frame)
let view = UIView()
view.isUserInteractionEnabled = true
self.addSubview(view)
view.translatesAutoresizingMaskIntoConstraints = false
view.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
view.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
view.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
view.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
let gr = UITapGestureRecognizer(target: self, action: #selector(toggle(_ :)))
view.addGestureRecognizer(gr)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@objc func toggle(_ sender: Any?) {
self.setOn(!self.isOn, animated: false)
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句