drawRect 圆和动画大小/颜色

时间:2023-03-24
本文介绍了drawRect 圆和动画大小/颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在使用标准 CGContextFillEllipseInRect() 代码在 UIView-drawRect: 方法中画一个圆.但是,我想稍微脉冲(变大和变小)并用动画改变颜色填充的强度.例如,如果圆圈充满红色,我想给圆圈打脉冲,并随着脉冲动作及时使红色稍微变亮和变暗.我对 Core Animation 没有太多经验,我对如何做到这一点有点迷茫,所以任何帮助都将不胜感激.

I am drawing a circle in the -drawRect: method of my UIView using the standard CGContextFillEllipseInRect() code. However, I would like to slightly pulse (make larger and smaller) and change the intensity of the color fill with an animation. For example, if the circle is filled with red I would like to pulse the circle and make the red slightly lighter and darker in-time with the pulsing action. Not having much experience with Core Animation I am a bit lost about how to do this, so any help would be greatly appreciated.

推荐答案

如果你不在drawRect:中画圆,这个就简单多了.相反,将您的视图设置为使用 CAShapeLayer,如下所示:

This is much simpler if you don't draw the circle in drawRect:. Instead, set up your view to use a CAShapeLayer, like this:

@implementation PulseView

+ (Class)layerClass {
    return [CAShapeLayer class];
}

每当视图大小发生变化时(包括首次出现时),系统都会将 layoutSubviews 发送到您的视图.我们重写 layoutSubviews 来设置形状并为其设置动画:

The system sends layoutSubviews to your view whenever it changes size (including when it first appears). We override layoutSubviews to set up the shape and animate it:

- (void)layoutSubviews {
    [self setLayerProperties];
    [self attachAnimations];
}

以下是我们如何设置图层的路径(决定其形状)和形状的填充颜色:

Here's how we set the layer's path (which determines its shape) and the fill color for the shape:

- (void)setLayerProperties {
    CAShapeLayer *layer = (CAShapeLayer *)self.layer;
    layer.path = [UIBezierPath bezierPathWithOvalInRect:self.bounds].CGPath;
    layer.fillColor = [UIColor colorWithHue:0 saturation:1 brightness:.8 alpha:1].CGColor;
}

我们需要给图层附加两个动画——一个用于路径,一个用于填充颜色:

We need to attach two animations to the layer - one for the path and one for the fill color:

- (void)attachAnimations {
    [self attachPathAnimation];
    [self attachColorAnimation];
}

以下是我们为图层路径设置动画的方式:

Here's how we animate the layer's path:

- (void)attachPathAnimation {
    CABasicAnimation *animation = [self animationWithKeyPath:@"path"];
    animation.toValue = (__bridge id)[UIBezierPath bezierPathWithOvalInRect:CGRectInset(self.bounds, 4, 4)].CGPath;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [self.layer addAnimation:animation forKey:animation.keyPath];
}

下面是我们如何为图层的填充颜色设置动画:

Here's how we animate the layer's fill color:

- (void)attachColorAnimation {
    CABasicAnimation *animation = [self animationWithKeyPath:@"fillColor"];
    animation.fromValue = (__bridge id)[UIColor colorWithHue:0 saturation:.9 brightness:.9 alpha:1].CGColor;
    [self.layer addAnimation:animation forKey:animation.keyPath];
}

这两个 attach*Animation 方法都使用了一个辅助方法,该方法创建一个基本动画并将其设置为通过自动反转和一秒的持续时间无限期重复:

Both of the attach*Animation methods use a helper method that creates a basic animation and sets it up to repeat indefinitely with autoreverse and a one second duration:

- (CABasicAnimation *)animationWithKeyPath:(NSString *)keyPath {
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:keyPath];
    animation.autoreverses = YES;
    animation.repeatCount = HUGE_VALF;
    animation.duration = 1;
    return animation;
}

这篇关于drawRect 圆和动画大小/颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:NSManagedObjectContext 的 performBlock: 是做什么用的? 下一篇:UIProgressView 和自定义跟踪和进度图像(iOS 5 属性)

相关文章