Forum Xamarin.iOS
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

No animation when setting UISwitch.On property programatically

wellhatwellhat Member ✭✭

Hi all,

I have a custom cell with a label and a switch on the right hand side to denote whether the switch is activated or not.

I would like the switch to also be toggled if the user taps the row, rather than requiring them to actually use the switch.

I got this to work, but unlike when the user taps the switch itself, when they tap the row it does not animate the change - the switch just turns on and off with no animation at all. This doesn't look good so I need to fix it.

In my RowSelected override, I am doing this:

    var cc = (CustomCell) tableView.CellAt(indexPath);
    cc.ToggleSwitch();

This is the relevant code in CustomCell.cs

        private bool _enabled;
        public bool Enabled 
        { 
            get 
            { 
                return _enabled; 
            }

            set
            {
                _enabled = value;
                this.CustomSwitch.On = _enabled;
            }
        }

        partial void CustomSwitch_ValueChanged(UISwitch sender)
        {
            //ignore this line, it just updates my data storage
            customController.Toggle(item);

            Enabled = sender.On;
        }

        public void ToggleSwitch()
        {
            CustomSwitch.On = !CustomSwitch.On;

            //tried this already and it did not work
            //UIView.Animate(0.3, () => { CustomSwitch.SendActionForControlEvents(UIControlEvent.ValueChanged); });

            CustomSwitch.SendActionForControlEvents(UIControlEvent.ValueChanged);
        }

I hope someone knows how to resolve this :)

Tagged:

Best Answer

Answers

  • wellhatwellhat Member ✭✭

    Thanks, that worked!

Sign In or Register to comment.