I'm trying to set the transition animation options for the Switch I created on the screen.
So I used the Instance Method below.
func setOn(_ on: Bool, animated: Bool)
I saw description in the apple dev documentation that the second param decide the difference in animation, but in reality both true/false worked with animation included. https://developer.apple.com/documentation/uikit/uiswitch/1623686-seton
Emulator demonstration gif image
I tested it with a real phone, but it was the same issue. Environment I checked:
- macOS Catalina 10.15.7
- Xcode 12.1
- Simulator: iOS 14.2, iOS 13.7, iOS 13.1
- iPhone XR (iOS 14.2)
Here is my code : (The only difference is the value of "animated: Bool")
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)
What's wrong and how can I turn off the animation options?
setOn(...)
before you've added the switch to the view - so animated or not won't matter... the first time you see the switch on the screen it will be in the "On" state. Second,.setOn(...)
, animated or not, is a function you could call based on some other action - such as changing the state of theswitch1
when the user taps onswitch2
. Are you trying to disable the animation when the switch is tapped? – DonMag