切换CABasicAnimation的位置属性中间动画导致闪烁 [英] Switching CABasicAnimation on the position property mid animation causes a flicker

查看:131
本文介绍了切换CABasicAnimation的位置属性中间动画导致闪烁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码使用CALayers有气泡从底部到顶部。如果用户触摸屏幕,我有一些代码替换当前运行的动画,其中一个具有手指触摸的toPoint。当动画切换时,会导致设备(而不是模拟器上)闪烁。任何提示,消除闪烁,将不胜感激!非常感谢。

I have some code that uses CALayers to have bubbles flowing bottom to top. If the user touches the screen I have some code that replaces the currently running animation with one that has a toPoint where the finger touched down. When the animation switches it causes a flicker on the device (not on the simulator). Any tips on eliminating the flicker would be appreciated! Thanks.

在图层本身内部出现的气泡代码:

Code for the bubbles flowing up inside the layer itself:

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"];

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

The code for attracting nearby bubbles to the touch point written in the super layer:

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锁,看看是否有帮助。这将防止以前的动画更改图层的位置,而您正在使用新的动画更新它们。

Try using a CATransaction lock around your updates to see if that helps. This will prevent the previous animations from changing the position of the layers while you're in the process of updating them with new animations.

在触摸处理方法中,动作和锁定:

In your touch handling method, wrap the animations in a transaction and lock:

[CATransaction lock];
[CATransaction begin];

// update the sublayers with new animations

[CATransaction commit];
[CATransaction unlock];

这篇关于切换CABasicAnimation的位置属性中间动画导致闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