How To Stop Repeating Animation With Button Pressed Swift
How To Create a Loading Button in Swift
Build piece of cake and reusable custom loading views
Every time the user interacts with our app, that requires u.s.a. to exercise some work (e.thousand. calling an endpoint or fetching data from the backend). We must permit them know that the app is doing something. Nosotros should never put the user in a position where they don't know what is currently happening.
This is why loading animations are and then important. They give the user a hint that something is happening in the app at that moment.
I of the most mutual scenarios is the sign-in button. When the user enters their credentials and taps the push, you lot probably must interact with the backend to log the user into the app. That procedure can have a couple of seconds or it could fail due to network problems. It's a good practice to testify the user some kind of loading within the push.
That is what this article is almost. I want to show you how you can make your ain loading push with a few lines of code using some Core Blitheness fundamentals.
You can encounter that the loading consists of three circles that scale upward and then return to their original position. Then allow's become ahead and create a custom UIView form from scratch that contains the dots to animate.
We can create the dots by creating three custom UIViews and setting their corner radius by one-half of their width:
The next thing to do is to configure the animations for the dots, and this is when Core Animation comes into action. Recollect nigh what the animation actually does: It scales in and then returns to its original size. And this is repeated while the animation is running.
We can achieve this past animating the views recursively with the UIview.animate… API, only I e'er endeavour to avert recursive functions unless they're really necessary. With Core Blitheness, we can set this animation pretty easily.
Showtime, let's ascertain three different CABasicAnimation properties (i for each dot) and so do some initial setup:
The almost important backdrop of the setup that you should take a closer look at are:
-
autoreverses: With this gear up totruthful, we're telling the animation to return to its original size once it's completed. -
toValue: This is the scale factor that we want to reach with the blitheness. -
repeatCount: Since we want to repeat the animation until it stops, we must set the echo count to infinity.
Now, in guild to start the blitheness, nosotros need to add our CABasicAnimations to the dots' layer. To go on things separate, let'due south create two functions: startAnimation() and stopAnimation().
We have everything in place to add our DotsAnimationView inside our ViewController's view and call the startAnimation() to run across what happens.
The animations start right later on we add them to the layer, so all of them start right at the same fourth dimension. Luckily for u.s.a., nosotros have the beginTime holding to set the starting indicate of our animations. Setting this property tin delay the animations to make a sequence consequence.
Note: Depending on the animation duration, the delay for the animations may change, merely yous can play effectually with some values until it fits your needs.
We only need to create a custom push and add the DotsAnimationView that we just created:
And that's it! We only need to call the startAnimation() and stopAnimation() functions of our button.
The cool thing about getting the blitheness in a separate view is that you tin use information technology inside a button or whatever other view every bit well. For case, you lot can use it while fetching some data from the backend to show in a collection view.
Conclusion
I hope this helps yous as a starting bespeak. Y'all can get every bit creative every bit you want. Perhaps your loading doesn't include the scale transformations but a translation or opacity instead. Whatever you want to practise, you lot can exercise information technology yourself. Just proceed the separation and reusable principles that we use hither in mind and so you can apply your loading animations non only inside your buttons.
If you take any questions, feel free to drop a comment. I'd be happy to help. Y'all can bank check the full code on GitHub.
Source: https://betterprogramming.pub/loading-button-in-swift-566f9ff0d98a
Posted by: trappawkwast86.blogspot.com

0 Response to "How To Stop Repeating Animation With Button Pressed Swift"
Post a Comment