Simple UIButton subclass with additional state change animations (e.g. backgroundColor) and missing features

Overview

SimpleButton

Carthage compatible Build Status Swift 5

UIButton subclass with animated, state-aware attributes. Easy to subclass and configure! Full API docs

Sample

Usage

Just create your own SimpleButton subclass and configure your button attributes by overriding configureButtonStyles.

class PrimaryButton: SimpleButton {
	override func configureButtonStyles() {
		super.configureButtonStyles()
    	setBorderWidth(4.0, for: .normal)
		setBackgroundColor(UIColor(red: 52/255, green: 73/255, blue: 94/255, alpha: 1.0), for: .normal)
		setBackgroundColor(UIColor(red: 44/255, green: 62/255, blue: 80/255, alpha: 1.0), for: .highlighted)
		setBorderColor(UIColor(red: 44/255, green: 62/255, blue: 80/255, alpha: 1.0), for: .normal)
		setScale(0.98, for: .highlighted)
		setTitleColor(UIColor.whiteColor(), for: .normal)
	}
}

For usage in Interfacebuilder, just use your SimpleButton subclass as custom class for any UIButton element. All defined styles gets applied automatically.

You can also configure your button without a subclass directly inline.

let awesomeButton = SimpleButton(type: .custom)
awesomeButton.setBorderWidth(2.0, for: .normal)
awesomeButton.setBorderColor(UIColor.redColor(), for: .highlighted)
view.addSubview(awesomeButton)

Note that you should use UIButtonType.custom to avoid undesired effects.

Please checkout the example project for a detailed usage demo.

@IBDesignable

Have a look on DesignableButton subclass within the Example Project for @IBDesignable usage.

Animation

Each state change of SimpleButton animates by default. Sometimes you need to define which state transition should animate and which should happen immediately. Therefore you can control that behaviour with the animated and animationDuration parameters.

setBorderWidth(4.0, for: .normal, animated: true, animationDuration: 0.2)
setBorderWidth(8.0, for: .highlighted, animated: false)

This means, every state change to .normal animates the borderWidth to 4.0. Every state change to .highlighted changes instantly the borderWidth to 8.0 without animation.

Loading state

SimpleButton has a custom loading state. You can toggle this state by setting simpleButton.isLoading. The button shows an UIActivityIndicator instead of the title when adding the loading state.

simpleButton.setCornerRadius(20, for: SimpleButtonControlState.loading)
simpleButton.isLoading = true

If you don´t like the default loading indicator, you can set your own UIView by doing

simpleButton.loadingView = CustomAwesomeLoadingView()

Please note, when using a custom loading view don´t forget to handle the position and initial add to subview by yourself to fit your needs.

Configurable attributes

Take a look at the Setter for state attributes section of the API Docs

Installation

Note that SimpleButton is written in swift 5 and may not be compatible with previous versions of swift.

Swift Package Manager (Recommended)

Add the following dependency to your Package.swift file:

.package(url: "https://github.com/allaboutapps/SimpleButton.git", from: "5.0.0")

Carthage

Add the following line to your Cartfile.

github "allaboutapps/SimpleButton" ~> 5.0

Then run carthage update.

Manually

Just drag and drop the SimpleButton.swift file into your project.

Contributing

  • Create something awesome, make the code better, add some functionality, whatever (this is the hardest part).
  • Fork it
  • Create new branch to make your changes
  • Commit all your changes to your branch
  • Submit a pull request
