|
| 1 | +using System; |
| 2 | +using System.Threading.Tasks; |
| 3 | +using Xamarin.Forms; |
| 4 | + |
| 5 | +namespace Plugin.MaterialDesignControls.Animations |
| 6 | +{ |
| 7 | + public static class TouchAndPressAnimation |
| 8 | + { |
| 9 | + public static void Animate(View view, EventType gestureType) |
| 10 | + { |
| 11 | + var touchAndPressEffectConsumer = view as ITouchAndPressEffectConsumer; |
| 12 | + |
| 13 | + switch (gestureType) |
| 14 | + { |
| 15 | + case EventType.Pressing: |
| 16 | + SetAnimation(view, touchAndPressEffectConsumer); |
| 17 | + break; |
| 18 | + case EventType.Cancelled: |
| 19 | + case EventType.Released: |
| 20 | + if (view.IsEnabled && touchAndPressEffectConsumer.Command != null && touchAndPressEffectConsumer.Command.CanExecute(touchAndPressEffectConsumer.CommandParameter)) |
| 21 | + { |
| 22 | + touchAndPressEffectConsumer.Command.Execute(touchAndPressEffectConsumer.CommandParameter); |
| 23 | + } |
| 24 | + |
| 25 | + RestoreAnimation(view, touchAndPressEffectConsumer); |
| 26 | + break; |
| 27 | + case EventType.Ignored: |
| 28 | + RestoreAnimation(view, touchAndPressEffectConsumer); |
| 29 | + break; |
| 30 | + default: |
| 31 | + throw new ArgumentOutOfRangeException(nameof(gestureType), gestureType, null); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + private static void SetAnimation(View view, ITouchAndPressEffectConsumer touchAndPressEffectConsumer) |
| 36 | + { |
| 37 | + if (touchAndPressEffectConsumer.Animation != AnimationTypes.None && view.IsEnabled && (touchAndPressEffectConsumer.Command == null || touchAndPressEffectConsumer.Command.CanExecute(touchAndPressEffectConsumer.CommandParameter))) |
| 38 | + { |
| 39 | + Task.Run(async () => |
| 40 | + { |
| 41 | + if (touchAndPressEffectConsumer.Animation == AnimationTypes.Fade) |
| 42 | + { |
| 43 | + await view.FadeTo(touchAndPressEffectConsumer.AnimationParameter.HasValue ? touchAndPressEffectConsumer.AnimationParameter.Value : 0.6, 100); |
| 44 | + } |
| 45 | + else |
| 46 | + { |
| 47 | + await view.ScaleTo(touchAndPressEffectConsumer.AnimationParameter.HasValue ? touchAndPressEffectConsumer.AnimationParameter.Value : 0.95, 100); |
| 48 | + } |
| 49 | + }); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + private static void RestoreAnimation(View view, ITouchAndPressEffectConsumer touchAndPressEffectConsumer) |
| 54 | + { |
| 55 | + if (touchAndPressEffectConsumer.Animation != AnimationTypes.None) |
| 56 | + { |
| 57 | + Task.Run(async () => |
| 58 | + { |
| 59 | + if (touchAndPressEffectConsumer.Animation == AnimationTypes.Fade) |
| 60 | + { |
| 61 | + await view.FadeTo(1, 100); |
| 62 | + } |
| 63 | + else |
| 64 | + { |
| 65 | + await view.ScaleTo(1, 100); |
| 66 | + } |
| 67 | + }); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
0 commit comments