以前紹介した投稿でアニメーションにひっかかりを感じると書いた。
UITableView を下にドラッグした時に画面上部の矢印が下向き↓からクルッと回転して上向き↑になるアニメーションが起こる。これを iPhoneで実行してみるとカクっとひっかかるような感じがあった。コードはこんな感じ。
[UIView animateWithDuration:0.2
animations:^{
self.imageView.transform =
CGAffineTransformMakeRotation(endAngle);
}];Blocks によるアニメーションを使っている。beginAnimations
先日この件で Kyasu さんより情報提供があった。
kyasu says: 2011年8月18日19:43 >プルダウンしてアニメーションが起きる時に若干のひっかかりを感じた。 この件ですが、Blocksを使わないでbeginAnimations,commitAnimationsを 使うと動作がブロックされないようです。?...試してみよう。
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.2];
self.imageView.transform =
CGAffineTransformMakeRotation(endAngle);
[UIView commitAnimations];するとひっかかりが無くなってスムーズにアニメーションが動作するようになった。おお、これはいい。ただ、その一方で beginAnimations と blocks アニメーションで違いがある?という疑問が湧いてきた。
UIViewAnimationOptionAllowUserInteraction
違いはあった。
UIViewAnimationOptionAllowUserInteraction
普通の表示だけのアニメーションだと違いは無いのだが今回のようにユーザが操作(ドラッグ)している最中にアニメーションを動作させる場合、blocks アニメーションのデフォルト動作では今回のようにひっかかりが発生する。この場合は blocksアニメーションのオプションに UIViewAnimationOptionAllowUserInteraction を指定する必要がある。こんな感じ。
[UIView animateWithDuration:0.2
delay:0.0
options:UIViewAnimationOptionCurveLinear |
UIViewAnimationOptionAllowUserInteraction
animations:^{
self.imageView.transform =
CGAffineTransformMakeRotation(endAngle);
}
completion:NULL
];beginAnimations を使ったアニメーションではデフォルトでこのオプションと同等の動作になっているということなのだろう。ソースコード
改良版は GitHub からどうぞ。
CustomCellSample at 2011-08-29 from xcatsan/iOS-Sample-Code - GitHub
- - - -
この件はずっと気になっていたので解消されてうれしい。
Kyasu さん、情報提供ありがとうございました。




Responses
Leave a Response