問題描述
我正在使用標準 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 圓和動畫大小/顏色的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!