0

我有一个UIView使用以下代码在其中绘制一个圆圈:

-(void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, myColor);
    CGContextFillEllipseInRest(context, myRect);
}

我想把这个圆圈动画成灰色......我怎样才能做到这一点?

-(void)fadeToGrey {
    // What goes here?
}
4

1 回答 1

2

您应该使用 CAShapeLayer 作为图层类并在setLayerProperties:方法内进行绘图。drawRect:不会被覆盖。这是我的示例代码,其中包含带有附加颜色动画方法的自定义 UIView 注释。

//Returns the class used to create the layer for instances of this class.
//it is CALayer class by default.
+ (Class)layerClass 
{
   return [CAShapeLayer class];
}

//Lays out subviews.
- (void)layoutSubviews
{
   [self setLayerProperties];
}

- (void)setLayerProperties {
//The view’s Core Animation layer used for rendering.
  CAShapeLayer *layer = (CAShapeLayer *)self.layer;
// Color Declarations
  UIColor* strokeColor = [UIColor redColor];
// Some sample Bezier Drawing. In my case it is a triangle.
// However, you can draw circle by using `+bezierPathWithRoundedRect:cornerRadius:`
   UIBezierPath* bezierPath = [UIBezierPath bezierPath];
   [bezierPath moveToPoint: CGPointMake(0.0, self.frame.size.height)];
   [bezierPath addLineToPoint: CGPointMake(self.frame.size.width/2, 0.0)];
   [bezierPath addLineToPoint: CGPointMake(CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
   [bezierPath addLineToPoint: CGPointMake(0.0, CGRectGetHeight(self.frame))];
    bezierPath.miterLimit = 8;

    bezierPath.lineJoinStyle = kCGLineJoinBevel;
    layer.path = bezierPath.CGPath;
    layer.fillColor = strokeColor.CGColor;
}

//here is the magic :)
- (void)attachColorAnimationToColor:(UIColor *)color {

//"fillColor" property of CAShapeLayer's instance is animatable 
//(thus we have overridden `layerClass` method btw), that leads us to create 
//CABasicAnimation instance with fromValue (initial color), toValue (final color).

   CABasicAnimation *animation = [self animationWithKeyPath:@"fillColor"];
   animation.fromValue = (__bridge id)(((CAShapeLayer *)self.layer).fillColor);
   animation.toValue = (__bridge id)color.CGColor;
   [self.layer addAnimation:animation forKey:animation.keyPath];
}

//setting properties of color animation
- (CABasicAnimation *)animationWithKeyPath:(NSString *)keyPath {
   CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:keyPath];
   animation.fillMode = kCAFillModeForwards;
   animation.removedOnCompletion=NO;
   animation.repeatCount = 1;
   animation.duration = 0.3f;
   return animation;
 }

您将通过如下调用来使用它attachColorAnimationToColor:

//your custom view declaration
YourView *yourView = [[YourView alloc]initWithFrame:CGRectMake(...)];
[self.view addSubview:yourView];
[yourView attachColorAnimationToColor:[UIColor lightGrayColor]];

对 fillColor 属性的 CAShape 图层类参考

随意询问是否有问题。

更新 我们的图层填充颜色是红色的。因此,如果您将动画触发为 redColor,则意味着您希望动画从“红色”变为“红色”。创建方法反向动画,其中 toValue 是我们形状的描边颜色。(动画不会更新它,它总是保持红色)。这是现在测试的样本:)

-(void)attachReverseAnimationFromColor:(UIColor*)color{
    CABasicAnimation *animation = [self animationWithKeyPath:@"fillColor"];
    animation.toValue = (__bridge id)(((CAShapeLayer *)self.layer).fillColor);
    animation.fromValue = (__bridge id)color.CGColor;
    [self.layer addAnimation:animation forKey:animation.keyPath];

}

PS快乐编码。

于 2013-11-07T13:59:11.767 回答