Animating shadows?

JonasRembrattJonasRembratt SEUniversity ✭✭✭
edited November 2014 in Xamarin.iOS

I'm starting to get the UI i wanted when I envisioned the app I'm currently writing but I haven't figured out how to animate the drop shadows I'm using for my Collection view images. This is my attempt, which doesn't work:

 public class Shadow
 {
        public CGColor Color { get; set; }

        public nfloat Radius { get; set; }

        public float Opacity { get; set; }

        public CGSize Offset { get; set; }

        public void Bind(UIView view, double animationDuration = 0)
        {
            if (view == null) throw new ArgumentNullException("view");
            if (animationDuration <= 0)
            {
                // this works ...
                view.Layer.ShadowColor = Color;
                view.Layer.ShadowRadius = Radius;
                view.Layer.ShadowOpacity = Opacity;
                view.Layer.ShadowOffset = Offset;
                view.Layer.MasksToBounds = false;
                return;
            }
            // this fails ...
            var anim = CABasicAnimation.FromKeyPath("ShadowOpacity");
            anim.From = new NSNumber(0f);
            anim.To = new NSNumber(Opacity);
            anim.Duration = animationDuration;
            view.Layer.AddAnimation(anim, "ShadowOpacity");
        }

        public static Shadow DefaultDropShadow 
        { 
            get 
            {
                return new Shadow { 
                    Color = UIColor.Black.CGColor,
                    Radius = 7f,
                    Opacity = 0.5f,
                    Offset = CGSize.Empty
                };
            } 
        }
 }

Two examples, one working while the other renders no shadow at all:

Example 1, works:

void setDropShadow(UIView someView)
{
    Shadow.DefaultDropShadow.Bind(someView); // <-- SUCCEEDS. (renders a drop shadow)
}

Example 2, fails:

void setDropShadow(UIView someView)
{
    Shadow.DefaultDropShadow.Bind(someView, 300.0); // <-- FAILS (no drop shadow rendered)
}

Any hints would be greatly appreciated!

Answers

  • JonasRembrattJonasRembratt SEUniversity ✭✭✭

    Would love to hear a few pointers if anyone know why this doesn't work. :-)

  • Tom-PrattTom-Pratt GBMember ✭✭
    edited December 2014

    Is it something as simple as changing to...

         var anim = CABasicAnimation.FromKeyPath("shadowOpacity");
    

    With a lower case 's'. Afterall I believe we're talking down in terms of objective c at this stage. Hence the NSNumbers and so on.

  • JonasRembrattJonasRembratt SEUniversity ✭✭✭

    Good point but I already tried this with no success.

Sign In or Register to comment.