It is a common issue in MonoTouch that function performSelector with delay attribute is not working out of the box, you need to fiddle with the NSRunLoop as per the comment from Geoff Norton from Novell:
PerformSelector with delay works fine, given that you understand its restrictions. It only works on threads with a NSRunLoop, and is completed async.
So what to do, and do not want to or cannot get into the NSRunLoop “issue”?
MonoTouch with full C# again excels there with the nice ability to cheat a bit with NSTimer and .NET delegates.
Here is a short code snippet of the helper class:
public delegate void DelegateFnc (); public delegate void DelegateViewFnc (UIView view); public class HelperSelector { public static void PerformSelector (DelegateFnc delegateFnc, int delaySeconds, int delayMiliSeconds) { NSTimer.CreateScheduledTimer (new TimeSpan (0, 0, 0, delaySeconds, delayMiliSeconds), delegate { delegateFnc (); }); } public static void PerformSelector (DelegateViewFnc delegateFnc, UIView view, int delaySeconds, int delayMiliSeconds) { NSTimer.CreateScheduledTimer (new TimeSpan (0, 0, 0, delaySeconds, delayMiliSeconds), delegate { delegateFnc (view); }); } }
So there you go, simply call the HelperSelector.PerformSelector function to call it with a delay, I have prepared two delegates, one for void function with no params and void function with UIView param, as those are most used by me, anyone can add any other delegates and create an overloaded function in the helper class.
Now lets have a look how to call the function:
HelperSelector.PerformSelector (new DelegateViewFnc (StartHideAnimationForView), view, 5, 0); public void StartHideAnimationForView (UIView view) { UIView.BeginAnimations (null); UIView.SetAnimationDuration (0.5); UIView.SetAnimationDelegate (this); view.Alpha = 0; UIView.CommitAnimations (); }
I call the StartHideAnimationForView with a delay and provided a simple code inside just for the sample purposes.
Happy coding!