Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit cdbb6e5

Browse files
Char0394jfversluisAndreiMisiukevichalmirvuk
authored
Added Animation Behavior (#169)
* Adding Animation Behavior * Removing Duplicate items * Fix * Update XamarinCommunityToolkit/Behaviors/Animations/AnimationTypes/FadeAnimation.shared.cs Co-authored-by: Gerald Versluis <[email protected]> * Update XamarinCommunityToolkit/Behaviors/Animations/AnimationBehaviour.shared.cs Co-authored-by: Andrei <[email protected]> * Microsoft.Toolkit.Xamarin.Forms namespace rename. (#167) * Huge rename of namepsace. * Update azure-pipelines.yml * NuGet rename * Merge fix Co-authored-by: Gerald Versluis <[email protected]> Co-authored-by: Gerald Versluis <[email protected]> * Adding Animation Behavior * Removing Duplicate items * Fix * Update XamarinCommunityToolkit/Behaviors/Animations/AnimationTypes/FadeAnimation.shared.cs Co-authored-by: Gerald Versluis <[email protected]> * Update XamarinCommunityToolkit/Behaviors/Animations/AnimationBehaviour.shared.cs Co-authored-by: Andrei <[email protected]> * Applying PR feedbacks * Remove EasingType Co-authored-by: Gerald Versluis <[email protected]> Co-authored-by: Andrei <[email protected]> Co-authored-by: Almir Vuk <[email protected]> Co-authored-by: Gerald Versluis <[email protected]>
1 parent 60092a2 commit cdbb6e5

15 files changed

+351
-5
lines changed

XamarinCommunityToolkit.UnitTests/Converters/ItemSelectedEventArgsConverter_Tests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ public class ItemSelectedEventArgsConverter_Tests
1313

1414
public static IEnumerable<object[]> GetData() => new List<object[]>
1515
{
16+
// We know it's deprecated, still good to test it
17+
#pragma warning disable CS0618 // Type or member is obsolete
1618
new object[] { new SelectedItemChangedEventArgs(expectedValue), expectedValue},
19+
#pragma warning restore CS0618 // Type or member is obsolete
1720
};
1821

1922
[Theory]

XamarinCommunityToolkit.UnitTests/Converters/ItemTappedEventArgsConverter_Tests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ public class ItemTappedEventArgsConverter_Tests
1313

1414
public static IEnumerable<object[]> GetData() => new List<object[]>
1515
{
16+
// We know it's deprecated, still good to test it
17+
#pragma warning disable CS0618 // Type or member is obsolete
1618
new object[] { new ItemTappedEventArgs(null, expectedValue), expectedValue},
19+
#pragma warning restore CS0618 // Type or member is obsolete
1720
};
1821

1922
[Theory]
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using Xamarin.Forms;
2+
3+
namespace Microsoft.Toolkit.Xamarin.Forms.Behaviors
4+
{
5+
public class AnimationBehavior : EventToCommandBehavior
6+
{
7+
public static readonly BindableProperty AnimationTypeProperty =
8+
BindableProperty.Create(nameof(AnimationType), typeof(AnimationBase), typeof(AnimationBehavior));
9+
10+
public AnimationBase AnimationType
11+
{
12+
get => (AnimationBase)GetValue(AnimationTypeProperty);
13+
set => SetValue(AnimationTypeProperty, value);
14+
}
15+
16+
bool isAnimating;
17+
TapGestureRecognizer tapGestureRecognizer;
18+
19+
protected override void OnAttachedTo(View bindable)
20+
{
21+
base.OnAttachedTo(bindable);
22+
23+
if (!string.IsNullOrWhiteSpace(EventName))
24+
return;
25+
26+
tapGestureRecognizer = new TapGestureRecognizer();
27+
tapGestureRecognizer.Tapped += OnTriggerHandled;
28+
View.GestureRecognizers.Clear();
29+
View.GestureRecognizers.Add(tapGestureRecognizer);
30+
}
31+
32+
protected override void OnDetachingFrom(View bindable)
33+
{
34+
if (tapGestureRecognizer != null)
35+
tapGestureRecognizer.Tapped -= OnTriggerHandled;
36+
37+
base.OnDetachingFrom(bindable);
38+
}
39+
40+
protected override async void OnTriggerHandled(object sender = null, object eventArgs = null)
41+
{
42+
if (isAnimating)
43+
return;
44+
45+
isAnimating = true;
46+
47+
await AnimationType?.Animate((View)sender);
48+
49+
if (Command?.CanExecute(CommandParameter) ?? false)
50+
Command.Execute(CommandParameter);
51+
52+
isAnimating = false;
53+
54+
base.OnTriggerHandled(sender, eventArgs);
55+
}
56+
}
57+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Threading.Tasks;
2+
using Xamarin.Forms;
3+
4+
namespace Microsoft.Toolkit.Xamarin.Forms.Behaviors
5+
{
6+
public abstract class AnimationBase : BindableObject
7+
{
8+
public static readonly BindableProperty DurationProperty =
9+
BindableProperty.Create(nameof(Duration), typeof(uint), typeof(AnimationBase), default(uint),
10+
BindingMode.TwoWay, defaultValueCreator: GetDefaultDurationProperty);
11+
12+
public uint Duration
13+
{
14+
get => (uint)GetValue(DurationProperty);
15+
set => SetValue(DurationProperty, value);
16+
}
17+
18+
public static readonly BindableProperty EasingTypeProperty =
19+
BindableProperty.Create(nameof(Easing), typeof(Easing), typeof(AnimationBase), Easing.Linear,
20+
BindingMode.TwoWay);
21+
22+
public Easing Easing
23+
{
24+
get => (Easing)GetValue(EasingTypeProperty);
25+
set => SetValue(EasingTypeProperty, value);
26+
}
27+
28+
static object GetDefaultDurationProperty(BindableObject bindable)
29+
=> ((AnimationBase)bindable).DefaultDuration;
30+
31+
protected abstract uint DefaultDuration { get; set; }
32+
33+
public abstract Task Animate(View view);
34+
}
35+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.Threading.Tasks;
2+
using Xamarin.Forms;
3+
4+
namespace Microsoft.Toolkit.Xamarin.Forms.Behaviors
5+
{
6+
public class FadeAnimation : AnimationBase
7+
{
8+
public static readonly BindableProperty FadeProperty =
9+
BindableProperty.Create(nameof(Fade), typeof(double), typeof(AnimationBase), 0.3, BindingMode.TwoWay);
10+
11+
public double Fade
12+
{
13+
get => (double)GetValue(FadeProperty);
14+
set => SetValue(FadeProperty, value);
15+
}
16+
17+
protected override uint DefaultDuration { get; set; } = 300;
18+
19+
public override async Task Animate(View view)
20+
{
21+
await view.FadeTo(Fade, Duration, Easing);
22+
await view.FadeTo(1, Duration, Easing);
23+
}
24+
}
25+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System.Threading.Tasks;
2+
using Xamarin.Forms;
3+
4+
namespace Microsoft.Toolkit.Xamarin.Forms.Behaviors
5+
{
6+
public class FlipHorizontalAnimation : RotateAnimation
7+
{
8+
protected override double DefaultRotation { get; set; } = 90;
9+
protected override uint DefaultDuration { get; set; } = 300;
10+
11+
public override async Task Animate(View view)
12+
{
13+
await view.RotateYTo(Rotation, Duration, Easing);
14+
await view.RotateYTo(0, Duration, Easing);
15+
}
16+
}
17+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.Threading.Tasks;
2+
using Xamarin.Forms;
3+
4+
namespace Microsoft.Toolkit.Xamarin.Forms.Behaviors
5+
{
6+
public class FlipVerticalAnimation : RotateAnimation
7+
{
8+
protected override double DefaultRotation { get; set; } = 90;
9+
10+
public override async Task Animate(View view)
11+
{
12+
await view.RotateXTo(Rotation, Duration, Easing);
13+
await view.RotateXTo(0, Duration, Easing);
14+
}
15+
}
16+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.Threading.Tasks;
2+
using Xamarin.Forms;
3+
4+
namespace Microsoft.Toolkit.Xamarin.Forms.Behaviors
5+
{
6+
public class RotateAnimation : AnimationBase
7+
{
8+
public static readonly BindableProperty RotationProperty =
9+
BindableProperty.Create(nameof(Rotation), typeof(double), typeof(AnimationBase), 180.0, BindingMode.TwoWay, defaultValueCreator: GetDefaulRotationProperty);
10+
11+
public double Rotation
12+
{
13+
get => (double)GetValue(RotationProperty);
14+
set => SetValue(RotationProperty, value);
15+
}
16+
17+
static object GetDefaulRotationProperty(BindableObject bindable)
18+
=> ((RotateAnimation)bindable).DefaultRotation;
19+
20+
protected override uint DefaultDuration { get; set; } = 200;
21+
protected virtual double DefaultRotation { get; set; } = 180.0;
22+
23+
public override async Task Animate(View view)
24+
{
25+
await view.RotateTo(Rotation, Duration, Easing);
26+
view.Rotation = 0;
27+
}
28+
}
29+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.Threading.Tasks;
2+
using Xamarin.Forms;
3+
4+
namespace Microsoft.Toolkit.Xamarin.Forms.Behaviors
5+
{
6+
public class ScaleAnimation : AnimationBase
7+
{
8+
public static readonly BindableProperty ScaleProperty =
9+
BindableProperty.Create(nameof(Scale), typeof(double), typeof(AnimationBase), 1.2, BindingMode.TwoWay);
10+
11+
public double Scale
12+
{
13+
get => (double)GetValue(ScaleProperty);
14+
set => SetValue(ScaleProperty, value);
15+
}
16+
17+
protected override uint DefaultDuration { get; set; } = 170;
18+
19+
public override async Task Animate(View view)
20+
{
21+
await view.ScaleTo(Scale, Duration, Easing);
22+
await view.ScaleTo(1, Duration, Easing);
23+
}
24+
}
25+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.Threading.Tasks;
2+
using Xamarin.Forms;
3+
4+
namespace Microsoft.Toolkit.Xamarin.Forms.Behaviors
5+
{
6+
public class ShakeAnimation : AnimationBase
7+
{
8+
public static readonly BindableProperty StartFactorProperty =
9+
BindableProperty.Create(nameof(StartFactor), typeof(double), typeof(AnimationBase), 15.0, BindingMode.TwoWay);
10+
11+
public double StartFactor
12+
{
13+
get => (double)GetValue(StartFactorProperty);
14+
set => SetValue(StartFactorProperty, value);
15+
}
16+
17+
protected override uint DefaultDuration { get; set; } = 50;
18+
19+
public override async Task Animate(View view)
20+
{
21+
for (var i = StartFactor; i > 0; i = i-5)
22+
{
23+
await view.TranslateTo(-i, 0, Duration, Easing);
24+
await view.TranslateTo(i, 0, Duration, Easing);
25+
}
26+
27+
view.TranslationX = 0;
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)