diff options
author | Jason Smith <jason.smith@xamarin.com> | 2016-03-22 13:02:25 -0700 |
---|---|---|
committer | Jason Smith <jason.smith@xamarin.com> | 2016-03-22 16:13:41 -0700 |
commit | 17fdde66d94155fc62a034fa6658995bef6fd6e5 (patch) | |
tree | b5e5073a2a7b15cdbe826faa5c763e270a505729 /Xamarin.Forms.Core/Tweener.cs | |
download | xamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.tar.gz xamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.tar.bz2 xamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.zip |
Initial import
Diffstat (limited to 'Xamarin.Forms.Core/Tweener.cs')
-rw-r--r-- | Xamarin.Forms.Core/Tweener.cs | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/Xamarin.Forms.Core/Tweener.cs b/Xamarin.Forms.Core/Tweener.cs new file mode 100644 index 00000000..73ed7853 --- /dev/null +++ b/Xamarin.Forms.Core/Tweener.cs @@ -0,0 +1,124 @@ +// +// Tweener.cs +// +// Author: +// Jason Smith <jason.smith@xamarin.com> +// +// Copyright (c) 2012 Xamarin Inc. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +using System; + +namespace Xamarin.Forms +{ + internal class Tweener + { + long _lastMilliseconds; + + int _timer; + + public Tweener(uint length) + { + Value = 0.0f; + Length = length; + Loop = false; + } + + public AnimatableKey Handle { get; set; } + + public uint Length { get; } + + public bool Loop { get; set; } + + public double Value { get; private set; } + + public event EventHandler Finished; + + public void Pause() + { + if (_timer != 0) + { + Ticker.Default.Remove(_timer); + _timer = 0; + } + } + + public void Start() + { + Pause(); + + _lastMilliseconds = 0; + _timer = Ticker.Default.Insert(step => + { + long ms = step + _lastMilliseconds; + + Value = Math.Min(1.0f, ms / (double)Length); + + _lastMilliseconds = ms; + + if (ValueUpdated != null) + ValueUpdated(this, EventArgs.Empty); + + if (Value >= 1.0f) + { + if (Loop) + { + _lastMilliseconds = 0; + Value = 0.0f; + return true; + } + + if (Finished != null) + Finished(this, EventArgs.Empty); + Value = 0.0f; + _timer = 0; + return false; + } + return true; + }); + } + + public void Stop() + { + Pause(); + Value = 1.0f; + if (Finished != null) + Finished(this, EventArgs.Empty); + Value = 0.0f; + } + + public event EventHandler ValueUpdated; + + ~Tweener() + { + if (_timer != 0) + { + try + { + Ticker.Default.Remove(_timer); + } + catch (InvalidOperationException) + { + } + } + _timer = 0; + } + } +}
\ No newline at end of file |