iPad的:动画UILabels变色 [英] iPad: Animate UILabels color changing
问题描述
我有一个弹出我的应用程序中显示点的标签。我使用下面的code,使标签得到较大规模,再小一点。我也想动画的颜色变化由紫色变为绿色。可有人点我的资源在实现这一目标?
mLabel.textColor = [的UIColor purpleColor] [UIView的beginAnimations:无背景:NULL];
[UIView的setAnimationDuration:1.0];
[UIView的setAnimationDelegate:个体经营];
mLabel.transform = CGAffineTransformMakeScale(1.5,1.5);
[UIView的setAnimationRepeatCount:1];
mLabel.transform = CGAffineTransformMakeScale(0.5,0.5);
mLabel.textColor =的UIColor绿彩];
[UIView的commitAnimations];
不幸的是,颜色不与UIView的动画制作动画。
答案<一个href=\"http://stackoverflow.com/questions/2426614/how-to-animate-the-textcolor-property-of-an-uilabel\">this问题给一些很好的解决方法,而无需使用核心动画。
如果你不介意使用CATextLayer,而不是一个UILabel,那么颜色(和你的榜样缩放),可以是动画是这样的:
#进口&LT; QuartzCore / QuartzCore.h&GT;
//还需要添加QuartzCore框架项目(以及进口)。
//
- (无效){animateTextLayerCGFloat的animationDuration = 5;CATextLayer * textLayer = [CATextLayer层]
[textLayer了setString:@Hello World的]。
[textLayer setForegroundColor:[的UIColor purpleColor] .CGColor]
[textLayer使用SetFontSize:30];
[textLayer SETFRAME:CGRectMake(20,200,300,40)];
[self.view层] addSublayer:textLayer];CABasicAnimation * colorAnimation = [CABasicAnimation
animationWithKeyPath:@foregroundColor];
colorAnimation.duration = animationDuration;
colorAnimation.fillMode = kCAFillModeForwards;
colorAnimation.removedOnCompletion = NO;
colorAnimation.fromValue =(ID)的UIColor purpleColor] .CGColor;
colorAnimation.toValue =(ID)的UIColor绿彩] .CGColor;
colorAnimation.timingFunction = [CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionLinear];CAKeyframeAnimation * scaleAnimation = [CAKeyframeAnimation
animationWithKeyPath:@改造];
NSArray的* scaleValues = [NSArray的arrayWithObjects:
[NSValue valueWithCATransform3D:CATransform3DScale(textLayer.transform,1,1,1)],
[NSValue valueWithCATransform3D:CATransform3DScale(textLayer.transform,1.5,1.5,1)],
[NSValue valueWithCATransform3D:CATransform3DScale(textLayer.transform,0.5,0.5,1)],零];
[scaleAnimation setValues方法:scaleValues];
scaleAnimation.fillMode = kCAFillModeForwards;
scaleAnimation.removedOnCompletion = NO;CAAnimationGroup * animationGroup = [CAAnimationGroup动画]
animationGroup.duration = animationDuration;
animationGroup.timingFunction = [CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionLinear];
animationGroup.fillMode = kCAFillModeForwards;
animationGroup.removedOnCompletion = NO;
animationGroup.animations =
[NSArray的arrayWithObjects:colorAnimation,scaleAnimation,零][textLayer addAnimation:animationGroup forKey:@animateColorAndScale];
}
I have a label which pops up displaying points in my application. I am using the following code to make the label get larger in scale, then smaller. I would also like to animate the color changing from purple to green. Can someone point me to a resource in achieving this?
mLabel.textColor = [UIColor purpleColor];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[UIView setAnimationDelegate:self];
mLabel.transform = CGAffineTransformMakeScale(1.5,1.5);
[UIView setAnimationRepeatCount:1];
mLabel.transform = CGAffineTransformMakeScale(0.5,0.5);
mLabel.textColor = [UIColor greenColor];
[UIView commitAnimations];
Unfortunately, the color doesn't animate with UIView animations. The answers to this question give some good workarounds without using Core Animation.
If you don't mind using a CATextLayer instead of a UILabel, then the color (and the scaling in your example) can be animated like this:
#import <QuartzCore/QuartzCore.h>
//Also need to add QuartzCore framework to project (as well as the import).
//
-(void)animateTextLayer {
CGFloat animationDuration = 5;
CATextLayer *textLayer = [CATextLayer layer];
[textLayer setString:@"Hello World"];
[textLayer setForegroundColor:[UIColor purpleColor].CGColor];
[textLayer setFontSize:30];
[textLayer setFrame:CGRectMake(20, 200, 300, 40)];
[[self.view layer] addSublayer:textLayer];
CABasicAnimation *colorAnimation = [CABasicAnimation
animationWithKeyPath:@"foregroundColor"];
colorAnimation.duration = animationDuration;
colorAnimation.fillMode = kCAFillModeForwards;
colorAnimation.removedOnCompletion = NO;
colorAnimation.fromValue = (id)[UIColor purpleColor].CGColor;
colorAnimation.toValue = (id)[UIColor greenColor].CGColor;
colorAnimation.timingFunction = [CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionLinear];
CAKeyframeAnimation *scaleAnimation = [CAKeyframeAnimation
animationWithKeyPath:@"transform"];
NSArray *scaleValues = [NSArray arrayWithObjects:
[NSValue valueWithCATransform3D:CATransform3DScale(textLayer.transform, 1, 1, 1)],
[NSValue valueWithCATransform3D:CATransform3DScale(textLayer.transform, 1.5, 1.5, 1)],
[NSValue valueWithCATransform3D:CATransform3DScale(textLayer.transform, 0.5, 0.5, 1)], nil];
[scaleAnimation setValues:scaleValues];
scaleAnimation.fillMode = kCAFillModeForwards;
scaleAnimation.removedOnCompletion = NO;
CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
animationGroup.duration = animationDuration;
animationGroup.timingFunction = [CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionLinear];
animationGroup.fillMode = kCAFillModeForwards;
animationGroup.removedOnCompletion = NO;
animationGroup.animations =
[NSArray arrayWithObjects:colorAnimation, scaleAnimation, nil];
[textLayer addAnimation:animationGroup forKey:@"animateColorAndScale"];
}
这篇关于iPad的:动画UILabels变色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!