我之前遇到过类似的问题,但我尝试的解决方案效果不佳。
以下是代码的一部分。首先,定义标签。
UILabel* _label;
...
下面是标签touchesBegan内的动画代码。所以基本上,当点击标签时,我想逐渐淡化背景图片并改变文字颜色。
_label.backgroundColor = [UIColor clearColor];
_label.textColor = [UIColor blackColor];
UIImage* image = [UIImage imageNamed:@"tile.png"];
CGSize imageSize = _label.frame.size;
UIGraphicsBeginImageContext(imageSize);
[image drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[UIView animateWithDuration:2.0
animations:^{
_label.backgroundColor = [UIColor colorWithPatternImage:newImage];
_label.textColor = [UIColor whiteColor];
[UIView setAnimationDidStopSelector:@selector(highlightEnded:finished:context:)];
}
completion:^(BOOL finished){
NSLog(@"stoped");
}];
图像背景和文字颜色改变正确,但没有动画;他们俩变化很快。
感谢您的提示。
答案 0 :(得分:0)
在动画文字时,您应该使用transitionWithView:duration:options:animations:completion:
代替animateWithDuration:
。
答案 1 :(得分:0)
在尝试了六个左右的建议之后,我发现一种保证工作的方法是添加额外的UIImageView作为背景(将标签的背景颜色设置为clearColor),然后通过其alpha动画。这和我能解决的一样好。
答案 2 :(得分:0)
您无法为文本颜色更改或背景图像更改设置动画。 你能做的是
image1.alpha = 0;
image2.alpha = 1;
[UIView animateWithDuration:.3 animations:^{
image1.alpha = 1;
image2.alpha = 0;
}];
这会给你一个淡化效果。 您可以手动插入image1和image2作为标签的子视图。
答案 3 :(得分:-1)
CATransition *animation = CATransition.animation;
animation.duration = 1.0;
animation.type = kCATransitionFade;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[viewToFade.layer addAnimation:animation forKey:@"fade"];
应该同时使用图像视图和标签。