<i id='PXPsx'><tr id='PXPsx'><dt id='PXPsx'><q id='PXPsx'><span id='PXPsx'><b id='PXPsx'><form id='PXPsx'><ins id='PXPsx'></ins><ul id='PXPsx'></ul><sub id='PXPsx'></sub></form><legend id='PXPsx'></legend><bdo id='PXPsx'><pre id='PXPsx'><center id='PXPsx'></center></pre></bdo></b><th id='PXPsx'></th></span></q></dt></tr></i><div id='PXPsx'><tfoot id='PXPsx'></tfoot><dl id='PXPsx'><fieldset id='PXPsx'></fieldset></dl></div>
    <tfoot id='PXPsx'></tfoot>

    <small id='PXPsx'></small><noframes id='PXPsx'>

    <legend id='PXPsx'><style id='PXPsx'><dir id='PXPsx'><q id='PXPsx'></q></dir></style></legend>
      <bdo id='PXPsx'></bdo><ul id='PXPsx'></ul>

        drawRect 圆和动画大小/颜色

        时间:2023-06-01
          <tfoot id='t2yxY'></tfoot>

          • <bdo id='t2yxY'></bdo><ul id='t2yxY'></ul>

            <i id='t2yxY'><tr id='t2yxY'><dt id='t2yxY'><q id='t2yxY'><span id='t2yxY'><b id='t2yxY'><form id='t2yxY'><ins id='t2yxY'></ins><ul id='t2yxY'></ul><sub id='t2yxY'></sub></form><legend id='t2yxY'></legend><bdo id='t2yxY'><pre id='t2yxY'><center id='t2yxY'></center></pre></bdo></b><th id='t2yxY'></th></span></q></dt></tr></i><div id='t2yxY'><tfoot id='t2yxY'></tfoot><dl id='t2yxY'><fieldset id='t2yxY'></fieldset></dl></div>

                  <small id='t2yxY'></small><noframes id='t2yxY'>

                    <tbody id='t2yxY'></tbody>
                  <legend id='t2yxY'><style id='t2yxY'><dir id='t2yxY'><q id='t2yxY'></q></dir></style></legend>
                  本文介绍了drawRect 圆和动画大小/颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我正在使用标准 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模板网!

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

                  相关文章

                  最新文章

                    <legend id='Vkc2q'><style id='Vkc2q'><dir id='Vkc2q'><q id='Vkc2q'></q></dir></style></legend>
                    1. <tfoot id='Vkc2q'></tfoot>

                    2. <i id='Vkc2q'><tr id='Vkc2q'><dt id='Vkc2q'><q id='Vkc2q'><span id='Vkc2q'><b id='Vkc2q'><form id='Vkc2q'><ins id='Vkc2q'></ins><ul id='Vkc2q'></ul><sub id='Vkc2q'></sub></form><legend id='Vkc2q'></legend><bdo id='Vkc2q'><pre id='Vkc2q'><center id='Vkc2q'></center></pre></bdo></b><th id='Vkc2q'></th></span></q></dt></tr></i><div id='Vkc2q'><tfoot id='Vkc2q'></tfoot><dl id='Vkc2q'><fieldset id='Vkc2q'></fieldset></dl></div>
                        <bdo id='Vkc2q'></bdo><ul id='Vkc2q'></ul>
                    3. <small id='Vkc2q'></small><noframes id='Vkc2q'>