Code snippets for the blog post "Swift Protocols and the Law of Unintended Consequences" at http://www.apokrupto.com/blog-1/2016/8/14/swift-protocols-and-the-law-of-unintended-consequences
Last active
October 22, 2016 08:50
-
-
Save warren-gavin/e1ed07ba4160d7f002848bd4602f9923 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var animationDuration: CFTimeInterval { get } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol Shakeable { | |
var horizontalShake: CGFloat { get } | |
var verticalShake: CGFloat { get } | |
var animationDuration: CFTimeInterval { get } | |
var repeatCount: Float { get } | |
func shake() | |
} | |
struct Defaults { | |
static let horizontalShake: CGFloat = 4.0 | |
static let verticalShake: CGFloat = 0.0 | |
static let duration: CFTimeInterval = 0.05 | |
static let repeatCount: Float = 5 | |
} | |
extension Shakeable { | |
var horizontalShake: CGFloat { | |
return Defaults.horizontalShake | |
} | |
var verticalShake: CGFloat { | |
return Defaults.verticalShake | |
} | |
var animationDuration: CFTimeInterval { | |
return Defaults.duration | |
} | |
var repeatCount: Float { | |
return Defaults.repeatCount | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Shakeable where Self: UIView { | |
func shake() { | |
let position = "position" | |
let animation = CABasicAnimation(keyPath: position) | |
animation.duration = animationDuration | |
animation.repeatCount = repeatCount | |
animation.autoreverses = true | |
animation.fromValue = NSValue(cgPoint: CGPoint(x: center.x - horizontalShake, | |
y: center.y - verticalShake)) | |
animation.toValue = NSValue(cgPoint: CGPoint(x: center.x + horizontalShake, | |
y: center.y + verticalShake)) | |
layer.add(animation, forKey: position) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ImageView: UIImageView, Shakeable { | |
@IBInspectable var horizontalShake: CGFloat = Defaults.horizontalShake | |
@IBInspectable var verticalShake: CGFloat = Defaults.verticalShake | |
} | |
class Button: UIButton, Shakeable { | |
@IBInspectable var horizontalShake: CGFloat = Defaults.horizontalShake | |
@IBInspectable var verticalShake: CGFloat = Defaults.verticalShake | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol Shakeable { | |
var horizontalShake: CGFloat { get } | |
var verticalShake: CGFloat { get } | |
var duration: CFTimeInterval { get } | |
var repeatCount: Float { get } | |
func shake() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol ShakeablesContainer { | |
var shakeables: [Shakeable] { get } | |
func shake() | |
} | |
extension ShakeablesContainer { | |
func shake() { | |
shakeables.forEach { $0.shake() } | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ViewController: UIViewController, ShakeablesContainer { | |
@IBOutlet weak var imageView: ImageView! | |
@IBOutlet weak var button: Button! | |
@IBAction func shake(_ sender: Button) { | |
shake() | |
} | |
var shakeables: [Shakeable] { | |
return [imageView, button].flatMap { | |
$0 as? Shakeable | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Since the shake direction is limited to horizontal and vertical in this case, what say if we use enum for the shake direction