├── AnimatedRoundButtonExample ├── Assets.xcassets │ ├── Contents.json │ ├── like.imageset │ │ ├── like.png │ │ └── Contents.json │ └── AppIcon.appiconset │ │ └── Contents.json ├── Demo.gif ├── MenuVC.swift ├── Info.plist ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard └── AppDelegate.swift ├── AnimatedRoundButton ├── AnimatedRoundButton.h ├── Info.plist └── AnimatedRoundButton.swift ├── LICENSE ├── .gitignore └── README.md /AnimatedRoundButtonExample/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /AnimatedRoundButtonExample/Demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MagicLab-team/AnimatedRoundButton/HEAD/AnimatedRoundButtonExample/Demo.gif -------------------------------------------------------------------------------- /AnimatedRoundButtonExample/Assets.xcassets/like.imageset/like.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MagicLab-team/AnimatedRoundButton/HEAD/AnimatedRoundButtonExample/Assets.xcassets/like.imageset/like.png -------------------------------------------------------------------------------- /AnimatedRoundButtonExample/Assets.xcassets/like.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "scale" : "1x" 6 | }, 7 | { 8 | "idiom" : "universal", 9 | "filename" : "like.png", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /AnimatedRoundButton/AnimatedRoundButton.h: -------------------------------------------------------------------------------- 1 | // 2 | // AnimatedRoundButton.h 3 | // AnimatedRoundButton 4 | // 5 | // Created by Roman Sorochak on 6/28/17. 6 | // Copyright © 2017 MagicLab. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | //! Project version number for AnimatedRoundButton. 12 | FOUNDATION_EXPORT double AnimatedRoundButtonVersionNumber; 13 | 14 | //! Project version string for AnimatedRoundButton. 15 | FOUNDATION_EXPORT const unsigned char AnimatedRoundButtonVersionString[]; 16 | 17 | // In this header, you should import all the public headers of your framework using statements like #import 18 | 19 | 20 | -------------------------------------------------------------------------------- /AnimatedRoundButton/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | FMWK 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | $(CURRENT_PROJECT_VERSION) 21 | NSPrincipalClass 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /AnimatedRoundButtonExample/MenuVC.swift: -------------------------------------------------------------------------------- 1 | // 2 | // MenuVC.swift 3 | // AnimatedRoundButton 4 | // 5 | // Created by Roman Sorochak on 6/28/17. 6 | // Copyright © 2017 MagicLab. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import AnimatedRoundButton 11 | 12 | class MenuVC: UIViewController { 13 | 14 | @IBOutlet weak var menuButton: AnimatedRoundButton! 15 | @IBOutlet weak var streamButton: AnimatedRoundButton! 16 | @IBOutlet weak var demoButton: AnimatedRoundButton! 17 | 18 | 19 | override var prefersStatusBarHidden: Bool { 20 | return true 21 | } 22 | 23 | override func viewDidLoad() { 24 | super.viewDidLoad() 25 | 26 | menuButton.font = UIFont(name: "Helvetica", size: 20)! 27 | demoButton.font = UIFont(name: "Helvetica", size: 20)! 28 | streamButton.font = UIFont(name: "Helvetica", size: 25)! 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 MagicLab 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /AnimatedRoundButtonExample/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIMainStoryboardFile 26 | Main 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UIRequiresFullScreen 32 | 33 | UIStatusBarHidden 34 | 35 | UISupportedInterfaceOrientations 36 | 37 | UIInterfaceOrientationLandscapeRight 38 | 39 | UISupportedInterfaceOrientations~ipad 40 | 41 | UIInterfaceOrientationPortrait 42 | UIInterfaceOrientationPortraitUpsideDown 43 | UIInterfaceOrientationLandscapeLeft 44 | UIInterfaceOrientationLandscapeRight 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData/ 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata/ 19 | 20 | ## Other 21 | *.moved-aside 22 | *.xccheckout 23 | *.xcscmblueprint 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | *.ipa 28 | *.dSYM.zip 29 | *.dSYM 30 | 31 | ## Playgrounds 32 | timeline.xctimeline 33 | playground.xcworkspace 34 | 35 | # Swift Package Manager 36 | # 37 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 38 | # Packages/ 39 | # Package.pins 40 | .build/ 41 | 42 | # CocoaPods 43 | # 44 | # We recommend against adding the Pods directory to your .gitignore. However 45 | # you should judge for yourself, the pros and cons are mentioned at: 46 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 47 | # 48 | # Pods/ 49 | 50 | # Carthage 51 | # 52 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 53 | # Carthage/Checkouts 54 | 55 | Carthage/Build 56 | 57 | # fastlane 58 | # 59 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 60 | # screenshots whenever they are needed. 61 | # For more information about the recommended setup visit: 62 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 63 | 64 | fastlane/report.xml 65 | fastlane/Preview.html 66 | fastlane/screenshots 67 | fastlane/test_output 68 | -------------------------------------------------------------------------------- /AnimatedRoundButtonExample/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "20x20", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "20x20", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "29x29", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "29x29", 61 | "scale" : "2x" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "size" : "40x40", 66 | "scale" : "1x" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "size" : "40x40", 71 | "scale" : "2x" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "size" : "76x76", 76 | "scale" : "1x" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "size" : "76x76", 81 | "scale" : "2x" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "size" : "83.5x83.5", 86 | "scale" : "2x" 87 | } 88 | ], 89 | "info" : { 90 | "version" : 1, 91 | "author" : "xcode" 92 | } 93 | } -------------------------------------------------------------------------------- /AnimatedRoundButtonExample/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AnimatedRoundButton 2 | Beautiful animated round button. 3 | 4 | | Demo | 5 | |---------------------------------| 6 | |![Demo](https://github.com/MagicLab-team/AnimatedRoundButton/blob/master/AnimatedRoundButtonExample/Demo.gif)| 7 | 8 | ## Contents 9 | - [Requirements](#requirements) 10 | - [Installation](#installation) 11 | - [Usage](#usage) 12 | - [License](#license) 13 | 14 | 15 | ## Requirements 16 | 17 | - iOS 8.0+ 18 | - Swift 3.0+ 19 | 20 | ## Installation 21 | All logic is in [AnimatedRoundButton.swift](https://github.com/MagicLab-team/AnimatedRoundButton/blob/master/AnimatedRoundButton/AnimatedRoundButton.swift) file. 22 | Just copy this [file](https://github.com/MagicLab-team/AnimatedRoundButton/blob/master/AnimatedRoundButton/AnimatedRoundButton.swift) to your project. 23 | 24 | 25 | # Usage 26 | 1 - You can set class AnimatedRoundButton to view in storyboard an use @IBInspectable properties to setup button. 27 | 28 | 2 - You can do it from code: 29 | ```swift 30 | let button = AnimatedRoundButton( 31 | frame: CGRect(x: 0, y: 0, width: 200, height: 200) 32 | ) 33 | button.center = view.center 34 | view.addSubview(button) 35 | 36 | button.text = "AnimatedRoundButton" 37 | button.font = UIFont(name: "Helvetica", size: 25)! 38 | ``` 39 | 40 | 3 - You may setup colors for different states 41 | ```swift 42 | button.textColorsForStates[.normal] = UIColor.red 43 | button.textColorsForStates[.up] = UIColor.white 44 | button.textColorsForStates[.down] = UIColor.white 45 | 46 | button.contentColorsForStates[.normal] = UIColor.clear 47 | button.contentColorsForStates[.up] = UIColor.red 48 | button.contentColorsForStates[.down] = UIColor.clear 49 | ``` 50 | 51 | 4 - You may setup scale factors for different states 52 | ```swift 53 | button.scaleFactors[.normal] = 1 54 | button.scaleFactors[.up] = 1.3 55 | button.scaleFactors[.down] = 0.7 56 | ``` 57 | 58 | 59 | ## License 60 | 61 | AnimatedRoundButton is released under the MIT license. See [LICENSE](https://github.com/MagicLab-team/AnimatedRoundButton/blob/master/LICENSE) for details. 62 | -------------------------------------------------------------------------------- /AnimatedRoundButtonExample/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // AnimatedRoundButtonExample 4 | // 5 | // Created by Roman Sorochak on 6/28/17. 6 | // Copyright © 2017 MagicLab. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | 14 | var window: UIWindow? 15 | 16 | 17 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 18 | // Override point for customization after application launch. 19 | return true 20 | } 21 | 22 | func applicationWillResignActive(_ application: UIApplication) { 23 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 24 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 25 | } 26 | 27 | func applicationDidEnterBackground(_ application: UIApplication) { 28 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 29 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 30 | } 31 | 32 | func applicationWillEnterForeground(_ application: UIApplication) { 33 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 34 | } 35 | 36 | func applicationDidBecomeActive(_ application: UIApplication) { 37 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 38 | } 39 | 40 | func applicationWillTerminate(_ application: UIApplication) { 41 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 42 | } 43 | 44 | 45 | } 46 | 47 | -------------------------------------------------------------------------------- /AnimatedRoundButtonExample/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | -------------------------------------------------------------------------------- /AnimatedRoundButton/AnimatedRoundButton.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AnimatedRoundButton.swift 3 | // AnimatedRoundButton 4 | // 5 | // Created by Roman Sorochak on 6/28/17. 6 | // Copyright © 2017 MagicLab. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | 12 | public enum ScaleState: CGFloat { 13 | case normal = 1 14 | case up = 1.2 15 | case down = 0.8 16 | } 17 | 18 | 19 | public enum TouchState: String { 20 | case began = "Began" 21 | case cancelled = "Cancelled" 22 | case ended = "Ended" 23 | } 24 | 25 | @IBDesignable 26 | public class AnimatedRoundButton: UIView { 27 | 28 | static var defaultContentPaddingPercent: CGFloat = 0.05 29 | static var defaultScaleAnimationDuration: TimeInterval = 0.2 30 | static var defaultBackgroundColor = UIColor.clear 31 | static var defaultBorderColor = UIColor.red 32 | static var defaultBorderWidth: CGFloat = 1 33 | static var defaultTextColor: UIColor = UIColor.red 34 | 35 | 36 | private (set) var contentView: UIView! 37 | private (set) var label: UILabel! 38 | 39 | @IBInspectable public var paddingPercent: CGFloat = defaultContentPaddingPercent { 40 | didSet { 41 | setup() 42 | } 43 | } 44 | public var handlerButtonTouch: ((_ state: TouchState)->Void)? 45 | 46 | @IBInspectable public var animationDuration: TimeInterval = defaultScaleAnimationDuration { 47 | didSet { 48 | animationDurationForStates[.normal] = animationDuration 49 | animationDurationForStates[.up] = animationDuration 50 | animationDurationForStates[.down] = animationDuration 51 | } 52 | } 53 | public var animationDurationForStates: [ScaleState: TimeInterval] = [ 54 | .normal : defaultScaleAnimationDuration, 55 | .up : defaultScaleAnimationDuration, 56 | .down : defaultScaleAnimationDuration 57 | ] 58 | public var scaleFactors: [ScaleState: CGFloat] = [ 59 | .normal : ScaleState.normal.rawValue, 60 | .up : ScaleState.up.rawValue, 61 | .down : ScaleState.down.rawValue 62 | ] 63 | public var backgroundColorsForStates: [ScaleState: UIColor] = [ 64 | .normal : defaultBackgroundColor, 65 | .up : defaultBackgroundColor, 66 | .down : defaultBackgroundColor 67 | ] 68 | @IBInspectable public var contentColor: UIColor = defaultBorderColor { 69 | didSet { 70 | contentView.backgroundColor = contentColor 71 | } 72 | } 73 | public var contentColorsForStates: [ScaleState: UIColor] = [ 74 | .normal : UIColor.clear, 75 | .up : UIColor.red, 76 | .down : UIColor.clear 77 | ] { 78 | didSet { 79 | contentColor = contentColorsForStates[.normal] ?? contentColor 80 | } 81 | } 82 | @IBInspectable public var borderWidth: CGFloat = defaultBorderWidth { 83 | didSet { 84 | layer.borderWidth = borderWidth 85 | } 86 | } 87 | @IBInspectable public var contentBorderWidth: CGFloat = defaultBorderWidth { 88 | didSet { 89 | contentView.layer.borderWidth = contentBorderWidth 90 | } 91 | } 92 | @IBInspectable public var borderColor: UIColor = defaultBorderColor { 93 | didSet { 94 | self.layer.borderColor = borderColor.cgColor 95 | } 96 | } 97 | public var borderColorsForStates: [ScaleState: UIColor] = [ 98 | .normal : UIColor.red, 99 | .up : UIColor.red, 100 | .down : UIColor.red 101 | ] { 102 | didSet { 103 | borderColor = borderColorsForStates[.normal] ?? borderColor 104 | } 105 | } 106 | @IBInspectable public var contentBorderColor: UIColor = defaultBorderColor { 107 | didSet { 108 | self.contentView.layer.borderColor = contentBorderColor.cgColor 109 | } 110 | } 111 | public var contentBorderColorsForStates: [ScaleState: UIColor] = [ 112 | .normal : UIColor.red, 113 | .up : UIColor.red, 114 | .down : UIColor.red 115 | ] { 116 | didSet { 117 | contentBorderColor = contentBorderColorsForStates[.normal] ?? contentBorderColor 118 | } 119 | } 120 | @IBInspectable public var text: String? { 121 | get { 122 | return label.text 123 | } 124 | set { 125 | label.text = newValue 126 | } 127 | } 128 | @IBInspectable public var textColor: UIColor? { 129 | get { 130 | return label.textColor 131 | } 132 | set { 133 | label.textColor = newValue 134 | } 135 | } 136 | @IBInspectable public var font: UIFont { 137 | get { 138 | return label.font 139 | } 140 | set { 141 | label.font = newValue 142 | } 143 | } 144 | public var textColorsForStates: [ScaleState: UIColor] = [ 145 | .normal : UIColor.red, 146 | .up : UIColor.white, 147 | .down : UIColor.red 148 | ] { 149 | didSet { 150 | textColor = borderColorsForStates[.normal] ?? textColor 151 | } 152 | } 153 | 154 | 155 | /** 156 | * MARK: initialization 157 | */ 158 | 159 | public override func awakeFromNib() { 160 | super.awakeFromNib() 161 | 162 | setup() 163 | } 164 | 165 | override public init(frame: CGRect) { 166 | super.init(frame: frame) 167 | 168 | setup() 169 | } 170 | 171 | required public init?(coder aDecoder: NSCoder) { 172 | super.init(coder: aDecoder) 173 | 174 | setup() 175 | } 176 | 177 | private func setup() { 178 | setupContentView() 179 | setupLabel() 180 | 181 | makeRoundView(view: self) 182 | makeRoundView(view: contentView) 183 | 184 | backgroundColor = AnimatedRoundButton.defaultBackgroundColor 185 | contentColor = AnimatedRoundButton.defaultBackgroundColor 186 | 187 | borderWidth = AnimatedRoundButton.defaultBorderWidth 188 | contentBorderWidth = AnimatedRoundButton.defaultBorderWidth 189 | 190 | borderColor = AnimatedRoundButton.defaultBorderColor 191 | contentBorderColor = AnimatedRoundButton.defaultBorderColor 192 | } 193 | 194 | private func setupContentView() { 195 | if contentView == nil { 196 | contentView = UIView() 197 | addSubview(contentView) 198 | } 199 | let margin: CGFloat = self.frame.width * paddingPercent 200 | 201 | let frame = CGRect( 202 | x: bounds.origin.x + margin, 203 | y: bounds.origin.y + margin, 204 | width: bounds.width - margin * 2, 205 | height: bounds.height - margin * 2 206 | ) 207 | 208 | contentView.frame = frame 209 | } 210 | 211 | private func setupLabel() { 212 | if label == nil { 213 | label = UILabel() 214 | contentView.addSubview(label) 215 | } 216 | label.textAlignment = .center 217 | label.numberOfLines = 0 218 | textColor = textColorsForStates[.normal] 219 | label.frame = contentView.bounds 220 | } 221 | 222 | private func makeRoundView(view: UIView) { 223 | view.clipsToBounds = true 224 | 225 | let cornerRadius = (view.frame.width > view.frame.height ? 226 | view.frame.width / 2 : view.frame.height / 2) 227 | 228 | view.layer.cornerRadius = cornerRadius 229 | } 230 | 231 | 232 | /** 233 | * MARK: handle touches 234 | */ 235 | 236 | override public func touchesBegan(_ touches: Set, with event: UIEvent?) { 237 | super.touchesBegan(touches, with: event) 238 | 239 | scale(.up) 240 | 241 | handlerButtonTouch?(.began) 242 | } 243 | 244 | override public func touchesCancelled(_ touches: Set, with event: UIEvent?) { 245 | self.touchesCancelled(touches, with: event) 246 | 247 | scale(.normal) 248 | 249 | touchesCancelled(touches, with: event) 250 | } 251 | 252 | override public func touchesEnded(_ touches: Set, with event: UIEvent?) { 253 | super.touchesEnded(touches, with: event) 254 | 255 | scale(.down) 256 | scale(.normal) 257 | 258 | self.handlerButtonTouch?(.ended) 259 | } 260 | 261 | 262 | /* 263 | * MARK: animations 264 | */ 265 | 266 | private var animations = [ScaleState]() 267 | 268 | private func scale(_ scaleState: ScaleState, prevAnimIsFinished: Bool = false) { 269 | if !prevAnimIsFinished { 270 | animations.append(scaleState) 271 | guard animations.count == 1 else { 272 | return 273 | } 274 | } 275 | 276 | let animationDuration = animationDurationForStates[scaleState] ?? AnimatedRoundButton.defaultScaleAnimationDuration 277 | UIView.animate(withDuration: animationDuration, 278 | delay: 0, 279 | options: .curveLinear, 280 | animations: { () -> Void in 281 | 282 | self.layer.backgroundColor = (self.backgroundColorsForStates[scaleState] ?? UIColor.clear).cgColor 283 | self.layer.borderColor = (self.borderColorsForStates[scaleState] ?? UIColor.clear).cgColor 284 | 285 | self.contentView.layer.backgroundColor = (self.contentColorsForStates[scaleState] ?? UIColor.clear).cgColor 286 | self.contentView.layer.borderColor = (self.contentBorderColorsForStates[scaleState] ?? UIColor.clear).cgColor 287 | 288 | let scaleFactor = self.scaleFactors[scaleState] ?? scaleState.rawValue 289 | self.layer.transform = CATransform3DMakeScale( 290 | scaleFactor, 291 | scaleFactor, 292 | 1 293 | ) 294 | let contentScaleFactor: CGFloat = { 295 | switch scaleState { 296 | case .up: 297 | return self.frame.width / self.contentView.frame.width 298 | default: 299 | return scaleState.rawValue 300 | } 301 | }() 302 | self.contentView.layer.transform = CATransform3DMakeScale( 303 | contentScaleFactor, 304 | contentScaleFactor, 305 | 1 306 | ) 307 | 308 | self.textColor = self.textColorsForStates[scaleState] ?? self.textColor 309 | }) { (bool) -> Void in 310 | self.animations.removeFirst() 311 | 312 | if !self.animations.isEmpty { 313 | self.scale(self.animations[0], prevAnimIsFinished: true) 314 | } 315 | } 316 | } 317 | } 318 | --------------------------------------------------------------------------------