私が達成したいのは、いくつかの UILabels を持ち、それらを順番に強調表示できるようにすることです。つまり、textColor、fontSize、および位置を変更したいと考えています。したがって、UIView のグループを取り、それらを強調表示する (backgroundColor を変更する) デモを作成することに成功しました。
いくつかの連続した UIView アニメーションを実行する最良の方法という質問に対する Andrew Fuchs の回答を読んだことがありますか? ネストされたブロックのない複数の UIView アニメーションを参照した
ただし、コードで UIView を UILabel に置き換えると、すべてが失敗し、アニメーションがなくなり、変更が即座に行われます。誰が私が間違っているのか教えてもらえますか? UILabel は UIView のサブクラスであるため、UIView で機能するプロパティが UILabel で機能すると予想していました。Apple の「View Programming Guide for iOS - Animation」では、backgroundColor をアニメーション化できると記載されています。そこで、基本に戻ってテスト コードを書き、UIView の backgroundColor、alpha、および中心位置をうまくアニメーション化できることを発見しました。UILabel アルファ プロパティと中心位置は正常にアニメーション化できますが、backgroundColor と textColor はアニメーション化できません。参照: GitHub AnimateSequentially
- (void)viewDidLoad {
[super viewDidLoad];
duration = 2.0;
yOffset = 100.0;
normalColor = [UIColor lightGrayColor];
highlightedColor = [UIColor greenColor];
view1StartPoint = view1.center;
view2StartPoint = view2.center;
lable1StartPoint = label1.center;
lable2StartPoint = label2.center;
[self resetEverything:nil];
}
- (IBAction)animateViews:(id)sender
{
[UIView animateWithDuration:duration
animations:^{
[view1 setBackgroundColor:highlightedColor]; // animates correctly
// [view1 setAlpha:0.2]; // animates correctly
CGPoint p = view1.center;
p.y -= yOffset;
view1.center = p; // animates correctly
}
completion:^(BOOL finished){
[UIView animateWithDuration:duration
animations:^{
[view2 setBackgroundColor:highlightedColor];
// [view2 setAlpha:0.2];
CGPoint p = view2.center;
p.y -= yOffset;
view2.center = p; // animates correctly
}
completion:nil];
}
];
}
- (IBAction)animateLabelsSimple:(id)sender // does NOT works sequentially
{
[UIView animateWithDuration:duration
animations:^{
[label1 setBackgroundColor:highlightedColor]; // changes instantly
// [label1 setTextColor:[UIColor orangeColor]]; // changes instantly
// [label1 setAlpha:0.2]; // animates correctly
CGPoint p = label1.center;
p.y -= yOffset;
label1.center = p; // animates correctly
}
completion:^(BOOL finished){
[UIView animateWithDuration:duration
animations:^{
[label2 setBackgroundColor:highlightedColor];
// [label2 setTextColor:[UIColor orangeColor]];
// [label2 setAlpha:0.2];
CGPoint p = label2.center;
p.y -= yOffset;
label2.center = p;
}
completion:nil];
}
];
}
- (IBAction)resetEverything:(id)sender
{
[view1 setBackgroundColor:normalColor];
[view2 setBackgroundColor:normalColor];
[label1 setBackgroundColor:normalColor];
[label2 setBackgroundColor:normalColor];
[view1 setAlpha:1.0];
[view2 setAlpha:1.0];
[label1 setAlpha:1.0];
[label2 setAlpha:1.0];
[label1 setTextColor:[UIColor blackColor]];
[label2 setTextColor:[UIColor blackColor]];
view1.center = view1StartPoint;
view2.center = view2StartPoint;
label1.center = lable1StartPoint;
label2.center = lable2StartPoint;
}
私はGitHub Chameleonプロジェクトを見て、UIViewクラスメソッド「animateWithDuration」がおそらくどのように実装されているかを見ました:
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
{
[self _beginAnimationsWithOptions:options | UIViewAnimationOptionTransitionNone];
[self setAnimationDuration:duration];
[self setAnimationDelay:delay];
[self _setAnimationCompletionBlock:completion];
animations();
[self commitAnimations];
}
UILabel をサブクラス化し、animateWithDuration メソッドをオーバーライドする必要がありますか? または、最善の方法は何ですか?