Yet another animated flat buttons in Swift

Related tags

Button DynamicButton
Overview

DynamicButton

Supported Platforms Version Carthage compatible Swift Package Manager compatible Build Status codecov.io codebeat badge

DynamicButton is a powerful flat design button written in Swift to display hamburger button like with animated transitions between style updates. It also allows you to create your own custom symbol / style buttons!

DynamicButton

RequirementsUsageInstallationContributionContactLicense

Requirements

  • iOS 8.0+ / tvOS 9.0+
  • Xcode 10.0+
  • Swift 5+

Usage

Basics

Here is how to create a button and setting its style:

import DynamicButton

let dynamicButton = DynamicButton(style: .hamburger)
// Equivalent to
// let dynamicButton   = DynamicButton()
// dynamicButton.style = .hamburger

// Animate the style update
dynamicButton.setStyle(.close, animated: true)

Customization

Button appearance and behavior can be customized using different properties:

let dynamicButton                 = DynamicButton()
dynamicButton.lineWidth           = 3
dynamicButton.strokeColor         = .black
dynamicButton.highlightStokeColor = .gray

Supported Symbol Styles

Here is the symbol list (DynamicButton.Style) already implemented by the library:

  • .arrowDown: downwards arrow
  • .arrowLeft: leftwards arrow
  • .arrowRight: rightwards arrow
  • .arrowUp: upwards arrow
  • .caretDown: down caret
  • .caretLeft: left caret
  • .caretRight: left caret
  • .caretUp: up caret:
  • .checkMark: check mark
  • .circleClose: close symbol surrounded by a circle
  • .circlePlus: plus symbol surrounded by a circle
  • .close: close symbol X
  • .dot: dot symbol .
  • .download: downwards triangle-headed arrow to bar
  • .fastForward: fast forward
  • .hamburger: hamburger button
  • .horizontalLine: horizontal line
  • .horizontalMoreOptions: horizontal more options
  • .none: no style
  • .pause: pause symbol
  • .play: play symbol
  • .plus: plus symbol +
  • .stop: stop symbol
  • .reload: reload symbol
  • .rewind: rewind
  • .verticalLine: vertical line |
  • .verticalMoreOptions: vertical more options
  • .location: location symbol

Custom symbols

To create your own symbols you have to create an object (or struct) that conforms to the DynamicButtonBuildableStyle protocol:

/// Diagonal line style: \
struct MyCustomLine: DynamicButtonBuildableStyle {
  let pathVector: DynamicButtonPathVector

  init(center: CGPoint, size: CGFloat, offset: CGPoint, lineWidth: CGFloat) {
    let r = size / 2
    let c = cos(CGFloat.pi * 0.3)
    let s = sin(CGFloat.pi * 0.3)

    let p1 = CGMutablePath()
    p1.move(to: CGPoint(x: center.x + r * c, y: center.y + r * s))
    p1.addLine(to: CGPoint(x: center.x - r * c, y: center.y - r * s))

    pathVector = DynamicButtonPathVector(p1: p1, p2: p1, p3: p1, p4: p1)
  }

  /// "MyCustomLine" style.
  static var styleName: String {
    return "MyCustomLine"
  }
}

myButton.style = .custom(MyCustomLine.self)

Note that a symbol can not have more than 4 paths.

And many more...

To go further, take a look at the example project.

Installation

CocoaPods

Install CocoaPods if not already available:

$ [sudo] gem install cocoapods
$ pod setup

Go to the directory of your Xcode project, and Create and Edit your Podfile and add DynamicButton:

$ cd /path/to/MyProject
$ touch Podfile
$ edit Podfile
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'

use_frameworks!
pod 'DynamicButton', '~> 6.2.1'

Install into your project:

$ pod install

Open your project in Xcode from the .xcworkspace file (not the usual project file):

$ open MyProject.xcworkspace

You can now import DynamicButton framework into your files.

Carthage

Carthage is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate DynamicButton into your Xcode project using Carthage, specify it in your Cartfile file:

github "yannickl/DynamicButton" >= 6.2.1

Swift Package Manager

You can use The Swift Package Manager to install DynamicButton by adding the proper description to your Package.swift file:

import PackageDescription

let package = Package(
    name: "YOUR_PROJECT_NAME",
    dependencies: [
        .package(url: "https://github.com/yannickl/DynamicButton.git", from: "6.2.1")
    ],
    // ...
)

