gpt4 book ai didi

iphone - 使用 UIView 或 CALayer 绘图和制作动画?

转载 作者:行者123 更新时间:2023-12-03 20:11:14 25 4
gpt4 key购买 nike

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

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

有替代方案吗?

附注我是一个德国人。抱歉,有错误。

<小时/>

我使用 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属性,但没有效果。

希望得到帮助。

马库斯

最佳答案

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

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

但是,这通常被认为是 CALayer 的优势。您可能只想在此处使用 UIViews,默认情况下,它不会产生像这样的动画转换。

关于iphone - 使用 UIView 或 CALayer 绘图和制作动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2599033/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com