void initButtons()
        {
            var moveBtn = UIButton.FromType(UIButtonType.RoundedRect);

            moveBtn.Frame = new RectangleF(View.Bounds.Width - 240, 60, 200, 40);
            moveBtn.SetTitle("Move X", UIControlState.Normal);
            moveBtn.TouchUpInside += (object sender, EventArgs e) => {
                if (logoView.Layer.PresentationLayer.Position.X < 400)
                {
                    Rouse.To(logoView, 1, new RouseLib.KeyPaths {
                        PositionX = 400
                    }, Easing.EaseInExpo);

//					Rouse.To (logoLayer, 1, new RouseLib.KeyPaths{ PositionX = 400}, Easing.EaseInExpo);
                    var localTime = CAAnimation.CurrentMediaTime();

                    var ka = new CAKeyFrameAnimation();
                    ka.KeyPath        = "position.x";
                    ka.BeginTime      = 0;
                    ka.Duration       = 1;
                    ka.TimingFunction = CAMediaTimingFunction.FromName(CAMediaTimingFunction.Linear);
                    ka.Values         = KeyFrameUtils.CreateKeyValues(logoLayer.Position.X, 400f, RouseLib.Easing.EaseInExpo);

//					var ka2 = new CAKeyFrameAnimation ();
//					ka2.KeyPath = "opacity";
//					ka2.BeginTime = 0;
//					ka2.Duration = 1;
//					ka2.TimingFunction = CAMediaTimingFunction.FromName (CAMediaTimingFunction.Linear);
//					ka2.Values = KeyFrameUtils.CreateKeyValues (1, 0.2f, RouseLib.Easing.EaseInExpo);
//
                    var group = CAAnimationGroup.CreateAnimation();
                    group.BeginTime           = localTime;
                    group.Duration            = 1;
                    group.FillMode            = CAFillMode.Forwards;
                    group.RemovedOnCompletion = false;
                    group.Animations          = new CAAnimation[] { ka };
                    logoLayer.AddAnimation(group, null);
                }
                else
                {
                    Rouse.To(logoView, 1, new RouseLib.KeyPaths {
                        PositionX = 70
                    }, Easing.EaseOutExpo);

//					Rouse.To (logoLayer, 1, new RouseLib.KeyPaths{ PositionX = 70}, Easing.EaseOutExpo);
                    var localTime = CAAnimation.CurrentMediaTime();

                    var ka = new CAKeyFrameAnimation();
                    ka.KeyPath        = "position.x";
                    ka.BeginTime      = 0;
                    ka.Duration       = 1;
                    ka.TimingFunction = CAMediaTimingFunction.FromName(CAMediaTimingFunction.Linear);
                    ka.Values         = KeyFrameUtils.CreateKeyValues(logoLayer.PresentationLayer.Position.X, 70f, RouseLib.Easing.EaseOutExpo);

                    //					var ka2 = new CAKeyFrameAnimation ();
                    //					ka2.KeyPath = "opacity";
                    //					ka2.BeginTime = 0;
                    //					ka2.Duration = 1;
                    //					ka2.TimingFunction = CAMediaTimingFunction.FromName (CAMediaTimingFunction.Linear);
                    //					ka2.Values = KeyFrameUtils.CreateKeyValues (1, 0.2f, RouseLib.Easing.EaseInExpo);
                    //
                    var group = CAAnimationGroup.CreateAnimation();
                    group.BeginTime           = localTime;
                    group.Duration            = 1;
                    group.FillMode            = CAFillMode.Forwards;
                    group.RemovedOnCompletion = false;
                    group.Animations          = new CAAnimation[] { ka };
                    logoLayer.AddAnimation(group, null);
                }
            };
            View.AddSubview(moveBtn);
        }
Example #2
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);
    }