2010-10-15 191 views
19

我有一个标签,在我的应用程序中弹出显示点。我使用下面的代码来使标签变大,然后变小。我也想动画颜色从紫色变为绿色。有人能指出我实现这个目标的资源吗?iPad:动画UILabels颜色变化

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]; 

回答

26

不幸的是,颜色不与UIView的动画动画。 对this question的回答在不使用Core Animation的情况下提供了一些很好的解决方法。

如果你不介意使用CATextLayer,而不是一个UILabel,那么颜色(和你的榜样缩放)可以是动画这样的:

#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"]; 
} 
+0

只要有将设置在CATextLayer – bioffe 2010-11-19 22:52:12

+4

垂直对齐的方式只用原来的UILabel自己这里有一个_much_更好的解决方法:HTTP://计算器.com/questions/6442774/is-uilabels-backgroundcolor-not-animatable/6442868#6442868 – Anna 2012-03-13 15:29:40

+2

@AnnaKarenina你指出的链接是动画背景颜色。 – xhan 2012-07-16 11:53:53

0

呼叫之前添加这commitAnimations

[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:mLabel cache:YES]; 
6

之所以文字颜色不能设置动画是UILabel使用常规CALayer而不是CATextLayer。

要使textColor具有动画效果(以及文本,字体等),我们可以继承UILabel并使其使用CATextLayer。

这是一个相当大量的工作,但幸运的是我已经做到了:-)

你可以找到一个完整的解释+在这个article

67

您可以在下拉式开源替代品的UILabel使用的iOS 4.0的观点转变的方法来做到这一点:

[UIView transitionWithView:statusLabel duration:0.25 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{ 
    [statusLabel setTextColor:[UIColor whiteColor]]; 
    [statusLabel setShadowColor:[UIColor blackColor]]; 
} completion:nil]; 
+0

哇!这很棒!感谢分享!对不起,感叹号,但我正要创建一个CATextLayer的子类。 – RileyE 2012-10-05 21:32:10

+0

这是一个很好的提示。被接受的答案在技术上可能更正确,但是这种方法实现的效果非常相似。非常感谢。 – LeffelMania 2013-01-08 06:51:29

+0

正确。使用'CATextLayer'是更正确的答案,因为它将插值from和颜色之间的颜色值。但是,当从一种颜色到另一种颜色的基本交叉渐变过渡足够时,这很方便。 – LucasTizma 2013-04-04 20:24:07

1

如果需要使用为突显或选择状态IB指定颜色,并希望与动画淡出的效果,你可以使用下面的代码的颜色变化:

斯威夫特:

@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 
     } 
    }); 

}

0

试试这个:

[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) 
     { 

     }]; 

}]; 
1

这行代码为我工作。确保设置选项以跨解散 斯威夫特:

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) 
    }