iPad:animationUILabels颜色变化
我有一个标签,在我的应用程序中popup显示点。 我使用下面的代码来使标签变大,然后变小。 我也想animation颜色从紫色变为绿色。 有人能指出我的资源来实现这一目标吗?
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];
不幸的是,颜色不与UIViewanimationanimation。 这个问题的答案给出了一些很好的解决方法,而不使用核心animation。
如果你不介意使用CATextLayer而不是UILabel,那么颜色(和你的例子中的缩放比例)就可以像这样animation:
#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"]; }
你可以使用iOS 4.0的视图转换方法来做到这一点:
[UIView transitionWithView:statusLabel duration:0.25 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{ [statusLabel setTextColor:[UIColor whiteColor]]; [statusLabel setShadowColor:[UIColor blackColor]]; } completion:nil];
textColor不具有animation效果的原因是UILabel使用常规的CALayer而不是CATextLayer。
为了使textColor具有animation性(以及文本,字体等),我们可以inheritanceUILabel并使其使用CATextLayer。
这是相当多的工作,但幸运的是我已经做到了:-)
你可以在本文中find一个完整的解释+ UILabel的一个开源替代品
如果您需要使用为突出显示或选定状态指定的IB颜色,并希望通过淡出效果设置animation的颜色变化,则可以使用下一个代码:
迅速:
@IBAction func buttonAction(sender: AnyObject) { // Set selected to leave same color as for highlighted // (default=black, highlighted=red, selected=red) self.openAllButton.selected = true // Delay before the color change animation let delayInSeconds = 0.1; let delay = delayInSeconds * Double(NSEC_PER_SEC) let popTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delay)); dispatch_after(popTime, dispatch_get_main_queue(), { // Animate color change UIView.transitionWithView(self.openAllButton, duration: 1.0, options: UIViewAnimationOptions.TransitionCrossDissolve, animations: { () -> Void in self.openAllButton.selected = false // selected->default }) { (v:Bool) -> Void in } });
}
这行代码适合我。 确保将选项设置为交叉parsingSwift:
static func animationButtonTextColor(button : UIButton, toColor : UIColor, duration : NSTimeInterval) { UIView.transitionWithView(button, duration: duration, options: UIViewAnimationOptions.TransitionCrossDissolve, animations: { button.setTitleColor(toColor, forState: .Normal) }, completion: nil) }
在调用commitAnimations
之前添加这个:
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:mLabel cache:YES];
尝试这个:
[UIView animateWithDuration:0.5 animations:^{ label.alpha=0.3; }completion:^(BOOL finished) { label.backgroundcolor=[uicolor purplecolor]; [UIView animateWithDuration:0.5 animations:^{ label.alpha=1.0; }completion:^(BOOL finished) { }]; }];