繁体   English   中英

使用UIView或CALayer绘制和动画?

[英]Drawing and Animating with UIView or CALayer?

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

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

有替代品吗?

PS我是德国人。 抱歉错误。


我用CATransaction获得动画,但现在我将用CABasicAnimation动画ax移动。 期望层的位置返回到前一个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属性,但这没有影响。

希望得到帮助。

马库斯

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

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM