14
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

iOSでAndroid風なToastを演出 〜 UIAlertController

Last updated at Posted at 2014-12-06

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

alert.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を使う場合

    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

    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

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コード書くのはまだ楽な感じがします。

14
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?