A UISwitch that infects its superview with its tint color.

Related tags

Switch ViralSwitch
Overview

Build Status CocoaPods Coverage Status Carthage compatible

UISwitch subclass that 'infects' the parent view with the onTintColor when the switch is turned on. Inspired by this Dribble by Ramotion.

Screenshot

ViralSwitch

Installation

Using cocoapods:

pod 'AMViralSwitch'

Using Carthage:

github "andreamazz/ViralSwitch"

Usage

AMViralSwitch is a drop-in replacement of UISwitch:

  • Use AMViralSwitch instead of UISwitch
  • Set the onTintColor property of the switch

The switch will automatically infect its superview with the selected color.

Animation duration

Use animationDuration property to control the animation's speed:

self.toggle.animationDuration = 1.0

Animation completion

You can set a completion block for both on and off animations:

self.toggle.completionOn = {
  print("Enabled")
}

self.toggle.completionOff = {
  print("Disabled")
}

Animate views

You can animate other views alongside the switch infection. Typically you'll want to change color to views or labels that are inside the same superview. You can animate CoreAnimation properties likes this:

Swift

toggle.animationElementsOn = [
    [
        AMElementView: self.greenView.layer,
        AMElementKeyPath: "backgroundColor",
        AMElementFromValue: UIColor.blackColor().CGColor,
        AMElementToValue: UIColor.whiteColor().CGColor
    ]
]

Objective-C

self.toggle.animationElementsOn = @[
   @[ AMElementView: self.greenView.layer,
      AMElementKeyPath: @"backgroundColor",
      AMElementFromValue: (id)[UIColor clearColor].CGColor,
      AMElementToValue: (id)[UIColor whiteColor].CGColor }
];

To animate the textColor of an UILabel the syntax is slightly different:

Swift

toggle.animationElementsOn = [
    [
        AMElementView: label,
        AMElementKeyPath: "textColor",
        AMElementFromValue: UIColor.blackColor(),
        AMElementToValue: UIColor.whiteColor()
    ]
]

Objective-C

self.blueSwitch.animationElementsOn = @[
    @{ AMElementView: self.blueLabel,
       AMElementKeyPath: @"textColor",
       AMElementToValue: [UIColor whiteColor] }
];

Follow the same principle to animate the tintColor of your UIButtons:

Swift

toggle.animationElementsOn = [
    [
        AMElementView: self.infoButton,
        AMElementKeyPath: "tintColor",
        AMElementToValue: UIColor.whiteColor()
    ]
]

Objective-C

self.blueSwitch.animationElementsOn = @[
    @{ AMElementView: self.infoButton,
       AMElementKeyPath: @"tintColor",
       AMElementToValue: [UIColor whiteColor] }
];

Author

Andrea Mazzini. I'm available for freelance work, feel free to contact me.

Want to support the development of these free libraries? Buy me a coffee ☕️ via Paypal.

Contributors

Thanks to everyone kind enough to submit a pull request.

MIT License

Copyright (c) 2017 Andrea Mazzini. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Comments
  • Crash on deallocate

    Crash on deallocate

    AMViralSwitch adds observer for "on" keypath in "didMoveToSuperview" delegate method, which is called twice (on adding to superview and on removing from one), but view removes observer one time in dealloc method.

    opened by alex566 10
  • Does not work on ios 9

    Does not work on ios 9

    Im Tryed to use it with ios 9.2 and it is not working. the error i getting:

    Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UISwitch setAnimationDuration:]: unrecognized selector sent to instance 0x7feb08f1a510

    opened by diretix 6
  • Feature Request - Label color changes

    Feature Request - Label color changes

    I love the way you can add additional animations for surrounding views, however I have a view that would look better if the label text changed color when the background color changes.

    I've tried using the current animationElements, however this only seems to work with a layer which doesn't seem to want to change text color.

    enhancement 
    opened by ddaddy 4
  • Added in code doesn't work

    Added in code doesn't work

    let s = AMViralSwitch()
            s.frame = CGRect(x: 20, y: 150, width: 200, height: 30)
            s.onTintColor = UIColor.blueColor()
            self.view.addSubview(s)
    

    doesn't infect superview

    opened by zdnk 3
  • iOS 7 support

    iOS 7 support

    This is great, thanks. I don't see any reason why this shouldn't work on iOS 7, however I get this popping up yet it still runs [NSConcreteValue doubleValue]: unrecognized selector sent to instance

    If I add an exception break point, the app stops on this line self.shape.transform = CATransform3DMakeScale(1.0, 1.0, 1.0);

    Any idea how to fix this for iOS 7?

    Thanks

    opened by ddaddy 3
  • Fix broken headings in Markdown files

    Fix broken headings in Markdown files

    GitHub changed the way Markdown headings are parsed, so this change fixes it.

    See bryant1410/readmesfix for more information.

    Tackles bryant1410/readmesfix#1

    opened by bryant1410 2
  • disappearing border while scrolling table

    disappearing border while scrolling table

    2016-05-31 14 53 51 Hello. I've found the following bug. I use ViralSwitch in my UITable and have some cells for more than one screen. When I switched on the top switcher and scrolled table to bottom and again to top - border of switcher disappeared. I've fix this by changing one string of code: AMViralSwitch.m:- (void)switchChanged: Line 80 old: self.layer.borderWidth = 0; new: self.layer.borderWidth = 1; And result after scrolling: 2016-05-31 15 00 18

    I don't know the reason of setting border to 0, but changing this code will fix bug that I've found

    opened by lutc 1
  • Correct the spelling of CocoaPods in README

    Correct the spelling of CocoaPods in README

    This pull requests corrects the spelling of CocoaPods 🤓 https://github.com/CocoaPods/shared_resources/tree/master/media

    opened by ReadmeCritic 0
  • Fix test suite

    Fix test suite

    Two main issues:

    • The starting screen is showing the background layer, as if the initial transform was not applied.
    • The second screen is not showing the switch's border.
    opened by andreamazz 0
