我有一些代码使用 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"];
超级层中编写的吸引附近气泡到触摸点的代码:
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];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)