Note that the Swift Package Manager is still in early design and development, for more information checkout its GitHub Page.

Manually

Download the project and copy the DynamicButton folder into your project to use it in.

Contribution

Contributions are welcomed and encouraged .

Contact

Yannick Loriot

License (MIT)

Copyright (c) 2015-present - Yannick Loriot

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
  • Unknown class DynamicButton in Interface Builder file

    Unknown class DynamicButton in Interface Builder file

    2018-12-20 11:33:59.954669-0600 PUBGStats[10256:112849] Unknown class DynamicButton in Interface Builder file. 2018-12-20 11:33:59.956261-0600 PUBGStats[10256:112849] +[UILabel removeAllAnimations]: unrecognized selector sent to class 0x113317ce0 2018-12-20 11:33:59.962764-0600 PUBGStats[10256:112849] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[UILabel removeAllAnimations]: unrecognized selector sent to class 0x113317ce0'

    opened by martin-one 6
  • SetTitle doesn't work?

    SetTitle doesn't work?

    Hi! I appreciate your work and I'm loving it! But I've been struggling to set title for a button, here's my code but it's not working.

        let confirm_button = DynamicButton()
    
        confirm_button.setTitle("Confirm", forState: .Normal)
    
        confirm_button.setTitleColor(UIColor.blackColor(), forState: .Normal)
    
        confirm_button.backgroundColor = UIColor.bluecolor()
    

    The above code executes perfectly but the button only shows background color but not the title. Is there a way to to so?

    opened by Jackson0111 6
  • Annoying animation with custom shapes

    Annoying animation with custom shapes

    annoying animation

    Here is the code for reload icon.

    class ReloadDynamicButton: DynamicButtonStyle {
        convenience required init(center: CGPoint, size: CGFloat, offset: CGPoint, lineWidth: CGFloat) {
        let curveBezierPath = UIBezierPath(arcCenter: center, radius: size / 3, startAngle: 0.0, endAngle:      CGFloat((3.0 * M_PI) / 2), clockwise: true)
        let path: CGPathRef = curveBezierPath.CGPath
    
        let curveEndPoint = curveBezierPath.currentPoint
    
        let thirdSize = size / 6
        let sixthSize = size / 12
    
        let a = CGPoint(x: center.x + sixthSize, y: center.y)
        let b = CGPoint(x: center.x - sixthSize, y: center.y + thirdSize)
        let c = CGPoint(x: center.x - sixthSize, y: center.y - thirdSize)
    
        let gravityCenter = CGPoint(x: (a.x + b.x + c.x) / 3, y: (a.y + b.y + c.y) / 3)
        let offsetFromCenter = CGPoint(x: curveEndPoint.x - gravityCenter.x, y: curveEndPoint.y - gravityCenter.y)
    
        let path1 = CGPathCreateMutable()
        CGPathMoveToPoint(path1, nil, offset.x + a.x, offsetFromCenter.y + a.y)
        CGPathAddLineToPoint(path1, nil, offset.x + b.x, offsetFromCenter.y + b.y)
    
        let path2 = CGPathCreateMutable()
        CGPathMoveToPoint(path1, nil, offset.x + a.x, offsetFromCenter.y + a.y)
        CGPathAddLineToPoint(path1, nil, offset.x + c.x, offsetFromCenter.y + c.y)
    
        self.init(path1: path, path2: path1, path3: path2, path4: path1)
    }
    }
    
    opened by mzeeshanid 3
  • Custom button symbols

    Custom button symbols

    It would be nice to be able to create custom buttons, perhaps with a function that can be fed CGPathRefs:

    let startCustomButton = DynamicButton(startLine1, startLine2, startLine3, startLine4)
    let endCustomButton = DynamicButton(endLine1, endLine2, endLine3, endLine4)
    
    startCustomButton.setStyle(endCustomButton.style, animated: true)
    

    ( maybe even use ASCIImage - https://github.com/cparnot/ASCIImage/issues/12 )

    enhancement 
    opened by cannyboy 3
  • IBInspectable Fixes

    IBInspectable Fixes

    I found that the ibinspectable enum that was there before wasn't working, so I updated the code to allow for an ibinspectable string that finds the right style and sets it in storyboard. In order to do so, I made a fromString static function on DynamicButton. I also made every description for each style lowerCamelCase

    I also found that the allStyles enum in DynamicButtonStyle.swift was missing the checkmark style, so I added that as well

    Changes: added ibinspectable ability to fill in styles, added checkmark style to allStyles (it was missing)

    opened by ksinghal 2
  • Swift package manager fails in Xcode 11

    Swift package manager fails in Xcode 11

    Installation of via SPM fails:

    The package dependency graph can not be resolved; unable find any available tag for the following requirements:
    
    https://github.com/yannickl/DynamicButton — 6.2.0..<7.0.0
    
    opened by timbms 1
  • DynamicButton.Style needs a hash(into:) method to conform to Hashable

    DynamicButton.Style needs a hash(into:) method to conform to Hashable

    The compiler's reporting the following problem:

    /Users/hkatz/DynamicButton/Sources/DynamicButtonStyle.swift:177:14: 'Hashable.hashValue' is deprecated as a protocol requirement; conform type 'DynamicButton.Style' to 'Hashable' by implementing 'hash(into:)' instead

    I think I could likely fix this and do a pull request on it, but I've never done that and don't have the time at the moment to figure it out. Soon tho! :-)

    opened by howardck 1
  • Hamburger icon with rounded tips

    Hamburger icon with rounded tips

    Hi!

    I'm creating a hamburger button using

    button = DynamicButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
    button.lineWidth = 4
    button.setStyle(.hamburger, animated: false)
    

    Now, the Hamburger has square tips,

    image

    but the Close icon has rounded tips

    image

    The documentation shows a hamburger icon with rounded tips, so I'm wondering. Am I doing something wrong? or is there something I can set in order to achieve that?

    thank you!

    opened by rpelorosso 1
  • DynamicButtonPathVector

    DynamicButtonPathVector

    Creating a custom DynamicButtonBuildableStyle in Swift 4 yields the following compile error:

    'DynamicButtonPathVector' initializer is inaccessible due to 'internal' protection level
    

    When trying to create a DynamicButtonPathVector(p1: CGMutablePath(), p2: CGMutablePath(), p3: CGMutablePath(), p4: CGMutablePath())

    I have a fix for this and I will send a PR for it.

    opened by franck-nadeau 1
  • PathHelper is unresolved

    PathHelper is unresolved

    Use of unresolved identifier 'PathHelper' when I'm trying to call PathHelper.line(atCenter: center, radius: size / 2, angle: PathHelper.F_PI_2) Xcode 8, Swift 3, 1.1.0.rc2

    opened by protspace 1
  • Animation not working if button is added on Interface Builder

    Animation not working if button is added on Interface Builder

    I added a button on interface builder. Set up click event. When changing the style, the animation is not working. The style does change, but without animation

    opened by csguzman 2
  • Calling layoutsubviews should not interrupt an existing transition

    Calling layoutsubviews should not interrupt an existing transition

    Currently, if you call layoutsubviews while the button is animating, setStyle(buttonStyle, animated: false) will be called and hence the animation will complete instantly.

    Since my button animates the view it is in, layoutsubviews is called several times during the transition and hence the button doesn't work.

    opened by nick-potts 3
  • Button Icon Size + Round Background

    Button Icon Size + Round Background

    @yannickl For the DynamicButtonStyle.arrowUp I'm trying to round it but i end up with the arrow at the top instead of center. how come? Could you make it possible to adjust icon size and properly align it in the center

    opened by otymartin 2
  • image is stretched out if set as background

    image is stretched out if set as background

    I'm trying to set an image as background, it works but the image is like stretched out - very ugly lol. Is there a way to fix that? or am I doing something wrong with the image?

    opened by Jackson0111 1
