我想要实现的是:拥有几个 UILabel 并能够依次突出显示它们,我的意思是我想改变它们的 textColor、fontSize 和位置。所以我已经成功编写了一个演示,它采用一组 UIView 并按顺序突出显示它们(更改它们的背景颜色)。
阅读了 Andrew Fuchs对执行多个顺序 UIView 动画的最佳方式?其中引用了没有嵌套块的多个 UIView 动画
然而,当我在我的代码中用 UILabel 代替 UIView 时,一切都崩溃了,而且它们没有动画,变化会立即发生。谁能告诉我我做错了什么?UILabel 是 UIView 的子类,所以我希望在 UIView 中工作的属性在 UILabel 中工作。在 Apple 的“iOS 视图编程指南 - 动画”中指出 backgroundColor 可以设置动画。所以我回到了基础,编写了一些测试代码,发现对于 UIView,我可以愉快地为其背景颜色、alpha 和中心位置设置动画。而 UILabel alpha 属性和中心位置可以成功设置动画,但 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 方法?或者最好的方法是什么?