Comments
  • Attributed buttons support + swift 4

    Attributed buttons support + swift 4

    Our designers often want to indent between the letters in the buttons. So I added support for this functionality in your library: setAttributedText("TEXT FOR STATE", for: .normal) setAttributedTextColor(.white, for: .normal) setAttributedTextSpacing(1.2) setAttribute(NSAttributedStringKey, value: Any, for: .normal) You can change button text with setAttributedText("ANOTHER TEXT FOR STATE", for: .normal) and all applied attributes refresh automatically.

    opened by axmav 4
  • translatesAutoresizingMaskIntoConstraints = false break button's frame

    translatesAutoresizingMaskIntoConstraints = false break button's frame

    Hello, In non autolayout view button's position is set to zero and size is minimized, removing this line of code fixes issue

    Why do you have this? Can I break anything by removing it?

    opened by Igor-Palaguta 3
  • Please update version on Cocoapods

    Please update version on Cocoapods

    Cocoapods still references the old 2.1.1 version, rather than the latest (v3 as of writing this).

    It would be much appreciated if this could be updated. For now I'll just change my Podfile to point at the latest tag.

    https://cocoapods.org/?q=simplebutton

    P.S. Thank you for this excellent Pod :)

    opened by nmccann 2
  • Selected state shows an extra background box

    Selected state shows an extra background box

    I'm trying to enable a toggle feature through the selected property and using UIControlState.Selected for state. I get an extra blue layer in the background that I haven't been able to get rid off efficiently. Any tips?

    screen shot 2016-06-29 at 3 43 26 pm
    opened by simoami 2
  • SimpleButton doesn't call `configureButtonStyles` when used in InterfaceBuilder

    SimpleButton doesn't call `configureButtonStyles` when used in InterfaceBuilder

    The button doesn't appear as expected when subclassed and used with @IBInspectable variables to set up the values for configureButtonStyles like here:

    import UIKit
    import SimpleButton
    
    @IBDesignable
    
    class DesignableButton: SimpleButton {
    
            /// Background color for normal state.
        @IBInspectable var backgroundColorNormal: UIColor?
        @IBInspectable var backgroundColorHighlight: UIColor?
        @IBInspectable var titleColorNormal: UIColor?
        @IBInspectable var titleColorHighlighted: UIColor?
    
        @IBInspectable var shadow: Bool = false
        @IBInspectable var shadowColor: UIColor?
        @IBInspectable var shadowOffset: CGSize = CGSizeZero
        @IBInspectable var shadowRadius: CGFloat = 0
        @IBInspectable var shadowOpacity: Float = 0
    
        override func configureButtonStyles() {
            super.configureButtonStyles()
    
            if let backgroundColorNormal = backgroundColorNormal {
                setBackgroundColor(backgroundColorNormal, forState: .Normal)
            }
            if let backgroundColorHighlight = backgroundColorHighlight {
                setBackgroundColor(backgroundColorHighlight, forState: .Highlighted)
            }
            if let titleColorNormal = titleColorNormal {
                setTitleColor(titleColorNormal, forState: .Normal)
            }
            if let titleColorHighlighted = titleColorHighlighted {
                setTitleColor(titleColorHighlighted, forState: .Highlighted)
            }
    
            if shadow {
                if let shadowColor = shadowColor {
                    setShadowColor(shadowColor)
                }
                setShadowOffset(shadowOffset)
                setShadowRadius(shadowRadius)
                setShadowOpacity(shadowOpacity)
            }
        }
    
    
    }
    
    opened by gunterhager 0
  • Button doesn't use .Normal attributes in .Disabled state when they're not explicitly set

    Button doesn't use .Normal attributes in .Disabled state when they're not explicitly set

    You can reproduce this by adding the following line to the sample project:

            let awesomeButton = SimpleButton.buttonWithType(.Custom) as! SimpleButton
            awesomeButton.enabled = false //add this
    

    If you run the project with that line in place, you will see that the awesomButton is not visible. Its attributes are set properly, but it hasn't been updated visually.

    The problem is caused by the following check:

        if state == self.state
    

    When you call e.g. setBackgroundColor, and button's state is .Disabled, but the attributes are set for .Normal. Thus, the method changeBackgroundColorForStateChange is not called and the button's appearance is not updated.

    I wasn't sure if the above check is needed for performance reasons or if you can just get rid it.

    bug 
    opened by avf 0
  • Code review and feedback 👌

    Code review and feedback 👌

    • Adds default values for animated and state properties (default false)
    • Access control: make set... methods private
    • adds @IBDesignable and @IBInspectable attributes to be nicely displayed in Interface Builder
    • change configure() to configureButtonStyles(): configure() feels a bit too generic to me, I think configureButtonStyles() will be clearer to other developers in the future.
    • Updated the Readme (wordings, new features like @IBDesignable)
    • Updated some project settings and added the SimpleButtonTests target (tests still need to be written)
    opened by aschuch 0
  • Update Podspec.

    Update Podspec.

    Summary

    The current Podspec is outdated, not reflecting the latest changes in the repository, such as the migration to Swift 5 and the version bump to 5.0.0.

    With this pull request, the .podspec will be up to date.

    Linter

    ❯ pod spec lint --allow-warnings
    
     -> SimpleButton (5.0.0)
        - WARN  | source: The version should be included in the Git tag.
        - NOTE  | xcodebuild:  note: Using new build system
        - NOTE  | xcodebuild:  note: Building targets in parallel
        - NOTE  | xcodebuild:  note: Using codesigning identity override: -
        - NOTE  | xcodebuild:  note: Build preparation complete
        - NOTE  | [iOS] xcodebuild:  note: Planning build
        - NOTE  | [iOS] xcodebuild:  note: Analyzing workspace
        - NOTE  | [iOS] xcodebuild:  note: Constructing build description
        - NOTE  | [iOS] xcodebuild:  warning: Capabilities for Signing & Capabilities may not function correctly because its entitlements use a placeholder team ID. To resolve this, select a development team in the App editor. (in target 'App' from project 'App')
        - NOTE  | [iOS] xcodebuild:  warning: Skipping code signing because the target does not have an Info.plist file and one is not being generated automatically. (in target 'App' from project 'App')
    
    Analyzed 1 podspec.
    
    SimpleButton.podspec passed validation.
    

    Closes: #28

    opened by renato-anchor 0
  • Please update version on Cocoapods

    Please update version on Cocoapods

    Cocoapods(https://cocoapods.org/pods/SimpleButton) still references the old 4.0.1 version, rather than the latest (5.0).

    It would be much appreciated if this could be updated.

    Thank you so much!

    opened by anchor-cassandra 0
  • "Vary For Trait"-aware?

    At present, the control completely blows VaryForTrait away. I think it is moderately easy to enable, but will have to review the Apple vids to confirm. This makes the corner radius attribute useless for a properly proportioned multi-device app.

    If time permits, i will see if I can make it aware, then post back here, else (due to time constraints) will just tweak my deliverable to miss the mark as minimally as i can.

    opened by wdcurry 2
Releases(5.0)
Owner
Andreas Tinoco Lobo
Andreas Tinoco Lobo
A Swift library to take the power of UIView.animateWithDuration(_:, animations:...) to a whole new level - layers, springs, chain-able animations and mixing view and layer animations together!

ver 2.0 NB! Breaking changes in 2.0 - due to a lot of requests EasyAnimation does NOT automatically install itself when imported. You need to enable i

Marin Todorov 3k Dec 27, 2022
A collection of animations for iOS. Simple, just add water animations.

DCAnimationKit A collection of animations for iOS Simply, just add water! DCAnimationKit is a category on UIView to make animations easy to perform. E

Dalton 797 Sep 23, 2022
Build complex, conducted animations declaratively without manually managing state.

Maestro Build complex, conducted animations declaratively without manually managing state. Code struct AnimatedPieChart: View { private enum Trim

Ryan Wachowski 5 Nov 20, 2022
A UICollectionViewLayout subclass that adds custom transitions/animations to the UICollectionView without effecting your existing code.

AnimatedCollectionViewLayout Normally a UICollectionView has no transition effects when you scroll from one item to another. There are lots of ways to

Jin Wang 4.5k Jan 1, 2023
(Animate CSS) animations for iOS. An easy to use library of iOS animations. As easy to use as an easy thing.

wobbly See Wobbly in action (examples) Add a drop of honey ?? to your project wobbly has a bunch of cool, fun, and easy to use iOS animations for you

Sagaya Abdulhafeez 150 Dec 23, 2021
(Animate CSS) animations for iOS. An easy to use library of iOS animations. As easy to use as an easy thing.

wobbly See Wobbly in action (examples) Add a drop of honey ?? to your project wobbly has a bunch of cool, fun, and easy to use iOS animations for you

Sagaya Abdulhafeez 150 Dec 23, 2021
Pure SwiftUI state-driven library to present view sequences and hierarchies.

PathPresenter swiftUIOnboarding.mp4 Pure SwiftUI routing with transitions, animations, and .sheet() support. In SwiftUI, View is a function of the sta

Aleksandr Dremov 21 Nov 15, 2022
UIImageView subclass that allows you to display a looped video and dynamically switch it.

AKVideoImageView Motivation AKVideoImageView was created because I wasn't satisfied with the standard AVPlayer when I was implementing a video backgro

Oleksandr Kirichenko 125 Apr 5, 2022
DynamicBlurView is a dynamic and high performance UIView subclass for Blur.

DynamicBlurView DynamicBlurView is a dynamic and high performance UIView subclass for Blur. Appetize's Demo Since using the CADisplayLink, it is a hig

Kyohei Ito 929 Jan 5, 2023
Advanced Natural Motion Animations, Simple Blocks Based Syntax

FlightAnimator Moved to Swift 3.1 Support: For Swift 3.1 - Use tag Version 0.9.9 See Installation Instructions for clarification Introduction FlightAn

Anton 589 Dec 29, 2022
DaisyChain is a micro framework which makes UIView animations chaining dead simple.

DaisyChain DaisyChain is a micro framework which makes UIView animations chaining dead simple. It uses the exact same interface you are familiars with

Ali Karagoz 31 Nov 3, 2022
Designed for gesture-driven animations. Fast, simple, & extensible!

Yet Another Animation Library Designed for gesture-driven animations. Fast, simple, & extensible! It is written in pure swift 3.1 with protocol orient

Luke Zhao 509 Dec 21, 2022
A concept to more easily define simple keyframe / multi-step animations in SwiftUI

?? Animate A concept to more easily define simple keyframe / multi-step animations in SwiftUI, without: Defining an @State value for each property to

Seb Jachec 3 Oct 18, 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
Easy to read and write chainable animations in Objective-C and Swift

Whats new in version 3.x? Swiftier syntax Swift 4 support Bug fixes and improvements Whats new in version 2.x? Re-architected from the ground up, no m

Jeff Hurray 3.2k Dec 30, 2022
⛓ Easy to Read and Write Multi-chain Animations Lib in Objective-C and Swift.

中文介绍 This project is inspired by JHChainableAnimations! Why Choose LSAnimator & CoreAnimator? You can write complex and easy-to-maintain animations in

木子 1.6k Nov 22, 2022
Physics-based animations for iOS, tvOS, and macOS.

Advance An animation library for iOS, tvOS, and macOS that uses physics-based animations (including springs) to power interactions that move and respo

Tim Donnelly 4.5k Dec 29, 2022
A library of custom iOS View Controller Animations and Interactions.

RZTransitions is a library to help make iOS7 custom View Controller transitions slick and simple. Installation CocoaPods (Recommended) Add the followi

Rightpoint 1.9k Nov 20, 2022
Match animations in SwiftUI and UIKit/AppKit

MatchedAnimation Match animations in SwiftUI and UIKit/AppKit. /// Draw a box in

Ryan Carver 6 Dec 21, 2022