├── Pod ├── Assets │ └── .gitkeep └── Classes │ ├── .gitkeep │ └── AnimationSugar.swift ├── Images ├── AnimationSugar_1.gif ├── AnimationSugar_2.gif ├── AnimationSugar_3.gif ├── AnimationSugar_4.gif └── AnimationSugar_logo.png ├── LICENSE ├── AnimationSugar.podspec └── README.md /Pod/Assets/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Pod/Classes/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Images/AnimationSugar_1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intrepidmatt/AnimationSugar/HEAD/Images/AnimationSugar_1.gif -------------------------------------------------------------------------------- /Images/AnimationSugar_2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intrepidmatt/AnimationSugar/HEAD/Images/AnimationSugar_2.gif -------------------------------------------------------------------------------- /Images/AnimationSugar_3.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intrepidmatt/AnimationSugar/HEAD/Images/AnimationSugar_3.gif -------------------------------------------------------------------------------- /Images/AnimationSugar_4.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intrepidmatt/AnimationSugar/HEAD/Images/AnimationSugar_4.gif -------------------------------------------------------------------------------- /Images/AnimationSugar_logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intrepidmatt/AnimationSugar/HEAD/Images/AnimationSugar_logo.png -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 intrepidmatt 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 | -------------------------------------------------------------------------------- /AnimationSugar.podspec: -------------------------------------------------------------------------------- 1 | 2 | # Be sure to run `pod lib lint AnimationSugar.podspec' to ensure this is a 3 | # valid spec before submitting. 4 | # 5 | # Any lines starting with a # are optional, but their use is encouraged 6 | # To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html 7 | # 8 | 9 | Pod::Spec.new do |s| 10 | s.name = "AnimationSugar" 11 | s.version = "1.0.1" 12 | s.summary = "Syntactic sugar for iOS animations." 13 | 14 | # This description is used to generate tags and improve search results. 15 | # * Think: What does it do? Why did you write it? What is the focus? 16 | # * Try to keep it short, snappy and to the point. 17 | # * Write the description between the DESC delimiters below. 18 | # * Finally, don't worry about the indent, CocoaPods strips it! 19 | s.description = <<-DESC 20 | A tiny Swift library for cleaning up your animation code. Easily tweak options 21 | with fluent-style syntax, and avoid nested callback for multi-step animations. 22 | DESC 23 | 24 | s.homepage = "https://github.com/intrepidmatt/AnimationSugar" 25 | s.license = 'MIT' 26 | s.author = { "Matt Bridges" => "matt@intrepid.io" } 27 | s.source = { :git => "https://github.com/intrepidmatt/AnimationSugar.git", :tag => s.version.to_s } 28 | s.social_media_url = 'https://twitter.com/rrridges' 29 | 30 | s.platform = :ios, '8.0' 31 | s.requires_arc = true 32 | 33 | s.source_files = 'Pod/Classes/**/*' 34 | 35 | # s.public_header_files = 'Pod/Classes/**/*.h' 36 | # s.frameworks = 'UIKit', 'MapKit' 37 | # s.dependency 'AFNetworking', '~> 2.3' 38 | end 39 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![AnimationSugar](Images/AnimationSugar_logo.png) 2 | 3 | ## AnimationSugar 4 | 5 | AnimationSugar is a tiny Swift library that can make iOS animation code slightly less ugly. 6 | 7 | When iterating on animations, it can be a pain to swap between the many method signatures of `UIView.animateWithDuration`. Also, multi-step animations that nest inside of completion blocks can quickly become an unreadabe mess. 8 | 9 | It was built at [Intrepid](http://intrepid.io). 10 | 11 | ##Examples 12 | 13 | animate { 14 | view.translate(tx: 150) 15 | } 16 | 17 | ![Image of animation to the right](Images/AnimationSugar_1.gif) 18 | 19 | animate { 20 | view.translate(tx: 150) 21 | } 22 | .withSpring(dampingRatio: 0.5, initialVelocity: 0) 23 | 24 | ![Image of animation with spring](Images/AnimationSugar_2.gif) 25 | 26 | animate { 27 | view.translate(tx: 150) 28 | } 29 | .thenAnimate { 30 | view.translate(ty: 150) 31 | } 32 | .thenAnimate { 33 | view.translate(tx: -150) 34 | } 35 | .thenAnimate { 36 | view.translate(ty: -150) 37 | } 38 | 39 | ![Image of animation in a square](Images/AnimationSugar_3.gif) 40 | 41 | animate(duration: 0.3) { 42 | view.translate(tx: 150) 43 | } 44 | .withOption(.CurveEaseOut) 45 | .withCompletion { _ in 46 | view.backgroundColor = UIColor(white: 0.8, alpha: 1.0) 47 | } 48 | 49 | ![Image of animation with completion](Images/AnimationSugar_4.gif) 50 | 51 | ## Usage 52 | 53 | ### Cocoapods 54 | 55 | pod install 'AnimationSugar' 56 | 57 | ## Reference 58 | 59 | ### Animation helpers 60 | 61 | `func animate(duration duration: NSTimeInterval = defaultAnimationDuration, animations: () -> ()) -> Animation` 62 | 63 | - Starts an animation. Use trailing closure syntax to write your animations, e.g.: `animate(duration: 0.3) { ... }`. Without the duration argument, a default duration is used (see `defaultAnimationDuration`). 64 | 65 | `func withDelay(delay: NSTimeInterval) -> Animation` 66 | - Set a delay to wait before the animation starts. 67 | 68 | `func withOption(option: UIViewAnimationOptions) -> Animation` 69 | - Set an animation option for the animation. For multiple options, either chain them e.g. `.withOption(.Autoreverse).withOption(.Repeat)` or use once with an array e.g. `.withOption([.Autoreverse, .Repeat])`. 70 | 71 | `func withSpring(dampingRatio dampingRatio: CGFloat, initialVelocity: CGFloat) -> Animation` 72 | - Use a spring animation curve with the given damping ratio and initial velocity. 73 | 74 | `func withCompletion(completion: (Bool) -> ()) -> Animation` 75 | - Set a completion block for when the animation finishes. Use trailing closure syntax e.g. `animate { ... }.withCompletion { ... }` 76 | 77 | `func thenAnimate(duration duration: NSTimeInterval = defaultAnimationDuration, animations: () -> ()) -> Animation` 78 | - Chain a new animation to occur after the previous animation is completed. See the "square" example above. Without the duration argument, a default duration is used (see `defaultAnimationDuration`) 79 | 80 | `static var defaultAnimationDuration` 81 | - Change the default animation duration, to use `animate()` or `thenAnimate()` without a duration argument. This value is initialized to 0.3 seconds and can be changed at any time, i.e. `Animation.defaultAnimationDuration = 0.25`. 82 | 83 | ### UIView helpers 84 | 85 | `func translate(tx tx: CGFloat = 0, ty: CGFloat = 0)` 86 | 87 | - Translate a view by altering its `transform` property 88 | 89 | `func scale(sx sx: CGFloat, sy: CGFloat)` 90 | 91 | - Scale a view by altering its `transform` property 92 | 93 | `func rotate(angle angle: CGFloat)` 94 | 95 | - Rotate a view by altering its `transform` property 96 | 97 | `func translateFrame(tx tx: CGFloat = 0, ty: CGFloat = 0)` 98 | 99 | - Translate a view by altering its `frame` property 100 | 101 | `func stretchFrame(deltaWidth deltaWidth: CGFloat, deltaHeight: CGFloat)` 102 | 103 | - Stretch a view's `frame` 104 | 105 | `func resizeFrame(width width: CGFloat, height: CGFloat)` 106 | 107 | - Resize a view's `frame` 108 | 109 | `func flipHorizontal(perspectiveDistance: CGFloat = 1000.0)` 110 | 111 | - Flip a view along the horizontal axis. 112 | 113 | `func flipVertical(perspectiveDistance: CGFloat = 1000.0)` 114 | 115 | - Flip a view along the vertical axis. 116 | 117 | `func fadeIn(duration duration: NSTimeInterval = Animation.defaultAnimationDuration) -> Animation` 118 | 119 | - Fade in a view from invisible to visible over time. Can be chained with other animations. 120 | 121 | `func fadeOut(duration duration: NSTimeInterval = Animation.defaultAnimationDuration) -> Animation` 122 | 123 | - Fade out a view from visible to invisible over time. Can be chained with other animations. 124 | 125 | 126 | -------------------------------------------------------------------------------- /Pod/Classes/AnimationSugar.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Animate.swift 3 | // 4 | // Created by Matt Bridges on 2/27/16. 5 | // Copyright © 2016 Matt Bridges. All rights reserved. 6 | // 7 | 8 | import Foundation 9 | import UIKit 10 | 11 | /** 12 | Animate changes to one or more `UIView` objects. 13 | 14 | - Parameter duration: The length over which the animation(s) occur. 15 | - Parameter animations: A block containing changes to `UIView` animatable properties. 16 | - Returns An `Animation` object that can be further modified (e.g. changing an 17 | animation curve) or chained with further animations. 18 | */ 19 | public func animate(duration: TimeInterval = Animation.defaultAnimationDuration, animations: @escaping () -> ()) -> Animation { 20 | return Animation(duration: duration, animations: animations) 21 | } 22 | 23 | public class Animation { 24 | 25 | private struct Constants { 26 | static let DefaultAnimationDuration: TimeInterval = 0.3 27 | } 28 | 29 | public static var defaultAnimationDuration = Constants.DefaultAnimationDuration 30 | private let animations: () -> () 31 | private let duration: TimeInterval 32 | private var delay: TimeInterval = 0 33 | private var options: UIViewAnimationOptions? 34 | private var completion: ((Bool) -> ())? 35 | private var springDampingRatio: CGFloat? 36 | private var springInitialVelocity: CGFloat? 37 | private var prevAnimation: Animation? 38 | private var nextAnimation: Animation? 39 | 40 | /** 41 | Initialize an `Animation` object. 42 | 43 | - Parameter duration: The length over which the animation(s) occur 44 | - Parameter animations: A block containing changes to `UIView` animatable properties. 45 | - Parameter startNow: A boolean indicating whether to immediately start the animation 46 | or wait to be triggered later. Chained animations set this parameter to false. 47 | */ 48 | init(duration: TimeInterval, animations: @escaping () -> (), startNow: Bool = true) { 49 | self.duration = duration 50 | self.animations = animations 51 | 52 | if (startNow) { 53 | DispatchQueue.main.async { 54 | self.start() 55 | } 56 | } 57 | } 58 | 59 | /** 60 | Modify the animation options. 61 | 62 | - Parameter option: The option(s) to set. E.g. `.withOption(.AnimationCurveEaseOut)` 63 | - Returns An `Animation` object that can be modified or chained with further animations. 64 | */ 65 | public func withOption(option: UIViewAnimationOptions) -> Animation { 66 | if let options = options { 67 | self.options = options.union(option) 68 | } else { 69 | self.options = option 70 | } 71 | 72 | return self 73 | } 74 | 75 | /** 76 | Add a delay before an animation begins. 77 | 78 | - Parameter delay: The amount of time to delay. 79 | - Returns An `Animation` object to be modified or chained with further animations. 80 | */ 81 | public func withDelay(delay: TimeInterval) -> Animation { 82 | self.delay = delay 83 | return self 84 | } 85 | 86 | /** 87 | Set a completion block that runs when the animation has completed. 88 | 89 | - Parameter completion: A block of code to run. 90 | - Returns An `Animation` object to be modified or chained with further animations. 91 | */ 92 | public func withCompletion(completion: @escaping (Bool) -> ()) -> Animation { 93 | self.completion = completion 94 | return self 95 | } 96 | 97 | /** 98 | Animate using "spring" physics. 99 | 100 | - Parameter dampingRatio: The damping ratio for the spring animation as it approaches 101 | its quiescent state. 102 | 103 | To smoothly decelerate the animation without oscillation, use a value of 1. Employ 104 | a damping ratio closer to zero to increase oscillation. 105 | - Parameter initialVelocity: The initial spring velocity. For smooth start to the 106 | animation, match this value to the view’s velocity as it was prior to attachment. 107 | A value of 1 corresponds to the total animation distance traversed in one second. 108 | 109 | For example, if the total animation distance is 200 points and you want the start of the 110 | animation to match a view velocity of 100 pt/s, use a value of 0.5. 111 | - Returns An `Animation` object to be modified or chained with further animations. 112 | */ 113 | public func withSpring(dampingRatio: CGFloat, initialVelocity: CGFloat) -> Animation { 114 | self.springDampingRatio = dampingRatio 115 | self.springInitialVelocity = initialVelocity 116 | return self 117 | } 118 | 119 | /** 120 | Chain a new animation to begin immediately after a previous animation finishes. 121 | 122 | Use this method to prevent excessive nesting of animations inside completion blocks. 123 | 124 | - Parameter duration: The length over which the animation occurs. 125 | - Parameter animations: A block containing changes to `UIView` animatable properties. 126 | - Returns An `Animation` object to be modified or chained with further animations 127 | */ 128 | public func thenAnimate(duration: TimeInterval = defaultAnimationDuration, animations: @escaping () -> ()) -> Animation { 129 | let nextAnimation = Animation(duration: duration, animations: animations, startNow: false) 130 | nextAnimation.prevAnimation = self 131 | self.nextAnimation = nextAnimation 132 | 133 | // Run current completion block, then run next animation 134 | let completionBlock = self.completion 135 | self.completion = { 136 | finished in 137 | completionBlock?(finished) 138 | self.nextAnimation?.start() 139 | } 140 | 141 | return nextAnimation 142 | } 143 | 144 | /** 145 | Start an animation. 146 | */ 147 | private func start() { 148 | if let prevAnimation = self.prevAnimation { 149 | prevAnimation.start() 150 | } else { 151 | if let nextAnimation = self.nextAnimation { 152 | nextAnimation.prevAnimation = nil 153 | } 154 | 155 | guard let dampingRatio = springDampingRatio, let initialVelocity = springInitialVelocity else { 156 | UIView.animate(withDuration: duration, 157 | delay: delay, 158 | options: options ?? [], 159 | animations: animations, 160 | completion: completion) 161 | return 162 | } 163 | 164 | UIView.animate(withDuration: duration, 165 | delay: delay, 166 | usingSpringWithDamping: dampingRatio, 167 | initialSpringVelocity: initialVelocity, 168 | options: options ?? [], 169 | animations: animations, 170 | completion: completion) 171 | } 172 | } 173 | } 174 | 175 | extension UIView { 176 | /** 177 | Translate the view using its `transform` property. 178 | 179 | - Parameter tx: Translation distance along the x axis 180 | - Parameter ty: Translation distance along the y axis 181 | */ 182 | public func translate(tx: CGFloat = 0, ty: CGFloat = 0) { 183 | self.transform = self.transform.translatedBy(x: tx, y: ty) 184 | } 185 | 186 | /** 187 | Scale the view using its `transform` property. 188 | 189 | - Parameter sx: Scale factor in the x dimension 190 | - Parameter xy: Scale factor in the y dimension 191 | */ 192 | public func scale(sx: CGFloat, sy: CGFloat) { 193 | self.transform = self.transform.scaledBy(x: sx, y: sy) 194 | } 195 | 196 | /** 197 | Rotate the view using its `transform` property. 198 | 199 | - Parameter angle: The angle to rotate in radians 200 | */ 201 | public func rotate(angle: CGFloat) { 202 | self.transform = self.transform.rotated(by: angle) 203 | } 204 | 205 | /** 206 | Translate the view by altering its `frame` property. 207 | 208 | - Parameter tx: Translation distance along the x axis 209 | - Parameter ty: Translation distance along the y axis 210 | */ 211 | public func translateFrame(tx: CGFloat = 0, ty: CGFloat = 0) { 212 | var frame = self.frame 213 | frame.origin.x = frame.origin.x + tx 214 | frame.origin.y = frame.origin.y + ty 215 | self.frame = frame 216 | } 217 | 218 | /** 219 | Stretch or shring the view using its `frame` property. 220 | 221 | - Parameter deltaWidth: The amount to stretch the frame in the x direction 222 | - Parameter deltaHeight: The amount to stretch the frame in the y direction 223 | */ 224 | public func stretchFrame(deltaWidth: CGFloat, deltaHeight: CGFloat) { 225 | var frame = self.frame 226 | frame.size.width = frame.size.width + deltaWidth 227 | frame.size.height = frame.size.height + deltaHeight 228 | self.frame = frame 229 | } 230 | 231 | /** 232 | Resize the view's `frame` property. 233 | 234 | - Parameter width: The new width 235 | - Parameter height: The new height 236 | */ 237 | public func resizeFrame(width: CGFloat, height: CGFloat) { 238 | var frame = self.frame 239 | frame.size.width = width 240 | frame.size.height = height 241 | self.frame = frame 242 | } 243 | 244 | /** 245 | Flip the view 180 degrees horizontally (along the y axis) 246 | 247 | - Parameter perspectiveDistance: The simulated distance between the observer 248 | and the view, in points. Since the flip is in three dimensions, a perspective 249 | transform is applied. Smaller values will have an exaggerated perspective. 250 | Default value is 1000 points. 251 | */ 252 | public func flipHorizontal(perspectiveDistance: CGFloat = 1000.0) { 253 | self.layer.transform.m34 = -1.0 / perspectiveDistance 254 | self.layer.transform = CATransform3DRotate(self.layer.transform, CGFloat(M_PI), 0, 1.0, 0) 255 | } 256 | 257 | /** 258 | Flip the view 180 degrees vertically (along the x axis) 259 | 260 | - Parameter perspectiveDistance: The simulated distance between the observer 261 | and the view, in points. Since the flip is in three dimensions, a perspective 262 | transform is applied. Smaller values will have an exaggerated perspective. 263 | Default value is 1000 points. 264 | */ 265 | public func flipVertical(perspectiveDistance: CGFloat = 1000.0) { 266 | self.layer.transform.m34 = -1.0 / perspectiveDistance 267 | self.layer.transform = CATransform3DRotate(self.layer.transform, CGFloat(M_PI), 1.0, 0, 0) 268 | } 269 | 270 | /** 271 | Fade in a view by animating its `alpha`. 272 | 273 | - Parameter duration: The duration of the animation 274 | - Returns: An `Animation` that can be chained or altered. 275 | */ 276 | public func fadeIn(duration: TimeInterval = Animation.defaultAnimationDuration) -> Animation { 277 | self.alpha = 0.0 278 | return animate(duration: duration) { 279 | self.alpha = 1.0 280 | } 281 | } 282 | 283 | /** 284 | Fade out a view by animating its `alpha`. 285 | 286 | - Parameter duration: The duration of the animation 287 | - Returns An `Animation` that can be chained or altered. 288 | */ 289 | public func fadeOut(duration: TimeInterval = Animation.defaultAnimationDuration) -> Animation { 290 | return animate(duration: duration) { 291 | self.alpha = 0.0 292 | } 293 | } 294 | } 295 | --------------------------------------------------------------------------------