drawRect圆和animation大小/颜色
我使用标准的CGContextFillEllipseInRect()
代码在我的UIView
的-drawRect:
方法中绘制一个圆。 但是,我想稍微脉动(做得越来越小),用animation改变颜色填充的强度。 例如,如果圆圈充满了红色,我想脉动这个圆圈,并且使得红色在脉冲动作中及时变得更亮和更暗。 对Core Animation没有太多的经验我对如何做到这一点有点失落,所以任何帮助将不胜感激。
如果您不在drawRect:
绘制圆,这将简单得多drawRect:
相反,设置您的视图使用CAShapeLayer
,如下所示:
@implementation PulseView + (Class)layerClass { return [CAShapeLayer class]; }
只要系统更改大小(包括首次出现时),系统就会将layoutSubviews
发送到您的视图。 我们重写layoutSubviews
来设置形状并对其进行animation处理:
- (void)layoutSubviews { [self setLayerProperties]; [self attachAnimations]; }
以下是我们如何设置图层的path(决定其形状)和形状的填充颜色:
- (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; }
我们需要在图层上添加两个animation – 一个用于path,一个用于填充颜色:
- (void)attachAnimations { [self attachPathAnimation]; [self attachColorAnimation]; }
以下是我们如何animation层的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]; }
以下是我们如何animation图层的填充颜色:
- (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
方法都使用了一个辅助方法来创build一个基本的animation,并将其设置为无限期地用自动反向和一秒持续时间重复:
- (CABasicAnimation *)animationWithKeyPath:(NSString *)keyPath { CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:keyPath]; animation.autoreverses = YES; animation.repeatCount = HUGE_VALF; animation.duration = 1; return animation; }
- 将imageNamed转换为imageWithContentsOfFile:
- 从下往上移动UIView
- 如何animationCAShapeLayerpath和fillColor
- launchscreen.storyboard和main.storyboard有什么不同?
- 在核心animation或核心graphics上创build简单的橡皮擦效果有多困难?
- UICollectionView布局更改的默认animation持续时间是多less?
- 为什么当我为我的CABasicAnimation设置一个较低的持续时间值时,它会跳转吗?
- IOS – 多个图像缓慢向上的animation
- 从下到上animationUIView高度