使用UIView或CALayer绘制和动画?

时间:2010-04-08 10:20:35

标签: iphone animation uiview drawing calayer

我正在尝试实现uiview绘制的图表。有3个UIViews可以为左/右幻灯片设置动画。问题是,我无法取消UIView动画。所以我用CALayer取代了UIViews。现在,问题是CALayer是否适合这个?在这样的CALayer上画画是否正常?当我操纵帧属性时,为什么CALayer这么慢。

CGRect frame = curve.frame;
frame.origin.x -= diffX;
[curve setFrame:frame];

有替代方案吗?

P.S。我是德国人。抱歉有错误。


我使用CATransaction获得动画,但现在我将使用CABasicAnimation为x移动设置动画。期望图层的位置返回到前一个x,这没有问题。

CABasicAnimation *theAnimation; 
theAnimation = [CABasicAnimation animationWithKeyPath:@"position"]; 
theAnimation.delegate = self;
theAnimation.duration = 1.0;
theAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
CGPoint position = [[curves objectAtIndex:i]position];
position.x = [[curves objectAtIndex:i]position].x - diffX;
[theAnimation setToValue:[NSValue valueWithCGPoint:position]];
[[curves objectAtIndex:i] addAnimation:theAnimation forKey:[NSString stringWithFormat: @"translate.x.%d", index]];

位置改变位置(例如xStart = 100,xEnd = 200),但是当动画结束时,层返回到开头x(例如x = 100)。 这是正常的吗?如何解决这个问题,结束位置不再变化? 我试图更改removeOnComplete属性,但这没有效果。

希望得到帮助。

马库斯

2 个答案:

答案 0 :(得分:1)

不确定'慢'是什么意思,但以这种方式设置CALayer的帧使用'隐式动画'。也就是说,它将动画从旧帧到新帧的过渡。你可以关闭它:

[CATransaction begin];
[CATransaction setValue: (id) kCFBooleanTrue forKey: kCATransactionDisableActions];
[curve setFrame:frame];
[CATransaction commit];

但是,这通常被认为是CALayer的优势。你想在这里使用UIViews,默认情况下不会像这样使用动画转换。

答案 1 :(得分:0)

不要在动画中设置目标位置,只需设置要移动的东西的位置属性。

当你添加动画:theAnimation时,你正在设置你指定的keyPath属性的任何更改的“视觉样式”。

当您更改附加动画的对象的位置时,例如从(0,0)到(500,500),CoreAnimation将为您设置更改动画。 theAnimation对象不需要开始和结束位置,因为底层对象具有它们。

相关问题