Skip to content

Instantly share code, notes, and snippets.

View masuhara's full-sized avatar

Daisuke Masuhara masuhara

View GitHub Profile
@masuhara
masuhara / gist:0d8f78c7c5086c3efbb4bf32a1eef745
Last active February 12, 2019 10:36
カウントダウンタイマー
【カウントダウンタイマー】
// ①Storyboardにタイマー用のLabelを置く
// ②変数宣言する
// タイマー用カウンター
var count: Double = 0.0
// タイマー用ラベル
@IBOutlet var timerLabel: UILabel!
// ②func showQuizの最後に以下のコードを追加する
Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(countDownTimer(timer:)), userInfo: nil, repeats: true)
extension UITextView {
func setTextWithTypeAnimation(typedText: String, characterDelay: TimeInterval = 5.0) {
text = ""
var writingTask: DispatchWorkItem?
writingTask = DispatchWorkItem { [weak weakSelf = self] in
for character in typedText {
DispatchQueue.main.async {
weakSelf?.text!.append(character)
}
Thread.sleep(forTimeInterval: characterDelay/100)
@masuhara
masuhara / gist:ae02e436cd59fd2c60bf2cbc140732f3
Last active September 16, 2018 07:02
Closure in Closure
func uploadProfileImage(imageData: Data, filename: String, result: @escaping (_ imageUrl: String?, _ error: Error?) -> Void) {
// 例えばアップロード先への参照ができるstorage変数に画像をアップロードし、アップロードが完了されたらその画像のダウンロードURLを取得する場合
storage.putData(imageData, completion: { (metadata, error) in
// 追加
print(metadata) // <- 呼ばれる
if let error = error {
// クロージャの呼び出し元に返す
result(nil, error)
} else {
metadata?.storageReference?.downloadURL(completion: { (url, error) in
@masuhara
masuhara / RainbowGradient.swift
Last active May 6, 2018 20:33
Rainbow color gradient written in Swift2.2
func insertGradientLayer(view: UIView) {
let color1 = UIColor.redColor()
let color2 = UIColor(red: 255, green: 165, blue: 0, alpha: 1.0)
let color3 = UIColor.yellowColor()
let color4 = UIColor(red: 0, green: 128, blue: 0, alpha: 1.0)
let color5 = UIColor.cyanColor()
let color6 = UIColor.blueColor()
let color7 = UIColor.purpleColor()
let fromColors = [color1.CGColor, color2.CGColor, color3.CGColor, color4.CGColor, color5.CGColor, color6.CGColor, color7.CGColor]
let toColors = fromColors.reverse()
@masuhara
masuhara / ViewController.swift
Created December 1, 2015 12:55
TokyoMetroAPI
import UIKit
class ViewController: UIViewController {
@IBOutlet var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
@masuhara
masuhara / ButtonAnimationSample.swift
Created November 22, 2015 08:02
Button Pop Animation with CABasicAnimation written in Swift
// Button Animation
@IBAction func pushButton(button: UIButton) {
// CABasicAnimation
let animation = CABasicAnimation(keyPath: "transform")
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
animation.duration = 0.05
animation.repeatCount = 1
animation.autoreverses = true
animation.removedOnCompletion = true
animation.toValue = NSValue(CATransform3D: CATransform3DMakeScale(1.1, 1.1, 1.0))
@masuhara
masuhara / ButtonAnimationSample.m
Last active November 7, 2015 19:15
Button Pop Animation with Objective-C
#pragma mark - Button Animation
- (void)pushButton {
// 1. Use Facebook POP
POPSpringAnimation *scaleAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
scaleAnimation.velocity = [NSValue valueWithCGSize:CGSizeMake(3.f, 3.f)];
scaleAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(1.f, 1.f)];
scaleAnimation.springBounciness = 18.0f;
[button.layer pop_addAnimation:scaleAnimation forKey:@"layerScaleSpringAnimation"];
@masuhara
masuhara / DMGameHighScore
Last active August 29, 2015 14:10
DMGameHighScore
- (void)saveHighScoreToNSUserDefaults:(float)currentScore
{
// Load last HighScore
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
float lastScore = [userDefaults floatForKey:@"highScore"];
if (lastScore < currentScore) {
[userDefaults setFloat:currentScore forKey:@"highScore"];
// Save now
[userDefaults synchronize];
@masuhara
masuhara / DMCheckString
Created September 22, 2014 13:26
Yuka(Osaka School)'s Calculator program that can trim appropriate strings.
#pragma mark - Check String (まっすーアルゴリズム)
- (NSString *)checkString:(NSString *)string
{
NSRange searchResult = [string rangeOfString:@"="];
NSString *numberString;
NSLog(@"string is %@", string);
if(searchResult.location == NSNotFound){