iPad: Анимация UILabels меняет цвет - PullRequest
19 голосов
/ 15 октября 2010

У меня есть ярлык, отображающий точки в моем приложении.Я использую следующий код, чтобы сделать метку больше в масштабе, а затем меньше.Я также хотел бы оживить изменение цвета от фиолетового до зеленого.Может кто-нибудь указать мне на ресурс в достижении этого?

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

Ответы [ 7 ]

67 голосов
/ 29 сентября 2012

Вы можете использовать метод перехода вида iOS 4.0, чтобы сделать это:

[UIView transitionWithView:statusLabel duration:0.25 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
    [statusLabel setTextColor:[UIColor whiteColor]];
    [statusLabel setShadowColor:[UIColor blackColor]];
} completion:nil];
27 голосов
/ 16 октября 2010

К сожалению, цвет не анимируется с анимацией UIView.Ответы на этот вопрос дают несколько хороших обходных путей без использования 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"];
}
6 голосов
/ 23 октября 2011

Причина, по которой textColor не является анимируемым, заключается в том, что UILabel использует обычный CALayer вместо CATextLayer.

Чтобы сделать анимируемым textColor (а также текст, шрифт и т. Д.), Мы можем создать подкласс UILabel и использовать для него CATextLayer.

Это довольно много работы, но, к счастью, я уже сделал это: -)

В этой статье

вы можете найти полное объяснение + замену UILabel с открытым исходным кодом.
1 голос
/ 26 июня 2015

Эта строка кода работает для меня. Обязательно установите параметры для перекрестного растворения Swift:

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)
  }
1 голос
/ 26 октября 2014

Если вам нужно использовать цвета IB, которые указаны для выделенных или выбранных состояний и хотите анимировать изменение цвета с эффектом затухания, вы можете использовать следующий код:

Swift:

@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 голосов
/ 05 ноября 2014

Попробуйте:

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

      }];

 }];
0 голосов
/ 16 октября 2010

Добавьте это перед вызовом на commitAnimations:

[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:mLabel cache:YES];
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...