Fundamentos de SwiftUI y desarrollo de Interfaces
Desarrolla Apps Incre铆bles: primeros pasos con SwiftUI
Creaci贸n de views y uso de preview
Personalizaci贸n de views con modifiers y uso de shapes
Image render
Composici贸n de Layouts con Stacks
ScrollView y carousel
List y ForEach
Estructurando nuestra app
Quiz: Fundamentos de SwiftUI y desarrollo de Interfaces
Interacci贸n del Usuario y Componentes de Entrada
Button y manejo de estado con @State
Text Field y Text Editor
Picker y DatePicker
Toggle y Slider
Formularios
Alertas y Modales
Gestures
Animaciones y uso del ProgressView
Quiz: Interacci贸n del Usuario y Componentes de Entrada
Manejo del Estado y Arquitectura con ViewModels
ViewModels y @ObservedObject
Compartir Datos con @EnvironmentObject
Creaci贸n de Componentes Reutilizables con @Binding
Quiz: Manejo del Estado y Arquitectura con ViewModels
Navegaci贸n Avanzada
Navegaci贸n con NavigationStack
Personalizaci贸n de la Barra de Navegaci贸n
Quiz: Navegaci贸n Avanzada
Lanzamiento de la app
Funcionalidades extra
Lanzamiento en simulador
You don't have access to this class
Keep learning! Join and start boosting your career
Let's start by improving our application by adding favorites functionality. This is a crucial step to offer a more personalized experience to users. Let's modify our code so that cards can be marked as favorites. We will find the heart icon code in our Cards
views and adjust it to make it more maintainable. It is essential to create a specific function that manages these actions, so we will optimize our project.
By implementing reusable functions, we facilitate future maintenance tasks in the code. To achieve this, we must follow the following steps:
Create a function: we copy the code of the heart icon and convert it into a function called favoriteButton
. This function will return a view containing the heart icon logic.
func favoriteButton() -> some View { // Favorite button code }
Modify images: in our card views, replace the heart image with the new favoriteButton
function, making sure it depends on the isFavorite
state of the object.
Image(systemName: card.isFavorite ? "heart.fill" : "heart")
Add gestures: to interact with the icon, we introduce a tap gesture that executes the change of state of the favorite.
.onTapGesture { // Code to change the state of favorite }
As the favorite change must be reflected in the whole application, it must be configured from the AppInfo
:
Define a closure: we add a closure called onToggleFavorite
to the top of the view to handle the state change and declare it as optional to avoid errors.
Add function in AppInfo
: we need a function called toggleFavorite
that will change the state of isFavorite
.
func toggleFavorite(for card: Card) { if let index = cards.firstIndex(of: card) { cards[index].isFavorite.toggle() } }
Call the function from the views: in the list of card views, we assign the closure to trigger the state change.
info.toggleFavorite(for: card)
Although our application has almost all the functionalities ready, a key visual aspect is the icon. A good design can make the difference and make our application stand out.
Locate the Assets: first, we must access the assets folder of our project, where we will find the option for the app icon.
Configure the icon: we change the Appearances
option to None
to offer more simplicity and then we drag our design to this space.
Designing an icon can be an exciting creative challenge, and if you are looking to improve your design skills, remember that the Graphic Design School at Platzi offers many useful resources. Always remember that every detail counts in the world of app development, so make your app stand out!
Contributions 0
Questions 0
Want to see more contributions, questions and answers from the community?