Exemple #1
0
    public static void To(object target,
                          float duration,
                          KeyPaths properties,
                          object options,
                          EasingFormula easing,
                          Action onComplete)
    {
        CALayer layer;

        if (TypeUtils.IsUIView(target))
        {
            layer = (target as UIView).Layer;
        }
        else
        {
            layer = target as CALayer;
        }

//		layer.RemoveAllAnimations(); // kill any previous animations hanging around

        if (easing == null)
        {
            easing = Easing.EaseInSine;
        }

        var animations = new List <CAKeyFrameAnimation>();
        var propType   = properties.GetType();

        foreach (var field in propType.GetFields(BindingFlags.Instance |
                                                 BindingFlags.NonPublic |
                                                 BindingFlags.Public))
        {
            if (field.GetValue(properties) != null)
            {
                var ka = new CAKeyFrameAnimation();
                ka.KeyPath        = LayerUtils.GetKeyPath(field.Name);
                ka.BeginTime      = 0;
                ka.Duration       = duration;
                ka.TimingFunction = CAMediaTimingFunction.FromName(CAMediaTimingFunction.Linear);

                var fromValue = LayerUtils.GetCurrentValue(layer, field.Name);
                var toValue   = Convert.ToSingle(field.GetValue(properties));
                ka.Values = KeyFrameUtils.CreateKeyValues((float)fromValue, (float)toValue, easing);

                if (TypeUtils.IsUIView(target))
                {
                    setLayerProperties(layer, field.Name, toValue);
                }

                animations.Add(ka);
            }
        }

        var localTime = CAAnimation.CurrentMediaTime();
        var group     = CAAnimationGroup.CreateAnimation();

        group.BeginTime           = localTime; // TODO + delay from options...
        group.Duration            = duration;
        group.FillMode            = CAFillMode.Forwards;
        group.RemovedOnCompletion = false;
        //		group.AutoReverses = false;
        //		group.RepeatCount = 0;

        if (onComplete != null)
        {
            group.AnimationStopped += (object sender, CAAnimationStateEventArgs e) => {
                onComplete.Invoke();
            };
        }

        group.Animations = animations.ToArray();
        layer.AddAnimation(group, null);
    }
Exemple #2
0
 public void CanGetKeyPathForProperty()
 {
     Assert.AreEqual("position.x", LayerUtils.GetKeyPath("PositionX"));
 }