Owner
Yannick Loriot
iOS developer and nodejs addict. Open-source lover and technology enthusiast.
Yannick Loriot
Flat button with 9 different states using POP

VBFPopFlatButton Flat button with 21 different states and 2 types animated using pop. These are some examples of both types in different states: And h

Victor Baro 3.1k Nov 30, 2022
Flat design pressable button for iOS developers.

HTPressableButton HTPressableButton is designed for iOS developers to be able to spend time developing ideas, not building basic buttons. These stylis

Famolus 859 Dec 12, 2022
A fully customisable swift subclass on UIButton which allows you to create beautiful buttons without writing any line of code.

JSButton Demo $ pod try JSButton ...or clone this repo and build and run/test the JSButton project in Xcode to see JSButton in action. If you don't ha

Jogendra 12 May 9, 2022
A fully customisable subclass of the native UIControl which allows you to create beautiful buttons without writing any line of code.

A fully customisable subclass of the native UIControl which allows you to create beautiful buttons without writing any line of code. Preview You'll be

Lorenzo Greco 2.3k Dec 31, 2022
RadioGroup - The missing iOS radio buttons group.

RadioGroup The missing iOS radio buttons group. Usage let radioGroup = RadioGroup(titles: ["First Option Title", "Another Option Title", "Last"]) radi

