我环顾一下,找不到一个方法来做到这一点,基本上我想要做形状动画,如下图。这可以用iOS 6 API甚至iOS 7 API来完成吗? CAAnimation中的所有东西似乎都是将整个图层移动到另一个CGPoint,而不是形状的一部分。QuartzCore动画形状?
我可以做手工我想,把一个NSTimer的1/24或1/60,计算改变我的形状和重绘递增变化达到所需的形状,直到,但显然想如果苹果公司为此提供API,就可以利用苹果的易用性,缓解时间效应(并节省大量开发时间)。 TY。
我环顾一下,找不到一个方法来做到这一点,基本上我想要做形状动画,如下图。这可以用iOS 6 API甚至iOS 7 API来完成吗? CAAnimation中的所有东西似乎都是将整个图层移动到另一个CGPoint,而不是形状的一部分。QuartzCore动画形状?
我可以做手工我想,把一个NSTimer的1/24或1/60,计算改变我的形状和重绘递增变化达到所需的形状,直到,但显然想如果苹果公司为此提供API,就可以利用苹果的易用性,缓解时间效应(并节省大量开发时间)。 TY。
你可以使用CAShapeLayer
其中有一个属性path
这是动画。你将不得不通过使用Quartz的CGPath函数或使用UIBezierPath来将你的形状创建为CGPath
。
你会做这样的事情:
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.bounds = CGRectMake(0.f, 0.f, 50.f, 50.f);
shapeLayer.position = CGPointMake(50.f, 50.f);
shapeLayer.fillColor = [[UIColor redColor] CGColor];
shapeLayer.path = [[UIBezierPath bezierPathWithOvalInRect:shapeLayer.bounds] CGPath];
[self.view.layer addSublayer:shapeLayer];
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"path"];
animation.fromValue = (__bridge id) [[UIBezierPath bezierPathWithOvalInRect:shapeLayer.bounds] CGPath];
animation.toValue = (__bridge id) [[UIBezierPath bezierPathWithRect:shapeLayer.bounds] CGPath];
animation.repeatCount = NSIntegerMax;
animation.duration = 2.0;
animation.autoreverses = YES;
[shapeLayer addAnimation:animation forKey:@"shapeAnimation"];
如果如预期,你应该看看CAKeyframeAnimation
核心动画的插值不起作用。
谢谢卡尔,这工作,我的一天会因为它而变得更容易。 –
对于任何一个像我这样的人来说,修改Karl的例子,这里是一个简单的语音气泡实验,用投影的CAMediaTimingFunction移动其底部长度的尖锐部分。这基本上是我想要做的,当有人点击定义的项目网格时,指针移动到位以反映点击选择。
UIBezierPath *spath = [UIBezierPath bezierPath];
[spath moveToPoint:CGPointZero];
[spath addLineToPoint:CGPointMake(320.0f, 0)];
[spath addLineToPoint:CGPointMake(320.0f, BLOCK_HEIGHT)];
// Pointy part
[spath addLineToPoint:CGPointMake(20.0f + POINTER_HALF, BLOCK_HEIGHT)];
[spath addLineToPoint:CGPointMake(20.0f, BLOCK_HEIGHT + POINTER_HALF)];
[spath addLineToPoint:CGPointMake(20.0f - POINTER_HALF, BLOCK_HEIGHT)];
[spath addLineToPoint:CGPointMake(0, BLOCK_HEIGHT)];
[spath closePath];
UIBezierPath *spath2 = [UIBezierPath bezierPath];
[spath2 moveToPoint:CGPointZero];
[spath2 addLineToPoint:CGPointMake(320.0f, 0)];
[spath2 addLineToPoint:CGPointMake(320.0f, BLOCK_HEIGHT)];
// Pointy part
[spath2 addLineToPoint:CGPointMake(300.0f + POINTER_HALF, BLOCK_HEIGHT)];
[spath2 addLineToPoint:CGPointMake(300.0f, BLOCK_HEIGHT + POINTER_HALF)];
[spath2 addLineToPoint:CGPointMake(300.0f - POINTER_HALF, BLOCK_HEIGHT)];
[spath2 addLineToPoint:CGPointMake(0, BLOCK_HEIGHT)];
[spath2 closePath];
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.bounds = CGRectMake(0.f, 0.f, 320.0f, 76.0f);
shapeLayer.position = CGPointMake(160.0, 200.0);
shapeLayer.fillColor = [[UIColor redColor] CGColor];
shapeLayer.path = [[UIBezierPath bezierPathWithOvalInRect:shapeLayer.bounds] CGPath];
[self.layer addSublayer:shapeLayer];
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"path"];
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.fromValue = (__bridge id)(spath.CGPath);
animation.toValue = (__bridge id)(spath2.CGPath);
animation.repeatCount = NSIntegerMax;
animation.duration = 2.0;
animation.autoreverses = YES;
[shapeLayer addAnimation:animation forKey:@"shapeAnimation"];
您的形状是由CGPath定义的吗? – Wain