ROADTO みちログ

ひとのみちのブログ。大阪でiOSアプリの道を歩くフリーランス。

Swift2の何回も調べてるちょっとしたこと

Swift2でなんども調べてるちょっとしたことを書きます。

タイマー

let timer = NSTimer.scheduledTimerWithTimeInterval(1.0,
                                                   target: self, 
                                                   selector: #selector(methodName:), 
                                                   userInfo: nil, 
                                                   repeats: true)

遅延実行

//1秒遅延
let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(1.0 * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue(), {
    // 遅延する処理
})

非同期実行

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
    // 非同期処理
    dispatch_async(dispatch_get_main_queue()) {
        // メインスレッド処理
    }
}

セマフォ

// 同時実行可能数 = 1
let semaphore = dispatch_semaphore_create(1)
// ここで待つ
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER)
// 処理が終わったことを通知
dispatch_semaphore_signal(semaphore)

非同期を同期的にする。

// 同時実行可能数 = 0
let semaphore = dispatch_semaphore_create(0)
// 非同期処理 ※DISPATCH_QUEUE_PRIORITY_BACKGROUNDを使うこと
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)) {
    // 処理が終わったことを通知
    dispatch_semaphore_signal(semaphore)
}
// ここで待つ
while dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW) != 0 {
    // 10秒間外部からのメソッド実行要求を受け付ける
    NSRunLoop.currentRunLoop().runMode(NSDefaultRunLoopMode, beforeDate: NSDate(timeIntervalSinceNow: 10))
}

画面サイズ

//画面幅
let width = UIScreen.mainScreen().bounds.size.width
//画面高さ
let height = UIScreen.mainScreen().bounds.size.height

アニメーション

UIView.animateWithDuration(1.0, delay: 3.0, options: .CurveEaseInOut, animations: { () -> Void in
    // アニメーション処理
}, completion: { (finished: Bool) -> Void in
    // アニメーション完了後の処理
})
自己紹介
メガネは売っていません
高浜 一道(たかはま かずみち)
大阪でiOSアプリの道を歩くフリーランス。
iPad・iPhoneとサーバを連携した、JSONやDBといったキーワードが出てくるツール系が多めです。
お仕事のご依頼やご連絡はこちらから。
お仕事ください!
Web : ROADTO
ML : k-takahama(a)roadto.jp

個人的なアカウントはこちら。
Tw : @hitonomichi
Fb : kazumichi.takahama