Yonat Sharon 188 Dec 20, 2022
Lickable-Button We made the buttons on the screen look so good you'll want to lick them

Lickable-Button We made the buttons on the screen look so good you'll want to lick them. - Steve Jobs A little SwiftUI button project at WWDC 2021 Lic

Nate Thompson 14 Dec 29, 2021
Added functionality that system buttons are not easy to implement

LLCustomButton Added functionality that system buttons are not easy to implement What are the functions ? Sets the position and spacing of text and im

coder 18 Jul 28, 2022
Added functionality that system buttons are not easy to implement

LLCustomButton Added functionality that system buttons are not easy to implement What are the functions ? Sets the position and spacing of text and im

coder 18 Jul 28, 2022
Inspired by Apple’s download progress buttons in the app store

GBKUIButtonProgressView Inspired by Apple’s download progress buttons in the app store Created by @pklada and @miketsprague Checkout the blog post. In

Guidebook 541 Sep 1, 2022
An easy way to create beautiful social authentication buttons

EasySocialButton An easy way to create beautiful social authentication buttons Screenshots Installation Cocoa Pods pod 'EasySocialButton' Manual Drag

Antonio Zaitoun 162 Dec 11, 2022
LabelButtonKit is a mini library for labeled buttons (of SF Symbols), written entirely in SwiftUI

A simple Button with an SFSymbol (icon) as a label, vertically. As well as a Dynamic Type variant using it horizontally. Using SwiftUI. A List View of buttons is also available here. Have fun! ??

Marcos Morais 5 Nov 26, 2022
Cute Animated Button written in Swift.

DOFavoriteButton Cute Animated Button written in Swift. It could be just right for favorite buttons! Requirements iOS 7.0+ Swift 1.2 Installation Cart

Daiki Okumura 3.6k Dec 29, 2022
Animated Play and Pause Button written in Swift, using CALayer, CAKeyframeAnimation.

AnimatablePlayButton Animated Play and Pause Button written in Swift, using CALayer, CAKeyframeAnimation. features Only using CAShapeLayer, CAKeyframe

keishi suzuki 77 Jun 10, 2021
Interactive and fully animated Material Design button for iOS developers.

WYMaterialButton Inspired by Google Material Design, written purely in Swift 3. WYMaterialButton implemented Material Design on iOS and add more dynam

Yu Wang 76 Oct 7, 2022
A small and flexible (well documented) UIButton subclass with animated loading progress, and completion animation.

ButtonProgressBar-iOS Example For LIVE PREVIEW on Appetize in your browser itself, click here. To run the example project, clone the repo, and run pod

Pushkar Sharma 566 Dec 9, 2022
Customizable and easy to use expandable button in Swift.

ExpandableButton Requirements iOS 9.0+ Installation CocoaPods: Add the following line to your Podfile: pod 'ExpandableButton' #for swift less than 4.

Dmytro Mishchenko 98 Dec 5, 2022
iOS Pod for a Soft UI (Neumorphic) Button for UIKit written in Swift

iOS Pod for a Soft UI (Neumorphic) Button for UIKit written in Swift

Pallav Agarwal 21 Oct 23, 2022
Recreating the cool parallax icons from Apple TV as iOS UIButtons (in Swift).

TVButton Recreating the cool parallax icons from Apple TV as iOS UIButtons (in Swift). The effect is triggered by long pressing or dragging. Usage Imp

Roy Marmelstein 1.2k Dec 21, 2022
Simple and customizable button in Swift

SwiftyButton Maintainer(s): @nickm01 @pmacro @aryamansharda Simple and customizable button in Swift. Installation Cocoapods pod 'SwiftyButton' Cartha

Scoop 542 Dec 13, 2022