Giter Site home page Giter Site logo

imswitch's Introduction

IMSwitch

Swift Version SPM Compatible License

lMSwitch glance

IMSwitch is a SwiftUI toggle component with image options. It provides a visually appealing and customizable way to toggle between two options.

An easy to use, customizable UISwitch which contains images. Build entirely in SwiftUI

Demo IMSwitch

Features

  • Smooth animations
  • Customizable appearance
  • Support for Right-to-Left (RTL) layout

Requirements

  • Swift 5.5+
  • iOS 15.0+

Installation

Swift Package Manager (SPM)

Add the following to your Package.swift file:

.package(url: "https://github.com/mohamed-arradi/IMSwitch.git", from: "1.0.0")

Usage

// Create an instance of ToggleState
let toggleState = ToggleState()

// Initialize IMSwitch with images and colors
let imSwitch = IMSwitch(
    leftImage: Image(systemName: "bicycle"),
    rightImage: Image(systemName: "parkingsign.circle"),
    activeColor: .white,
    circleFilledColor: .blue,
    backgroundColor: Color.white.opacity(0.85),
    animation: .easeInOut,
    animationLength: 0.35,
    toggleState: toggleState
)

Properties

  • leftImage: The image displayed when the toggle is in the left (inactive) state.
  • rightImage: The image displayed when the toggle is in the right (active) state.
  • activeColor: The color applied to the state image.
  • circleFilledColor: The color of the filling circle that moves along the toggle.
  • backgroundColor: The background color of the toggle.
  • animation: The type of animation used for state transitions.
  • animationLength: The duration of the animation in seconds.
  • isPrimaryOptionSelected: Represents the state of the primary option.
  • isRTL: Represents the Right-to-Left (RTL) layout direction.

Observing Changes

import SwiftUI

struct YourView: View {
    @StateObject var toggleState = ToggleState()

    var body: some View {
        IMSwitch(
            // ... other parameters ...
            toggleState: toggleState
        )
        .onReceive(toggleState.$isPrimaryOptionSelected) { newValue in
            // React to changes in isPrimaryOptionSelected
            print("isPrimaryOptionSelected changed to \(newValue)")
        }
    }
}

Example with RTL

IMSwitch(
    // ... other parameters ...
    toggleState: toggleState
)
.environment(\.layoutDirection, .rightToLeft)
.frame(width: 300, height: 90)

IMSwitch is available under the MIT license. See the LICENSE file for more info.

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.