Releases(1.0.1)
Owner
Andrea Mazzini
💻 Software Engineer 🌲 Woodworker
Andrea Mazzini
iOS7 style drop in replacement for UISwitch

SevenSwitch iOS7 style drop in replacement for UISwitch Usage Cocoapods pod 'SevenSwitch', '~> 2.1' Swift support was added in version 2.0. If your p

Ben Vogelzang 779 Sep 5, 2022
Nicely animated flat design switch alternative to UISwitch

AIFlatSwitch A smooth, nice looking and IBDesignable flat design switch for iOS. Can be used instead of UISwitch. Inspired by Creativedash's Dribbble

null 963 Jan 5, 2023
UISwitch which paints over the parent view with the color in Swift.

AnimatedSwitch Swift subclass of the UISwitch which paints over the parent view with the color if switch is turned on and returns original superview b

Alex Sergeev 248 Jul 26, 2022
System Color Picker - The macOS color picker as an app with more features

System Color Picker The macOS color picker as an app with more features Download Requires macOS 11 or later. Features Quickly copy, paste, and convert

Sindre Sorhus 758 Dec 24, 2022
The purpose of the color game is to find the right color without getting distracted. It is as difficult as it is fun.

Color Game The purpose of the color game is to find the right color without getting distracted. It is as difficult as it is fun. Getting Started Insta

MDS 7 Dec 13, 2022
The purpose of the color game is to find the right color without getting distracted. It is as difficult as it is fun.

Color Game The purpose of the color game is to find the right color without getting distracted. It is as difficult as it is fun. Getting Started Insta

null 7 Dec 13, 2022
Simple utility to change macOS Big Sur menu bar color by appending a solid color or gradient rectangle to a wallpaper image

Change menu bar color in macOS Big Sur Simple utility to change macOS Big Sur menu bar color by appending a solid color or gradient rectangle to a wal

Igor Kulman 876 Jan 5, 2023
ColorAssetsFinder can help you find particular color assets easily by hex color code.

ColorAssetsFinder ColorAssetsFinder can help you find particular color assets easily by hex color code. Features Find particular color assets in Asset

Phil Chang 2 May 11, 2022
A basic iOS app that takes input from the user, displays it, allows changing both text color and background color.

Hello-iOSApp App Description A basic iOS app that takes input from the user, displays it, allows changing both text color and background color. App Wa

null 0 Jan 8, 2022
ColorWheel Test - An attempt at creating a Color Wheel to be utilized for color picking in SwiftUI utlizing various tutuorials on youtube

This code focuses on creating a Color Wheel in which a user will be able to select a desired color to then be implemented in another project that will display that color in an LED connected to an arduino

Gerardo Cerpa 0 Jan 15, 2022
Make system color defined in UIColor/NSColor available to SwiftUI Color.

SwiftUIColor Make system color defined in UIColor/NSColor available to SwiftUI Color. About In this library, colors are defined in an asset file. Crea

null 2 Oct 24, 2022
An easy to use, customizable replacement for UISegmentedControl & UISwitch.

BetterSegmentedControl BetterSegmentedControl is an easy to use, customizable replacement for UISegmentedControl and UISwitch written in Swift. Featur

George Marmaridis 2k Dec 30, 2022
iOS7 style drop in replacement for UISwitch

SevenSwitch iOS7 style drop in replacement for UISwitch Usage Cocoapods pod 'SevenSwitch', '~> 2.1' Swift support was added in version 2.0. If your p

Ben Vogelzang 779 Sep 5, 2022
Nicely animated flat design switch alternative to UISwitch

AIFlatSwitch A smooth, nice looking and IBDesignable flat design switch for iOS. Can be used instead of UISwitch. Inspired by Creativedash's Dribbble

null 963 Jan 5, 2023
A rapid prototype of UISwitch built with Swift and PaintCode.

LTJelloSwitch A cute UISwitch prototype built in about 8 hours. One of my co-worker gave me this concept last year. It was written in Objective-C and

Lex Tang 366 Aug 5, 2022
UIView subclass that bends its edges when its position changes.

AHKBendableView BendableView is a UIView subclass that bends its edges when its position change is animated. Internally, BendableView contains CAShape

Arek Holko 591 Jul 24, 2022
An Integer type that clamps its value to its minimum and maximum instead of over- or underflowing.

ClampedInteger An Integer type that clamps its value to its minimum and maximum instead of over- or underflowing. Examples let big = ClampedIntege

Berik Visschers 0 Jan 17, 2022
ChromaColorPicker - An intuitive HSB color picker built in Swift

An intuitive HSB color picker built in Swift. Supports multiple selection handles and is customizable to your needs.

Jonathan Cardasis 536 Dec 29, 2022
SheetyColors is an action sheet styled color picker for iOS

?? Based on UIAlertController: The SheetyColors API is based on UIKit's UIAlertController. Simply add buttons to it as you would for any other Action Sheet by defining UIAlertAction instances. Therefore, it nicely integrates with the look & feel of all other native system dialogs. However, you can also chose to use the color picker it self without an action sheet.

Christoph Wendt 102 Nov 10, 2022