drawRect circle and animate size/color(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 圆和动画大小/颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:drawRect 圆和动画大小/颜色
基础教程推荐
- 如何从 logcat 中删除旧数据? 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
