nontomatic
nontomatic

Reputation: 2043

Turn off UISwitch animations

It occurred to me that toggling switches in the iOS default apps feels more snappy. It looks like the animations are just turned off.

Is there a setting to change this behaviour on UISwitch?

This does not seem to do the trick:

UISwitch.setAnimationsEnabled(enabled: false)

Upvotes: 0

Views: 2215

Answers (3)

Matteo
Matteo

Reputation: 49

On UISwitch you can call setOn with animated param = false

Upvotes: -1

3f9
3f9

Reputation: 1

I'm still wondering why Apple silently removed the animations in their system apps. – nontomatic

Many areas in iOS default apps make this no animation UISwitch - On/Off button e.g

  • Wi-Fi, Personal Hotspot, under Settings.app

  • Set up an alarm in the Clock.app

Here broken on / off button animation e.g

  • Bold Text (activate the option, then comes a message and tap on cancel) under Settings.app
  • Open App Store.app create Apple ID and confirm the terms of use
  • and so many more areas...

On my iPad 9.7, iPhone 5S and iPhone XS Max with iOS 12.2 reproducible. That are bugs since iOS 7 and Apple never fix this for iOS default apps.

Many third party apps take over the same bugs, because since iOS 7 and until today it has never been fixed properly. Always the developers have to check it themselves and fix it in their apps (or discovered by user). For special processes e.g. where an on / off button takes over many functions will be even worse. Many animations (since iOS 7) are broken.

Note!! In iOS 6 (iPhone 5), iOS 5 (iPhone 4S) no problems with UISwitch - On/Off buttons animations (nothing broken) under iOS default apps. All third-party apps don't have these problems either, which was developed under this iOS version at that time.

Upvotes: 0

petertoth
petertoth

Reputation: 249

I don't think there is an elegant solution to this one, but I found a hacky solution which actually works.

UISwitch has a subview which has 2 gesture recognizers attached to it: one for the long press and one for the pan. If you subclass UISwitch, remove these gesture recognizers and add a new one right after initialization, you can achieve a non-animated behavior.

Please keep in mind that this solution might not work in future iOS versions as the internal implementation which this solution relies on might change.

Here is a code snippet that worked for me:

class PTSwitch: UISwitch {

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

    let firstSubview = subviews[0]

    guard let gestureRecognizers = firstSubview.gestureRecognizers else {
        return
    }

    for recognizer in gestureRecognizers {
        firstSubview.removeGestureRecognizer(recognizer)
    }

    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(gestureRecognized))
    firstSubview.addGestureRecognizer(tapGestureRecognizer)
}

@objc func gestureRecognized() {
    setOn(!isOn, animated: false)
}

}

As you can see, I removed the built-in gesture recognizers and added a tap gesture recognizer. You can also add an other one for pan gesture recognizing, it should also work. Then in the gesture recognizer's callback function I simply used the setOn function from UISwitch with animated: false to disable the animations.

Upvotes: 1

Related Questions