Ios 更改UIView的foregroundColor

Ios 更改UIView的foregroundColor,ios,iphone,objective-c,Ios,Iphone,Objective C,有没有办法更改UIView的前景色颜色 我正在使用UIView(drawRect)创建一个箭头,如“>”,背景清晰,只有两条黑线。这很有效。但后来我想把“>”的颜色从黑色改为红色,例如。最好有一个动画,比如从黑色到红色的渐变,使用CAKeyframeAnimation。我可以为borderColor和backgroundColor做,但这些属性不是我要找的 我正在使用此动画块更改另一个UIView的边框颜色。我想对foregroundColor也这么做,但它在iOS中不起作用 CAKeyfram

有没有办法更改UIView的前景色颜色

我正在使用UIView(drawRect)创建一个箭头,如“>”,背景清晰,只有两条黑线。这很有效。但后来我想把“>”的颜色从黑色改为红色,例如。最好有一个动画,比如从黑色到红色的渐变,使用CAKeyframeAnimation。我可以为borderColor和backgroundColor做,但这些属性不是我要找的

我正在使用此动画块更改另一个UIView的边框颜色。我想对foregroundColor也这么做,但它在iOS中不起作用

CAKeyframeAnimation* colorAnim = [CAKeyframeAnimation animationWithKeyPath:@"borderColor"];
NSArray* colorValues = [NSArray arrayWithObjects:(id)[UIColor blackColor].CGColor,(id)[UIColor redColor].CGColor,  nil];
colorAnim.values = colorValues;
colorAnim.calculationMode = kCAAnimationPaced;
colorAnim.duration = 5.0;
colorAnim.repeatCount = 1;
[self.myView.layer addAnimation:colorAnim forKey:@"borderColor"];

谢谢你的帮助,谢谢

UIView类没有
foregroundColor
属性,因此您必须自己实现它。在UIView子类接口中,定义属性:

@property (nonatomic, strong) UIColor *foregroundColor;
在子类实现中,重写
setForegroundColor:
setter方法:

- (void)setForegroundColor:(UIColor *)newForegoundColor
{
    _foregroundColor = newForegroundColor;
    [self setNeedsDisplay]; // This is need so that drawRect: is called
}
在子类中使用的任何初始化器中,将
foregroundColor
属性设置为默认值:

self.foregroundColor = [UIColor blackColor];
drawRect:
实现中,使用
foregroundColor
属性来笔划正在绘制的V形:

CGContextSetStrokeColorWithColor(context, self.foregroundColor.CGColor);

嗨,首先,非常感谢你的回答。我做了你建议的一切,新属性工作得很好,但是动画没有改变子视图的颜色。我是这样打电话的:
CAKeyframeAnimation*colorAnim2=[CAKeyframeAnimation animationWithKeyPath:@“foregroundColor”]
colorAnim2.values=颜色值
colorAnim2.calculationMode=kCAAnimationPaced
colorAnim2.duration=5.0
colorAnim2.repeatCount=1
[self.balloodtriangleorder.layer addAnimation:colorAnim forKey:@“foregroundColor”]
要使自定义特性可设置动画,必须在
CALayer
子类和中绘制。