gpt4 book ai didi

iphone - 在动画中切换位置属性上的 CABasicAnimation 会导致闪烁

转载 作者:行者123 更新时间:2023-12-03 19:13:05 28 4
gpt4 key购买 nike

我有一些代码使用 CALayers 使气泡从下到上流动。如果用户触摸屏幕,我会编写一些代码,将当前正在运行的动画替换为具有手指触摸的 toPoint 的动画。当动画切换时,它会导致设备上(而不是模拟器上)闪烁。任何有关消除闪烁的提示将不胜感激!谢谢。

在层本身内部向上流动的气泡的代码:

CABasicAnimation *animation = [CABasicAnimationanimationWithKeyPath:@"position"];
[animation setDelegate:self];
CGPoint position = [self position];
NSValue *prevVal = [NSValue valueWithCGPoint:position];
[animation setFromValue:prevVal];
CGPoint toPoint = CGPointMake(position.x,-100);
[animation setToValue:[NSValue valueWithCGPoint:toPoint]];
[animation setDuration:animationDuration];
[self addAnimation:animation forKey:@"flow"];

super 层中编写的吸引附近气泡到触摸点的代码:

int count = [self.layer.sublayers count];
for(int i = 0; i < count ; i++) {
CALayer *layer= [self.layer.sublayers objectAtIndex:i];
CALayer *p = (CALayer*)[layer presentationLayer];
CGPoint position = [p position];

if(abs(position.x - touchPoint.x) < 100 && abs(position.y - touchPoint.y) < 100) {

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
[animation setDelegate:self];
NSValue *prevVal = [NSValue valueWithCGPoint:position];
[animation setFromValue:prevVal];
[animation setToValue:[NSValue valueWithCGPoint:touchPoint]];
[animation setDuration:2.0];
[animation setTimingFunction:[CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseOut]];
[layer addAnimation:animation forKey:@"flow"];
}

}

最佳答案

尝试在更新周围使用 CATransaction 锁,看看是否有帮助。当您使用新动画更新图层时,这将防止以前的动画更改图层的位置。

在您的触摸处理方法中,将动画包装在事务中并锁定:

[CATransaction lock];
[CATransaction begin];

// update the sublayers with new animations

[CATransaction commit];
[CATransaction unlock];

关于iphone - 在动画中切换位置属性上的 CABasicAnimation 会导致闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1102896/

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