LoginSignup
3
3

More than 5 years have passed since last update.

[UIView beginAnimations:context]でcontextに任意のオブジェクトを指定してユーザ情報をやり取りする方法

Posted at

(良いタイトルが思い浮かばなかったので、長くなりました。。)

UIViewのAnimation処理でbeginAnimations:contextを使う方法で、
contextに任意のユーザ情報を渡すやり方です。

実際にbeginAnimations:contextは

+ (void)beginAnimations:(NSString *)animationID context:(void *)context

とcontextはvoid*型になっているので何でも渡せるようになっています。
以下は、contextを用いてユーザ情報を渡すSampleです。

// beginAnimations:context sample

- (void)animationStart {
    NSDictionary *userInfo = @{@"key":@"val"};

    [UIView beginAnimations:@"animation" context:(__bridge_retained void*)(userInfo)];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:0.27f];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
    /* animation処理 */
    [UIView commitAnimations];
}

次にAnimationが終了した際に呼ばれるメソッドの中身で、実際にcontextを受け取り解釈します。

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    NSDictionary *contextObj = (__bridge_transfer NSDictionary*)context;
    NSLog(@"%@", contextObj[@"key"]);
}

実行結果

val

勘違いし易いのが(私も最初はそうでした)、beginAnimationsはグラフィックスコンテキストではありません。
上述の通り、アニメーションのデリゲートメソッドで受け取り、その後の条件判定用にユーザが好きに指定するものです。

間違ってる点等あれば、こちらでご指摘か、同じ記事を管理blogにも載せてますので、そちらにお願い致します。

3
3
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
3
3