iOSでAndroid風なToastを演出 〜 UIAlertController

#はじめに
アニメーションするパーツをかきたくなって
AndroidのToast風なものをつくってみました。


![alert.gif](https://qiita-image-store.s3.amazonaws.com/0/12362/f3013b6e-8796-0684-dac1-63396730c197.gif)


##Toastとは
>http://developer.android.com/guide/topics/ui/notifiers/toasts.html

>A toast provides simple feedback about an operation in a small popup. It only fills the amount of space required for the message and the current activity remains visible and interactive. For example, navigating away from an email before you send it triggers a "Draft saved" toast to let you know that you can continue editing later. Toasts automatically disappear after a timeout.

#コードA
GCDのdispatch_afterを使う場合

```objc
    UIAlertController *alertController =
    [UIAlertController alertControllerWithTitle:@"Alert" message:@"iOS8" preferredStyle:UIAlertControllerStyleAlert];
    
    [self presentViewController:alertController animated:YES completion:^{
        double delayInSeconds = 3.0;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
        dispatch_after(popTime, dispatch_get_main_queue(), ^{
            [UIView animateWithDuration:0.8
                             animations:^{
                                 alertController.view.alpha  = 0.0;
                             } completion:^(BOOL finished){
                                 [alertController dismissViewControllerAnimated:NO completion:nil];
                             }];
        });
    }];
```

##dispatch_time
アプリがbackgroundになっているときはカウントを停止する。
backgroundになっているときも秒数カウントするにはdipatch_walltimeを使う。

##dispatch_after
GCDで遅延実行を行うための関数

#コードB
```objc
    UIAlertController *alertController =
    [UIAlertController alertControllerWithTitle:@"Alert" message:@"iOS8" preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:alertController animated:YES completion:^{
            [UIView animateWithDuration:0.8 delay:3
                                options:UIViewAnimationOptionCurveEaseOut
                             animations:^{
                                 alertController.view.alpha  = 0.0;
                             }
                             completion:^(BOOL finished){
                                 [alertController dismissViewControllerAnimated:NO completion:nil];
                             }
             ];
    }];
```
#参考URL

http://www.raywenderlich.com/60749/grand-central-dispatch-in-depth-part-1

http://stackoverflow.com/questions/4139219/how-do-you-trigger-a-block-after-a-delay-like-performselectorwithobjectafter

GCDでタイマーな処理
http://qiita.com/woxtu/items/05d4d80db7fa8e9db408

What is the difference between dispatch_time and dispatch_walltime and in what situations is better to use one or the other?
http://stackoverflow.com/questions/26062702/what-is-the-difference-between-dispatch-time-and-dispatch-walltime-and-in-what-s

iOS quiz: 以下のdispatch_after()を使ったコードが1, 2, 3, 4という順で実行される理由を説明してください
http://d.hatena.ne.jp/gfx/20140402/1396438225

GCD (Grand Central Dispatch) – Dispatch Queue でマルチスレッドプログラミング
http://fernweh.jp/b/grandcentraldispatch/

#おまけ
動画GIFをつくるのにPhotoShopをつかうと簡単なんですね・・・
http://ach.pazru.com/photoshop/動画をgifアニメーションにする（mov%20→%20gif）

#さいごに
swiftに比べるとXcodeでObj-cコード書くのはまだ楽な感じがします。