I need to cancel the animation to a switch when its on / off state changes, I have a list with reusable cells and this animation does not look good when you scroll.
This animation is by design so we can't remove it when the user clicks the switch directly to change its state. But we could remove it using custom renderer if we use code behind to programmatically change its status.
[assembly: ExportRenderer(typeof(Switch), typeof(CustomSwitchRenderer))] namespace App.iOS { public class CustomSwitchRenderer : SwitchRenderer { protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName == nameof(Element.IsToggled)) { Control.On = Element.IsToggled; } else { base.OnElementPropertyChanged(sender, e); } } } }
[assembly: ExportRenderer(typeof(Xamarin.Forms.Switch), typeof(CustomSwitchRenderer))] namespace App.Droid { public class CustomSwitchRenderer : SwitchRenderer { public CustomSwitchRenderer(Context context) : base(context) { } protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName == nameof(Element.IsToggled)) { Control.Checked = Element.IsToggled; Control.JumpDrawablesToCurrentState(); } else { base.OnElementPropertyChanged(sender, e); } } } }
I found it disabled the animations on Android even though this status is changed by clicking:
Answers
This animation is by design so we can't remove it when the user clicks the switch directly to change its state. But we could remove it using custom renderer if we use code behind to programmatically change its status.
For iOS:
For Android:
I found it disabled the animations on Android even though this status is changed by clicking:

Thanks, this work fine @LandLu