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?
2
Answers
This animation is default
UISwitch
feedback on user interaction, you cannot disable that animation from direct interaction, but we can prevent it overriding control and use own interaction (and still keep possibility to animate it if needed programmatically).Tested with Xcode 12.1 / iOS 14.1
Here is possible approach:
You have to force call the ‘setOn’ callback every time you press the switch. However, in order for the tapGestureRecognizer to respond to your callback, you have to eliminate all other default gestureRecognizers (found on the subviews).