Monday, 29 February 2016

iOS Swift animation - shake element on input error

We're used to seeing an input element shake when input has failed validation, like a person shaking their head at you. Here's the code to create the animation:

public func errorShake(element: UIView!){
        let animation = CABasicAnimation(keyPath: "position")
        animation.duration = 0.07
        animation.repeatCount = 4
        animation.autoreverses = true
        animation.fromValue = NSValue(CGPoint: CGPointMake( - 10,
        animation.toValue = NSValue(CGPoint: CGPointMake( + 10,
        element.layer.addAnimation(animation, forKey: "position")

Thursday, 25 February 2016

Morph a view into another in Swift

Today I was tasked with morphing a button, when tapped, into another view that I'd already constructed of a bespoke date selector.

I achieved this by adding an extension to UIView, with a function that animates the bounds, frame and background colour of one view to another:

   func morphToView(duration:Double,delay:Double,toView:UIView){
        UIView.animateWithDuration(duration, delay: delay, usingSpringWithDamping: 1.0, initialSpringVelocity: 10, options: [], animations: {
            self.bounds = toView.bounds;
            self.frame = toView.frame
            self.backgroundColor = toView.backgroundColor
            }, completion: nil)


making it super easy to use:

btnYes.morphToView(1.3, delay: 0.0, toView: dateSelector)

Thursday, 18 February 2016

Xamarin Android aapt.exe exited with code 1, Resources.deisgner.cs not regenerating

Wasted ages on this - Resources.deisgner.cs was not updating and eventually I was getting an error "Xamarin Android aapt.exe exited with code 1".

The solution turned out be simple - I'd added some images to my resources folder that contained hyphens in their filenames. I removes these, and everything started working again.

Xamarin Android detect Android version and perform circular reveal

Working on a simple circular reveal using Animator, I needed to detect if we were running  >=Lollipop or not. This is how I did it. I'll probably refactor this somewhat but here's the headlines:

           var _circle = FindViewById<imageview>(Resource.Id.imageView1);
           var currentapiVersion = Android.OS.Build.VERSION.SdkInt;
            if (currentapiVersion >= Android.OS.BuildVersionCodes.Lollipop)
                // Do something for lollipop and above versions
                // get the center for the clipping circle
                int cx = _circle.Width / 2;
                int cy = _circle.Height / 2;

                // get the final radius for the clipping circle
                var finalRadius = (float)Math.Sqrt(cx * cx + cy * cy);

                // create the animator for this view (the start radius is zero)
                Animator anim =
                    ViewAnimationUtils.CreateCircularReveal(_circle, cx, cy, 0, finalRadius);

                // make the view visible and start the animation
                _circle.Visibility = ViewStates.Visible;
                _circle.Visibility = ViewStates.Visible;

Xamarin C# equivalent of Math.hypot

I'm doing some more Xamarin for the next few weeks, and today I'm working on a material-design circular reveal for an Android app. I had to convert this line of Java (amongst others) into C#:

float finalRadius = (float)Math.hypot(cx, cy);

And...the answer is.....

var finalRadius = (float)Math.Sqrt(cx * cx + cy * cy);