Coder Social home page Coder Social logo

multistatebutton's Introduction

banner image

MultiStateButton

Build

Button elements that encompass multi-state semantics are quite common on iOS. We can find many examples of such just from some of Apple's first party apps on iOS, such as buttons that manage download of podcast episodes and albums in Podcast and Music, respectively.

Here we present an implementation of such button element tailored for SwiftUI, with customizability designed in mind.

Here's a demo of a scenario that simulates a download button in action.

banner image

Usage

There are three things needed to implement a multi-state button.

  1. Define all possible states of the semantics, as well as a view model for the button.
  2. Define an appearance for each possible semantics state.
  3. Define the element in your SwiftUI view.

Define all possible states of the semantics, as well as a view model for the button.

The first step is to define a view model that conforms to the MultiStateButtonViewModelProtocol protocol (defined here).

One of the associated types of the protocol is the State type, which needs to encompass all possible states of the semantics of the button.

Define an appearance for each possible semantics state.

Each state needs to map to a type that conforms to PrimitiveButtonStyle (see here), which is defined in SwiftUI that describes a type that applies custom interaction behavior and a custom appearance to the button.

There are many predefined button styles already defined in SwiftUI. It is your choice to either use one of the predefined button styles or define your own.

Define the element in your SwiftUI view.

Once you have all the states mapped, the view model defined and all button styles defined, the last step is to simply plugging those into MultiStateButton (defined here) and have it defined in your SwiftUI view that consumes the button.

Check out the demo app to see an example of how to define and customize such a multi-state button.

License

This project is licensed under the MIT License.


Copyright 2024 Will Li.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.