[英]Navigation bar button item image color is different when design through xib of xcode5
[英]Xcode5 - toggle button border color automatically
我是IOS编程的新手。 我想在应用程序启动时自动切换按钮边框颜色以引起用户注意,我尝试了以下代码,但仅选择了最终颜色。
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelay:0.0];
[UIView setAnimationDuration:3.0];
button.layer.borderWidth=2.5f;
button.layer.borderColor=[[UIColor blueColor]CGColor];
[UIView setAnimationDelay:3.0];
button.layer.borderColor=[[UIColor clearColor]CGColor];
[UIView setAnimationDelay:6.0];
button.layer.borderColor=[[UIColor redColor]CGColor];
[UIView commitAnimations];
}
您正在执行的是链接动画的无效方法。 结果,仅应用最后的更改。 此外,您应该使用Apple从iOS 4开始就推荐的基于块的动画。应该是这样的:
[UIView animateWithDuration:3.0 animations:^{
button.layer.borderWidth=2.5f;
button.layer.borderColor=[[UIColor blueColor]CGColor];
} completion:^(BOOL finished) {
[UIView animateWithDuration:3.0 animations:^{
button.layer.borderColor=[[UIColor clearColor]CGColor];
} completion:^(BOOL finished) {
[UIView animateWithDuration:3.0 animations:^{
button.layer.borderColor=[[UIColor redColor]CGColor];
}];
}];
}];
该答案与0x7fffffff的答案相同,不同的是它使用块变量,因此看起来更简洁,希望更有意义:
void (^animateToRed)(BOOL finished) = ^(BOOL finished) {
[UIView animateWithDuration:3.0 animations:^{
button.layer.borderColor=[[UIColor redColor]CGColor];
} completion: nil];
}
void (^animateToClear)(BOOL finished) = ^(BOOL finished) {
[UIView animateWithDuration:3.0 animations:^{
button.layer.borderColor=[[UIColor clearColor]CGColor];
} completion:animateToRed];
}
[UIView animateWithDuration:3.0 animations:^{
button.layer.borderWidth=2.5f;
button.layer.borderColor=[[UIColor blueColor]CGColor];
} completion:animateToClear];
UIView
的animateWithDuration:animations:completion:
方法是随时间变化的动画的最佳方法。
它需要3个参数。
此代码段创建了两个完成块。
animateToRed
完成块将边框的动画设置为红色。 它的完成块为nil
,至此,我们已经完成了动画制作。
animateToClear
完成块处理要清除的边框的动画。 它的完成块是我们刚刚定义的animateToRed
。
最后,我们调用animateWithDuration
,将边框设置为蓝色,并将animateToClear
块传递给完成对象(依次调用animateToRed
块)。
对于这种简单且没有重复动画的动画,以这种方式进行操作似乎有点过头了(尽管它的可读性更高)。 但是,对于一系列更为复杂的动画,尤其是在存在重复性的情况下,创建像这样的块变量以供使用和快速传递变得非常有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.