2010-10-15 100 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) 
    }