如何使用CAKeyframeAnimation为CoreGraphics绘制形状的动画

Cor*_*les 7 core-animation core-graphics uiview cakeyframeanimation

我试图在UIView子类中为UIBeizerPath(在我的示例中为三角形)绘制动画.但是,整个子视图是动画而不是形状.

动画中是否有我遗漏的东西?

- (void)drawRect:(CGRect)rect {

   CAShapeLayer *drawLayer = [CAShapeLayer layer];

   drawLayer.frame           = CGRectMake(0, 0, 100, 100);
   drawLayer.strokeColor     = [UIColor greenColor].CGColor;
   drawLayer.lineWidth       = 4.0;

   [self.layer addSublayer:drawLayer];

   UIBezierPath *path = [UIBezierPath bezierPath];

  [path moveToPoint:CGPointMake(0,0)];
  [path addLineToPoint:CGPointMake(50,100)];
  [path addLineToPoint:CGPointMake(100,0)];
  [path closePath];

  CGPoint center = [self convertPoint:self.center fromView:nil];

  [path applyTransform:CGAffineTransformMakeTranslation(center.x, center.y)];

  [[UIColor redColor] set];

  [path fill];

  [[UIColor blueColor] setStroke];

  path.lineWidth = 3.0f;

  [path stroke];

  CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];

  pathAnimation.duration        = 4.0f;
  pathAnimation.path            = path.CGPath;
  pathAnimation.calculationMode = kCAAnimationLinear;

  [drawLayer addAnimation:pathAnimation forKey:@"position"];
}
Run Code Online (Sandbox Code Playgroud)

Kur*_*vis 17

  • 你正在创建一个CAShapeLayer,但后来没有做任何有用的事情.我们来解决这个问题.

  • 不要设置图层和动画-drawRect:,因为这严格意味着使用CoreGraphics或UIKit API进行绘图的时间.相反,您希望CAShapeLayer绘制三角形 - 这样您就可以为其设置动画.

  • CAKeyframeAnimation.path用于完全不同的东西(例如沿路径移动图层).

  • 您的动画正在position为图层的值设置动画.毫不奇怪,它移动了图层!您想要为该path值设置动画.

  • CAKeyframeAnimation背后的想法是你提供一个values设置图层属性的数组.在关键帧之间的时间内,它将在两个相邻的关键帧之间进行插值.所以你需要给它几条路径 - 每条路一条.

  • 插入任意路径很困难.当路径具有相同数量和类型的元素时,CA的路径插值效果最佳.因此,我们确保所有路径都具有相同的结构,只有一些点在彼此之上.

  • 动画的秘诀,也许是计算机的秘诀:你必须准确地解释你想要发生的事情."我想为每个点的绘制设置动画,因此它似乎是动画的"并不是足够的信息.

这是一个UIView子类,我认为你做的是你要求的,或者至少是关闭的.要设置动画,请将按钮挂钩到-animate:动作.

SPAnimatedShapeView.h:

#import <UIKit/UIKit.h>

@interface SPAnimatedShapeView : UIView

- (IBAction)animate:(id)sender;

@end
Run Code Online (Sandbox Code Playgroud)

SPAnimatedShapeView.m:

#import "SPAnimatedShapeView.h"
#import <QuartzCore/QuartzCore.h>

@interface SPAnimatedShapeView ()
@property (nonatomic, retain)   CAShapeLayer*   shapeLayer;
@end

@implementation SPAnimatedShapeView

@synthesize shapeLayer = _shapeLayer;

- (void)dealloc
{
    [_shapeLayer release];
    [super dealloc];
}

- (void)layoutSubviews
{
    if (!self.shapeLayer)
    {
        self.shapeLayer = [[[CAShapeLayer alloc] init] autorelease];
        self.shapeLayer.bounds = CGRectMake(0, 0, 100, 100);     // layer is 100x100 in size
        self.shapeLayer.position = self.center;                  // and is centered in the view
        self.shapeLayer.strokeColor = [UIColor blueColor].CGColor;
        self.shapeLayer.fillColor = [UIColor redColor].CGColor;
        self.shapeLayer.lineWidth = 3.f;

        [self.layer addSublayer:self.shapeLayer];
    }
}

- (IBAction)animate:(id)sender
{
    UIBezierPath* path0 = [UIBezierPath bezierPath];
    [path0 moveToPoint:CGPointZero];
    [path0 addLineToPoint:CGPointZero];
    [path0 addLineToPoint:CGPointZero];
    [path0 addLineToPoint:CGPointZero];

    UIBezierPath* path1 = [UIBezierPath bezierPath];
    [path1 moveToPoint:CGPointZero];
    [path1 addLineToPoint:CGPointMake(50,100)];
    [path1 addLineToPoint:CGPointMake(50,100)];
    [path1 addLineToPoint:CGPointMake(50,100)];

    UIBezierPath* path2 = [UIBezierPath bezierPath];
    [path2 moveToPoint:CGPointZero];
    [path2 addLineToPoint:CGPointMake(50,100)];
    [path2 addLineToPoint:CGPointMake(100,0)];
    [path2 addLineToPoint:CGPointMake(100,0)];

    UIBezierPath* path3 = [UIBezierPath bezierPath];
    [path3 moveToPoint:CGPointZero];
    [path3 addLineToPoint:CGPointMake(50,100)];
    [path3 addLineToPoint:CGPointMake(100,0)];
    [path3 addLineToPoint:CGPointZero];

    CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"path"];    
    animation.duration = 4.0f;
    animation.values = [NSArray arrayWithObjects:(id)path0.CGPath, (id)path1.CGPath, (id)path2.CGPath, (id)path3.CGPath, nil];
    [self.shapeLayer addAnimation:animation forKey:nil];
}

@end
Run Code Online (Sandbox Code Playgroud)

  • 事实证明,有一种更简单的方法:使用从0到1的`CABasicAnimation`为`strokeEnd`属性设置动画.[解释和示例代码](http://oleb.net/blog/2010/12/animating-drawing-的-cgpath与 - cashapelayer /). (6认同)