diff options
Diffstat (limited to 'Xamarin.Forms.Platform.iOS/CADisplayLinkTicker.cs')
-rw-r--r-- | Xamarin.Forms.Platform.iOS/CADisplayLinkTicker.cs | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/Xamarin.Forms.Platform.iOS/CADisplayLinkTicker.cs b/Xamarin.Forms.Platform.iOS/CADisplayLinkTicker.cs new file mode 100644 index 00000000..29dd732a --- /dev/null +++ b/Xamarin.Forms.Platform.iOS/CADisplayLinkTicker.cs @@ -0,0 +1,73 @@ +using System; +using System.Collections.Concurrent; +using System.Threading; +#if __UNIFIED__ +using UIKit; +using CoreAnimation; +using Foundation; + +#else +using MonoTouch.UIKit; +using MonoTouch.CoreAnimation; +using MonoTouch.Foundation; +#endif + +namespace Xamarin.Forms.Platform.iOS +{ + internal class CADisplayLinkTicker : Ticker + { + readonly BlockingCollection<Action> _queue = new BlockingCollection<Action>(); + CADisplayLink _link; + + public CADisplayLinkTicker() + { + var thread = new Thread(StartThread); + thread.Start(); + } + + internal new static CADisplayLinkTicker Default + { + get { return Ticker.Default as CADisplayLinkTicker; } + } + + public void Invoke(Action action) + { + _queue.Add(action); + } + + protected override void DisableTimer() + { + if (_link != null) + { + _link.RemoveFromRunLoop(NSRunLoop.Current, NSRunLoop.NSRunLoopCommonModes); + _link.Dispose(); + } + _link = null; + } + + protected override void EnableTimer() + { + _link = CADisplayLink.Create(() => SendSignals()); + _link.AddToRunLoop(NSRunLoop.Current, NSRunLoop.NSRunLoopCommonModes); + } + + void StartThread() + { + while (true) + { + var action = _queue.Take(); + var previous = UIApplication.CheckForIllegalCrossThreadCalls; + UIApplication.CheckForIllegalCrossThreadCalls = false; + + CATransaction.Begin(); + action.Invoke(); + + while (_queue.TryTake(out action)) + action.Invoke(); + CATransaction.Commit(); + + UIApplication.CheckForIllegalCrossThreadCalls = previous; + } + } + } +}
\ No newline at end of file |