简体   繁体   English

如何使用animateWithDuration淡化标签的背景色

[英]How to fade a label's background color with animateWithDuration

I'm trying to fade a label's background color by using animateWithDuration, but my code isn't working out. 我正在尝试使用animateWithDuration淡化标签的背景色,但是我的代码无法正常工作。 Here's what I've got: 这是我得到的:

.h (in @interface...) .h(在@interface ...中)

IBOutlet UILabel *labelColor;

.m (in viewDidLoad method) .m(在viewDidLoad方法中)

[labelColor setBackgroundColor:[UIColor colorWithRed:55/255.0 green:191/255.0 blue:122/255.0 alpha:0.3]];

if (labelColor.alpha >= 0.3) {
    [UIView animateWithDuration:1 animations:^{
        labelColor.alpha = 0.3;
        labelColor.alpha = 1.0;
    }];
} else if (labelColor.alpha == 1.0) {
    labelColor.alpha = 0.3;
}

The color shows up at 0.3 alpha, but doesn't fade from 0.3 to 1.0. 颜色显示为0.3 alpha,但不会从0.3渐变为1.0。 I'm trying to make it so the label's color fades from 0.3 to 1.0 in a continuous loop, resetting the alpha to 0.3 as the it hits 1.0. 我正在尝试使标签的颜色连续不断地从0.3渐变为1.0,并在达到1.0时将alpha重置为0.3。

Any help on how to achieve this is appreciated. 感谢您提供有关如何实现此目标的任何帮助。

There are a few problems with your current code: 您当前的代码存在一些问题:

(1) Your else statement will never be called since if labelColor.alpha == 1.0 it will also be >= .3 (1)您的else语句将永远不会被调用,因为如果labelColor.alpha == 1.0,它也会> = .3

(2) Having both labelColor.alpha = 0.3; (2)同时具有labelColor.alpha = 0.3; and labelColor.alpha = 1.0; labelColor.alpha = 1.0; within your animation block means that only the second line (ie labelColor.alpha = 1.0; ) will dictate the animation 在动画块中意味着只有第二行(即labelColor.alpha = 1.0; )将决定动画

(3) You haven't set up a loop as you'd like (3)您尚未按照需要设置循环

(4) You haven't animated the fade out (4)您尚未为淡出设置动画

If I'm in fact understanding what it is you're trying to accomplish, you could continuously fade the entire UILabel in and out like so: 如果我实际上了解了您要完成的工作,则可以像这样继续淡入淡出整个UILabel

- (void)performLabelFade {
    // If the label is faded out, fade it in
    if (labelColor.alpha == .3) {
        [UIView animateWithDuration:1 animations:^{
            labelColor.alpha = 1.0;
        } completion:^(BOOL finished) {
            // Repeat the current method (i.e. like a loop)
            [self performLabelFade];
        }
    }
    // Else if the label is faded in, fade it out
    else if (labelColor.alpha == 1.0) {
        [UIView animateWithDuration:1 animations:^{
            labelColor.alpha = 0.3;
        } completion:^(BOOL finished) {
            // Repeat the current method (i.e. like a loop)
            [self performLabelFade];
        }
    }
}

You should set your initial conditions outside of the animation block. 您应该在动画块之外设置初始条件。

labelColor.alpha = 0.3;
[UIView animateWithDuration:1 animations:^{
    labelColor.alpha = 1.0;
}];

Side note : This will fade the alpha value of the entire UILabel , not just the background colour. 旁注 :这将淡化整个UILabel的alpha值,而不仅仅是背景色。 If you want the text to stick around while your background fades, you should be using something more along these lines: 如果您想让文本在背景渐淡时停留在周围,则应该在以下几行中使用更多的东西:

labelColor.backgroundColor = [UIColor colorWithRed:55/255.0 green:191/255.0 blue:122/255.0 alpha:0.3];
[UIView animateWithDuration:1 animations:^{
    labelColor.backgroundColor = [UIColor colorWithRed:55/255.0 green:191/255.0 blue:122/255.0 alpha:1.0];
}];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM