动画UIButton文本颜色

时间:2012-07-15 03:36:05

标签: ios animation uibutton

有没有办法为UIButton的textColor属性设置动画?我找到了一些UILabel文本颜色动画的解决方案,但是UIButton还没有看到任何解决方案。

6 个答案:

答案 0 :(得分:16)

使用视图转换

[UIView transitionWithView:backButton
                          duration:0.5f
                           options:UIViewAnimationOptionTransitionCrossDissolve
                        animations:^{
                            [backButton setTitleColor:[_peacock lighten:d.primary] forState:UIControlStateNormal];
                        }
                        completion:^(BOOL finished){
                        }];

答案 1 :(得分:6)

NSTimer绝对不适合作为动画工具,也不应该用于需要精确度的一般计时(帧率)。这个blog post完美地说明了我对如何处理不可动画的UILabel属性的立场,这些属性应该通过CA而不是NSTimer发送到渲染服务器。您可以使用或包装CATextLayer而不是UILabel,并在标准动画块中为其foregroundColor属性设置动画。

答案 2 :(得分:1)

回答非常好here。 它基本上使用这个方便的人:

[UIView transitionWithView:duration:options:animations:^()completion:^()]; 

答案 3 :(得分:0)

显然你需要使用

[UIButton setTitleColor:[UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:1.0] forState:UIControlStateNormal];

至于实际让它动画并随时间改变颜色,你需要创建一个NSTimer实例并使用@selector参数指向一个特定的方法来运行。每次计时器运行时,它都会触发该方法,然后更改UIButton的颜色。

 [NSTimer scheduledTimerWithTimeInterval:2.0f
         target:self
     selector:@selector(updateCounter:)
     userInfo:nil
     repeats:YES];

同时退房: http://servin.com/iphone/iPhone-Timers-and-Animated-Views.html

答案 4 :(得分:0)

您可以使用两个位于相同位置的不同颜色的按钮(或标签等):

        byte[] buffer = new byte[Request.InputStream.Length];
        Request.InputStream.Read(buffer, 0, buffer.Length);

        string data = Encoding.Default.GetString(buffer);


        string[] tokens = data.Split(',');
        if (tokens.Length > 1)
        {
            // not sure what I'm doing, trying stuff.  BOOOOM!
            image = Convert.FromBase64String(tokens[1]);
            base64 = Convert.ToBase64String(image);

            //string fileName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".png";
            //string path = Path.Combine(@"D:\www\images", fileName);
            //File.WriteAllBytes(path, image);
        }

之后,您可以通过button2的过渡动画来获取颜色动画:

button1 = UIButton(frame: frame)
button2 = UIButton(frame: frame) // same frame

button1.setTitleColor(UIColor.redColor(), forState: .Normal) // red
button2.setTitleColor(UIColor.blueColor(), forState: .Normal) // blue

答案 5 :(得分:-2)

这不是一个真正的动画,但它会起作用。

创建一个计时器,其频率与您想要的颜色一样。

让计时器每次触发时调用一个方法changeColorOnButton

[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(changeColorOnButton:) userInfo:nil repeats:YES];

创建一个颜色数组colorsArray(或者提前排列颜色,或者将数组添加到数组中并随机化它们。创建一个int,intForColorInArray

在changeColorOnButton方法中,执行以下操作:

- (void) changeColorOnButton:(UIButton *) button {

     UIColor *nextColor = [colorsArray objectAtIndex:intForColorArray];
     [button.titleLabel.setTextColor:nextColor];
     intForColorArray = (intForColorArray +1) % [colorsArray count];
}
相关问题