一个CATransaction块的范围 [英] The scope of a CATransaction block

查看:145
本文介绍了一个CATransaction块的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了一些code,去除特定层从它的superlayer通过执行以下操作:

I have seen some code that removes a given layer from it's superlayer by doing the following:

void RemoveImmediately(CALayer *layer) {
    [CATransaction flush];
    [CATransaction begin];
    [CATransaction setValue:(id)kCFBooleanTrue
                     forKey:kCATransactionDisableActions];
    [layer removeFromSuperlayer];
    [CATransaction commit];
}  

我写了改变给定层的动画方式使用CAKeyframeAnimation,它看起来像这样位置的方法:

I have written a method that changes the position of a given layer in an animated way using CAKeyframeAnimation, which looks like this:

- (void)animateMovingObject:(NXUIObject*)obj
           fromPosition:(CGPoint)startPosition
             toPosition:(CGPoint)endPosition
               duration:(NSTimeInterval)duration {    

    CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    pathAnimation.calculationMode = kCAAnimationPaced;
    pathAnimation.duration = duration;

    CGMutablePathRef curvedPath = CGPathCreateMutable();
    CGPathMoveToPoint(curvedPath, NULL, startPosition.x, startPosition.y);
    CGPathAddCurveToPoint(curvedPath, NULL, 
                      startPosition.x, endPosition.y, 
                      startPosition.x, endPosition.y,
                      endPosition.x, endPosition.y);
    pathAnimation.path = curvedPath;
    [obj addAnimation:pathAnimation forKey:@"pathAnimation"];
    CGPathRelease(curvedPath);
}

就知道,一切都很好。现在假设我在我的应用程序'主机层,其中有3子层。我想的前两个子层被移动到另一个位置,并要删除的最后一个。所以,我做了以下内容:

Up to know, everything is fine. Now suppose I have in my app a 'master layer', which has 3 sublayers. I want the first two sublayers to be moved to another position and the last one to be removed. So I did the following:

CALayer obj1 = ... // set up layer and add as sublayer
[self.masterLayer addSublayer:obj1];
[self animateMovingObject:obj1
             fromPosition:CGPointMake(0.0, 0.0)
               toPosition:CGPointMake(100.0, 100.0)
                 duration:2.0];

CALayer obj2 = ... // set up layer and add as sublayer
[self.masterLayer addSublayer:obj2];
[self animateMovingObject:obj2
             fromPosition:CGPointMake(0.0, 0.0)
               toPosition:CGPointMake(150.0, 100.0)
                 duration:2.0];

CALayer obj3 = ... // set up layer and add as sublayer
[self.masterLayer addSublayer:obj3];
// ...
RemoveImmediately(obj3);      // This removes the two previous animations too
//[obj3 removeFromSuperlayer];  // This just removes obj3, the previous animations works

请注意,如果我称之为 RemoveImmediately(),甚至路过 obj3 作为参数,我看不到第一两层被动画。但是,如果我删除 obj3 只是通过调用 removeFromSuperlayer 前两层一般的动画。
貌似CATransaction块取消正在做所有的动画,甚至用CAKeyframeAnimation创建的那些。

Note that if I call RemoveImmediately(), even passing obj3 as argument I can't see the first two layers being animated. However, if I remove the obj3 just by calling removeFromSuperlayer the first two layers are animated normally. Looks like the CATransaction block cancels all animations being done, even those that were created with CAKeyframeAnimation.

我什么我失踪?

先谢谢了。

推荐答案

一CATransaction的范围应该只是在 [CATransaction开始] [CATransaction提交] 线。

The scope of a CATransaction should just be between the [CATransaction begin] and [CATransaction commit] lines.

此外,你不应该需要在code中的 [CATransaction冲洗] 。苹果通常建议你不要强迫挂起出于性能的考虑交易的执行,你正在设置不应该影响任何未决的动画交易。

Also, you shouldn't need the [CATransaction flush] in that code. Apple generally recommends you not force the execution of pending transactions for performance reasons, and the transaction you're setting up there should not affect any pending animations.

我猜测这个奇怪的行为仍然是关系到你在你的<一问什么href=\"http://stackoverflow.com/questions/4398111/multiple-cakeyframeanimation-in-different-layers-at-the-same-time\">$p$pvious问题时,其中三个动画以某种方式相互干扰。正如我评论那里,这是行为就像三个动画被添加到一个层,而不是三个独立的。我会检查并确保没有发生这种情况的可能(交叉指针,等等)。

I'm guessing this odd behavior is still related to what you asked in your previous question, where the three animations are somehow interfering with one another. As I commented there, this is behaving like the three animations are being added to one layer, not three independent ones. I'd check and make sure there was no possibility of that happening (crossed pointers, etc.).

这篇关于一个CATransaction块的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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