Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
13
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

@nofrmm

iOSでAndroid風なToastを演出 〜 UIAlertController

はじめに

アニメーションするパーツをかきたくなって
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コード書くのはまだ楽な感じがします。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
13
Help us understand the problem. What are the problem?