Auto Layout is very powerful tool to control every view layouts in iOS. In case it is not used directly, the OS will auto transforms the view’s properties like the frame into constraints before laying out the views.
Auto Layout and Animation
Auto Layout is recommended for all the projects as due to its visual nature it can be easily adjusted when working on a view especially in one that you haven’t create. It is though a bit rigid and animations are very important in iOS so we need to figure out different ways to implement different types of animations while using the Auto Layout.
Animating with Constraints
By using constraints we can smartly implement animations with Auto Layout. It is possible to modify the constraints of the view be it by changing the constant property, by creating or deleting constraints or even by calling layoutIfNeeded() within the animation block as below:
// Constraints can be either connected through Interface Builder as outlets
// or created at runtime
var heightConstraint: NSLayoutConstraint!
…
heightConstraint.constant = 100
UIView.animate(withDuration: 0.25) {
self.view.layoutIfNeeded()
}
The more advanced the animation get the more constraints you need to keep track and manage. Consider you have to animate the red box to a different location where every position is part of the layout and affects other views. You will require four constraints to lay out a view means you now have eight constraints to be tracked four for every position.
Animating with Transform
Implementation the transform property of a UIView is an excellent way to easily create simple animations that won’t modify the layout of the screen. CGAffineTransforms support translation, rotation, scale, as well as any combination of them. The best case for transform animations are with views that never change position but do have an animation. If you are unable to visualise it, imagine a view that won’t change its position once it’s in the place yet can slide in from the top when the view first appears.
In order to animate the transition use the transform to translate the view off the screen before it appears and on the screen once it appears.
func viewWillAppear(_ animated: Bool){
…
contentView.transform = CGAffineTransform(translationX: 0, y: -contentView.frame.height)
}
func viewDidAppear(_ animated: Bool) {
…
UIView.animate(withDuration: 0.25) {
contentView.transform = .identity
}
}
Other properties like the alpha or scale and rotate the view can also be mixed. There are multiple other use cases for this type of animation like wiggling icons, make the button bounce on tapping or shaking a text field if its contents unable to pass the required validation.
In this animation there are three animated elements that are present in both compact and expanded states the playback button, the artwork, and the playback bars. In these elements the playback button and the image view play major role in every layout. If we tried to use constraints above mentioned to perform this transition it will be required to keep track of dozens of constraints. Still you want to traverse that route then we would lose the ability to have the transition follow the user input.
Animation implemented is easier than it seems. We created a container view first in our controller for every state. Each container has a static layout with all elements that couldn’t be animated. In each container invisible views were created that represented the frame of all animated element in that state. These views further helped in creation of the layout like the element was there. Now we included the actual elements to our main view. These views were constraints free as we have set their frame manually. With all the views in place, we connected it all with three IBOutlets for each element: compactReferenceView, expandedReferenceView, and elementView.
Image credit: savvyapps.com
Hopefully, above information will be helpful in gaining information regarding How do Advanced iOS Animations through Auto Layout. Looking forward to listen to your views and query regarding the same.
A user-oriented solution on cutting edge technology to engage customers or boost your brand to eventually edge out your competitor and realize the potential and importance of latest online business solution in various domain to ensure stability and rocket sky the ROI. Singsys boasts best-fit developers, designers that were key to partner with multiple Fortune 500 firms to deliver industry oriented web, mobile and e-commerce solution always.
You may be interested in following:
- iOS Application Battery Optimization Techniques to Minimize Burden on Battery
- Apple Developer Account Creation Step by Step
Related Posts...
iOS
Jun 27th, 2024
On National Doctor’s Day, let’s appreciate the advancements in healthcare, especially the rise of telemedicine. This innovation has revolutionized medical care, making it more convenient and effective. India’s eSanjeevani platform stands out, providing equitable healthcare to all, especially in rural areas. Launched by the Ministry of Health and Family Welfare, eSanjeevani facilitates remote consultations and enhances the quality of care. As telemedicine continues to evolve, driven by technological advancements, it promises to play a crucial role in the future of healthcare, ensuring accessibility and efficiency for everyone.
Read more
May 7th, 2024
Have you ever considered how convenient it would be to order groceries online and deliver them to your doorstep? This has become more popular, especially after the COVID-19 pandemic. People […]
Read more
Apr 30th, 2024
Election Evolution: The Rise of Political Campaign Management App Development – From lawn signs to social media, campaign app development marks a new era in election strategy. This blog explores the evolution of campaign tech.
Read more