【发布时间】:2013-06-30 01:39:33
【问题描述】:
如何改变形状(从椭圆形到矩形,反之亦然)动画?
脏代码:
UIBezierPath *roundedRectBezierPath = [UIBezierPath bezierPathWithRoundedRect:newRect cornerRadius:10];
UIBezierPath *ovalBezierPath = [UIBezierPath bezierPathWithOvalInRect:newRect];
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"path"];
animation.duration = 3;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
if (isRect) {
self.shapeLayer.path = roundedRectBezierPath.CGPath;
animation.fromValue = (__bridge id)(ovalBezierPath.CGPath);
animation.toValue = (__bridge id)(roundedRectBezierPath.CGPath);
} else {
self.shapeLayer.path = ovalBezierPath.CGPath;
animation.fromValue = (__bridge id)(roundedRectBezierPath.CGPath);
animation.toValue = (__bridge id)(ovalBezierPath.CGPath);
}
[self.shapeLayer addAnimation:animation forKey:@"animatePath"];
结果很奇怪:
我希望形状会缩小/扩大,而不是奇怪的重绘。
【问题讨论】:
-
哈哈!看起来矩形正在绘制[左上,右上,右下,左下],但椭圆是从“3点钟”位置逆时针绘制的。动画将映射从正方形到圆形的路径上的点。即矩形的左上角在圆圈上向右。尝试将圆形路径逆时针旋转 (4 * PI) / 3 rads。
-
仍然不是预期的动画。
-
@user620297 相反。我期待这种行为。来自文档:“如果两条路径具有不同数量的控制点或段,则结果未定义”。
-
@DavidRönnqvist 你是对的。那么该怎么做呢?
标签: iphone ios cocoa-touch core-animation