Last active
September 2, 2016 17:35
-
-
Save KaiCode2/211cea8436132ab207be67c7f1e7beb4 to your computer and use it in GitHub Desktop.
This file contains 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
public extension UIImage { | |
/** | |
Example: | |
``` | |
UIImage.animationSequence(withPattern: "0XX_animation_images", swapMatching: "XX", numberOfImages: 60) | |
``` | |
@param base The base string on which the animation image sequence will be generated. Note: Must have *match* embeded inside. | |
@param match: The pattern contained in pattern that will be substituted. | |
@param count: number of images to be generated. Note: Must be less than or equal to log 10 of match length. | |
*/ | |
static func animationSequence(withBase base: String, | |
swapMatching match: String, | |
numberOfImages count: Int) -> [UIImage] { | |
func curriedName(base: String, match: String) -> (String) -> String { | |
return { replacement in | |
return base.replacingOccurrences(of: match, with: replacement) | |
} | |
} | |
let baseString = curriedName(base: base, match: match) | |
var images: [UIImage] = [] | |
let baseNumber = match.characters.count | |
for i in 0..<count { | |
let numberOf0s = baseNumber - Int(floor(log10(Double(i)))) + 1 | |
let zeros = Array(repeating: "0", count: numberOf0s).reduce("", +) | |
if let image = UIImage(named: baseString(zeros + "\(i)")) { | |
images.append(image) | |
} | |
} | |
return images | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment