Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save softlion/06c10cf13eed81ce608e5f6f226bb7cc to your computer and use it in GitHub Desktop.
Save softlion/06c10cf13eed81ce608e5f6f226bb7cc to your computer and use it in GitHub Desktop.
using System;
using Foundation;
using MvvmCross.Binding;
using MvvmCross.Binding.Bindings.Target.Construction;
using MvvmCross.Binding.iOS.Target;
using MvvmCross.Platform.UI;
using UIKit;
namespace Vapolia.Ios.Lib.MvxUi
{
/// <summary>
/// Animates visibility of views that are subviews of a StackView.
/// Uses the StackView's ability to animate the change of the 'Hidden' property.
///
/// Sample usage
/// set.Bind(NoDeviceContainer).For(AnimatedStackVisibilityTargetBinding.Name).To(vm => vm.IsStateDisabled).WithConversion("Visibility");
///
/// Note that performances are very bad with 5 animated items: there is a kind of "pause" before animations really starts.
/// If someone finds why ...
/// </summary>
[Preserve]
public class AnimatedStackVisibilityTargetBinding : MvxUIViewVisibilityTargetBinding
{
public const string Name = "AnimatedStackVisibility";
[Preserve]
public AnimatedStackVisibilityTargetBinding(UIView target) : base(target)
{
}
public static void Register(IMvxTargetBindingFactoryRegistry registry)
{
registry.RegisterCustomBindingFactory<UIView>(Name, view => new AnimatedStackVisibilityTargetBinding(view));
}
[Preserve]
public override Type TargetType => typeof(UIView);
[Preserve]
public override MvxBindingMode DefaultMode => MvxBindingMode.OneWay;
[Preserve]
protected override void SetValueImpl(object target, object value)
{
var view = target as UIView;
if (view == null)
return;
var visible = (MvxVisibility) value == MvxVisibility.Visible; //value.ConvertToBoolean();
////Disable animation when view is bound after a cell reuse
//if (view.Tag == 1)
//{
// view.Tag = 0;
// view.Alpha = visible == MvxVisibility.Visible ? 1 : 0;
// return;
//}
if (!UIView.AnimationsEnabled)
{
view.Hidden = !visible;
view.Alpha = visible ? 1 : 0;
}
else
{
UIView.Animate(.4, () =>
{
view.Hidden = !visible;
view.Alpha = visible ? 1 : 0;
});
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment