在位置属性mid动画上切换CABasicAnimation会导致闪烁

4 iphone cocoa-touch core-animation

我有一些代码使用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"];
Run Code Online (Sandbox Code Playgroud)

用于将附近气泡吸引到超级层中写入的触摸点的代码:

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"];
   }        
Run Code Online (Sandbox Code Playgroud)

}

Jas*_*man 5

尝试在更新周围使用CATransaction锁定,看看是否有帮助.这将防止以前的动画更改层的位置,而你在新的动画更新的过程当中是.

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

[CATransaction lock];
[CATransaction begin];

// update the sublayers with new animations

[CATransaction commit];
[CATransaction unlock];
Run Code Online (Sandbox Code Playgroud)