iPhone核心动画 - 画一个圆圈

ben*_*ink 48 iphone core-animation

我想创建一个动画.我可以解释这个问题的最佳方法是,如果你能想象画一个圆圈.它从顶部或12点钟开始,顺时针顺时针绘制,直到它在10秒左右的空间内变成一个完整的圆圈.

我得到的壁橱是使用Core Animation绘制一个围绕中心点旋转的点(使用此处的示例代码).但我对如何绘制圆圈感到茫然?任何建议都非常欢迎.

非常感谢 :)

Dav*_*ist 161

你真正应该做的是为CAShapeLayer的笔画设置动画,其中路径是一个圆圈.这将使用Core Animation加速,并且在-drawRect:中绘制圆的一部分时不那么混乱.

下面的代码将在屏幕中心创建一个圆形图层,并顺时针为其设置动画,使其看起来好像正在绘制.你当然可以使用你想要的任何形状.(你可以阅读这篇文章上奥莱Begemanns博客,详细了解如何制作动画的形状层的行程.)

注意: stoke属性与图层上的边框属性不同.要更改笔划的宽度,您应该使用"lineWidth"而不是"borderWitdh"等.

// Set up the shape of the circle
int radius = 100;
CAShapeLayer *circle = [CAShapeLayer layer];
// Make a circular shape
circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 2.0*radius, 2.0*radius) 
                                         cornerRadius:radius].CGPath;
// Center the shape in self.view
circle.position = CGPointMake(CGRectGetMidX(self.view.frame)-radius, 
                              CGRectGetMidY(self.view.frame)-radius);

// Configure the apperence of the circle
circle.fillColor = [UIColor clearColor].CGColor;
circle.strokeColor = [UIColor blackColor].CGColor;
circle.lineWidth = 5;

// Add to parent layer
[self.view.layer addSublayer:circle];

// Configure animation
CABasicAnimation *drawAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
drawAnimation.duration            = 10.0; // "animate over 10 seconds or so.."
drawAnimation.repeatCount         = 1.0;  // Animate only once..

// Animate from no part of the stroke being drawn to the entire stroke being drawn
drawAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
drawAnimation.toValue   = [NSNumber numberWithFloat:1.0f];

// Experiment with timing to get the appearence to look the way you want
drawAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];

// Add the animation to the circle
[circle addAnimation:drawAnimation forKey:@"drawCircleAnimation"];
Run Code Online (Sandbox Code Playgroud)

  • 还有其他方法.您可以创建自定义图层并为其设置动画.看看[这个使用自定义CALayer设置动画片切片的精彩教程](http://blog.pixelingene.com/2012/02/animating-pie-slices-using-a-custom-calayer/) (3认同)

mat*_*mer 8

根据@ David的回答,这是问题的另一个解决方案.此方法允许您设置圆的动画方向,并提供稍微更多的控制.编辑:我写过一篇关于如何用Swift绘制圆圈的博客文章,我将尽力跟上贝塔的最新动态.如果下面的代码不适合你,请检查那里.

let radius = 100.0

// Create the circle layer
var circle = CAShapeLayer()

// Set the center of the circle to be the center of the view
let center = CGPointMake(CGRectGetMidX(self.frame) - radius, CGRectGetMidY(self.frame) - radius)

let fractionOfCircle = 3.0 / 4.0

let twoPi = 2.0 * Double(M_PI)
// The starting angle is given by the fraction of the circle that the point is at, divided by 2 * Pi and less
// We subtract M_PI_2 to rotate the circle 90 degrees to make it more intuitive (i.e. like a clock face with zero at the top, 1/4 at RHS, 1/2 at bottom, etc.)
let startAngle = Double(fractionOfCircle) / Double(twoPi) - Double(M_PI_2)
let endAngle = 0.0 - Double(M_PI_2)
let clockwise: Bool = true

// `clockwise` tells the circle whether to animate in a clockwise or anti clockwise direction
circle.path = UIBezierPath(arcCenter: center, radius: radius, startAngle: CGFloat(startAngle), endAngle: CGFloat(endAngle), clockwise: clockwise).CGPath

// Configure the circle
circle.fillColor = UIColor.blackColor().CGColor
circle.strokeColor = UIColor.redColor().CGColor
circle.lineWidth = 5

// When it gets to the end of its animation, leave it at 0% stroke filled
circle.strokeEnd = 0.0

// Add the circle to the parent layer
self.layer.addSublayer(circle)

// Configure the animation
var drawAnimation = CABasicAnimation(keyPath: "strokeEnd")
drawAnimation.repeatCount = 1.0

// Animate from the full stroke being drawn to none of the stroke being drawn
drawAnimation.fromValue = NSNumber(double: fractionOfCircle)
drawAnimation.toValue = NSNumber(float: 0.0)

drawAnimation.duration = 30.0

drawAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)

// Add the animation to the circle
circle.addAnimation(drawAnimation, forKey: "drawCircleAnimation")
Run Code Online (Sandbox Code Playgroud)