简体   繁体   English

使用UIView或CALayer绘制和动画?

[英]Drawing and Animating with UIView or CALayer?

i am trying to implement a graph which a uiview draws. 我正在尝试实现uiview绘制的图表。 There are 3 UIViews to animate a left/right slide. 有3个UIViews可以为左/右幻灯片设置动画。 The problem is, that i can't cancel the UIView animation. 问题是,我无法取消UIView动画。 So I replaced the UIViews by CALayer. 所以我用CALayer取代了UIViews。 Now, the question is if CALayer is appicable for this? 现在,问题是CALayer是否适合这个? Is it normal to draw on a CALayer like this? 在这样的CALayer上画画是否正常? And why is a CALayer so slow when I manipulate the frame properties. 当我操纵框架属性时,为什么CALayer这么慢。

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

Is there a alternativ? 有替代品吗?

PS I am a german guy. PS我是德国人。 Sorry for mistakes. 抱歉错误。


I got the animation with CATransaction, but now I will animate ax move with CABasicAnimation. 我用CATransaction获得动画,但现在我将用CABasicAnimation动画ax移动。 That's no problem expect that the position of the layer go back to the previous 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]];

The position changes the position (eg xStart = 100, xEnd = 200), but when the animation ends the layer goes back to the beginning x (eg x = 100). 位置改变位置(例如xStart = 100,xEnd = 200),但是当动画结束时,层返回到开头x(例如x = 100)。 Is that normal? 这是正常的吗? How can I solve this problem, that the end position doesn't change anymore? 如何解决这个问题,结束位置不再变化? I tried to changed the removeOnComplete property but that didn't effect. 我试图更改removeOnComplete属性,但这没有影响。

Hope for help. 希望得到帮助。

Markus 马库斯

Not sure what you mean by 'slow', but setting the frame of a CALayer in this way uses 'implicit animation'. 不确定你的意思是'慢',但以这种方式设置CALayer的框架使用'隐式动画'。 That is, it will animated the transition from the old frame to the new frame. 也就是说,它将动画从旧帧到新帧的过渡。 You can turn this off: 你可以关闭它:

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

However, this is usually considered an advantage of CALayer. 但是,这通常被认为是CALayer的优势 You way want to just use UIViews here, which will not, by default, animate transitions such as this. 你想在这里使用UIViews,默认情况下不会像这样使用动画转换。

Instead of setting the destination position in theAnimation, just set the position property of the thing you want to move. 不要在动画中设置目标位置,只需设置要移动的事物的位置属性。

When you addAnimation:theAnimation, you're setting the "visual style" of any changes to the keyPath property you specified. 当你添加动画:theAnimation时,你正在设置你指定的keyPath属性的任何更改的“视觉样式”。

When you change the position of the object that the animation is attached to, say from (0,0) to (500,500), CoreAnimation will animate the change for you. 当您更改动画附加到的对象的位置时,例如从(0,0)到(500,500),CoreAnimation将为您设置更改动画。 The theAnimation object doesn't need the start and end positions, since the underlying object has them. theAnimation对象不需要开始和结束位置,因为底层对象具有它们。

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

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