Tag: Animation

  • Tutorial: Improving your animations with UIViewAnimationOptions

    Animations can add a lot of magic to your application. They can be used to impress your users with great effects and give important user feedback to encourage a positive behavior. Creating animations using the Apple supplied UIView API is quite easy and by using UIViewAnimationOptions correctly, you can either create or improve the animations in your app.

    You might be familiar with the UIView animation API. However one of the lesser used parts of this API is including the UIViewAnimationOptions. This allows you to improve the realism of your animations. These options change the animations time over progress ratio. By doing this, objects accelerate or decelerate whilst animating. This is important because, in the real world, objects do not start and stop instantly and do require time to speed up and slow down. Here is a diagram which shows two of the options. EaseIn starts the animation slowly and accelerates the progress, while EaseOut starts the animation quickly and decelerates the progress.

    TimingCurves

    Tutorial

    Start off with a simple single page application template. Set yourself up three cars (all at the same Y position origin) and a race button above the vehicles. The labels above the vehicles and the red finish line are optional.

    Screen Shot 2018-01-31 at 13.52.24

    Then we want to create the necessary outlets for the cars and their top constraints. It is very important that the outlets to the constraints are correct, as these are the constraints that will be manipulated in the animation block.

    Screen Shot 2018-01-31 at 14.03.12

    Now we should be ready to start animating our outlets. I have created some constants within the UIViewController, to help simplify the animation code. You can do the same if you wish.

    Our animation code is going to sit entirely within our IBAction method. It’s a good idea to keep your IBActions as short as possible. However, because this is a simple demo I will make an exception. When we press the “Race” button, the top constraints for the cars will be modified to the position needed to help them cross the finish line. The cars will be animated for the same length of time and cover the same distance, but each in a different way according to their UIViewAnimationOptions.

    Modify your constraint constant and call layoutIfNeeded inside the animation block, this will use Auto-Layout to perform the animation. Note that it is best practice to call layoutIfNeeded function before the animation block as well to ensure that all pending layout operations have been completed.

    https://gist.github.com/rtking1993/a7b9b24cec94ee50cc4d8a2d6c68c617

    Here is the full source code for the project:

    https://github.com/rtking1993/UIViewAnimationOptions

  • Tutorial: Implementing UIInterpolating MotionEffect

    This is quite a neat effect that you can add to any of your views on your app. Essentially as you move your phone, the accelerometers inside predict the orientation changes of the phone and provide this movement to your view. It is a great trick to help bring attention to prominent views or images in your app. By creating a small extension on the UIView class you will be able to add this effect to any view with one line of code.

    Tutorial

    First of all, you want to set up a single view application template. Then create a fixed size UIView to contain your main UIImageView. It is important that the clip to bounds attribute for the UIView is set to true and the UIImageView is constrained to be larger than the UIView.

    Screen Shot 2018-01-28 at 8.17.12 AM

    Here is the extension that you will need to add to your project that you can use on any UIView or subclass of UIView.

    https://gist.github.com/rtking1993/e0692e99f0dde15b3cbb32eee0ef8ca0

    Then we want to connect our UIImageView to an outlet and implement the motion effect in the viewDidLoad method.

    https://gist.github.com/rtking1993/2e31ae78464b0aa2a8b54f26c9cda06c

    Adjusting the intensity will change the severity of the effect, reducing the intensity will make the effect less pronounced while increasing the intensity will raise the sensitivity. Be careful of setting the intensity too high, as it could leave your users feeling a bit nauseous!

    Here is the full source code for the project:

    https://github.com/rtking1993/UIImagePickerViewController