So far I am really enjoying the book. In an attempt to better understand the concepts in the book and make them stick in my head, I have been coding sample applications and plan on blogging on the subject. The first of these blog posts, based on a question which popped into my head, “what is the difference between animateWithDuration and transitionWithView?”
animateWithDuration(_:animations:) is a class method allowing you to animate views within it’s animations closure. Sounds more complex than it is:
Typically before the main view is displayed you set the view you want to animate into its starting state. In this case, we want to animate the title view onto the screen so within viewWillAppear() move the title view off the screen:
animateWithDuration allows you to animate the following properties of a UIView:
Changes you can make
Modify this property to change the view’s size and position relative to its superview’s coordinate system. (If the transform property does not contain the identity transform, modify the bounds or center properties instead.)
Modify this property to change the view’s size.
Modify this property to change the view’s position relative to its superview’s coordinate system.
Modify this property to scale, rotate, or translate the view relative to its center point. Transformations using this property are always performed in 2D space. (To perform 3D transformations, you must animate the view’s layer object using Core Animation.)
Modify this property to gradually change the transparency of the view.
Modify this property to change the view’s background color.
Modify this property to change the way the view’s contents are stretched to fill the available space.
animateWithDuration(_:animations:) animates the animatable properties of a UIView, whereas transitionWithView(_:duration:options:animations:completion:) animates the addition or removal of a view.
It is possible to animate the addition and removal of views using the animateWithDuration class methods but transitionWithView(:_duration:options:animations:completion:) allows you to apply pre-defined transitions to animate any set of changes to your view.
The method transitionWithView(_:duration:options:animations:completion:) takes an extra parameter view when compared to animateWithDuration(_delay:options:animations:completion:). The view passed in serves as a container for the transition animation.
iOS Animations by Tutorials provides a number of examples covering common actions such as: “adding a new view”, “removing a view”, “hiding/ showing a view”, and “replacing a view with another view”.
Predefined transition animation options
The transitionWithView(_:duration:options:animations:completion”) class method has a number of predefined transition animation options. I have listed these options below:
So in summary, animateWithDuration allows you to animate a UIView’s animatable properties. You can animate as many UIView’s animatable properties as you wish within the animateWithDuration’s animation closure method. transitionWithView on the other hand uses a containing view to animate the addition or removal of a view. These animations use predefined transitions which you set within the options parameter.
http://www.developerdave.co.uk/wp-content/uploads/2015/09/4605043024_84915c26d6_o.jpg15044752developerdavedeveloperdave2015-09-24 08:07:352016-06-24 13:36:30The difference between animateWithDuration and transitionWithView