首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CATransaction块的作用域

CATransaction块的作用域
EN

Stack Overflow用户
提问于 2010-12-11 00:25:32
回答 1查看 1.9K关注 0票数 0

我见过一些代码,它通过执行以下操作将给定的层从其上层移除:

代码语言:javascript
复制
void RemoveImmediately(CALayer *layer) {
    [CATransaction flush];
    [CATransaction begin];
    [CATransaction setValue:(id)kCFBooleanTrue
                     forKey:kCATransactionDisableActions];
    [layer removeFromSuperlayer];
    [CATransaction commit];
}  

我已经编写了一个方法,使用CAKeyframeAnimation以动画方式更改给定层的位置,如下所示:

代码语言:javascript
复制
- (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个子层。我希望前两个子层被移动到另一个位置,最后一个被移除。因此,我做了以下工作:

代码语言:javascript
复制
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作为参数传递,我也看不到前两层是动画。但是,如果我仅仅通过调用removeFromSuperlayer来删除obj3,那么前两层就会正常地进行动画处理。看起来CATransaction块取消了正在执行的所有动画,即使是使用CAKeyframeAnimation创建的动画也是如此。

我错过了什么?

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-12-11 00:45:34

CATransaction的作用域应该只在[CATransaction begin][CATransaction commit]行之间。

此外,您不应该在该代码中使用[CATransaction flush]。Apple通常建议您不要出于性能原因而强制执行挂起的事务,并且您在那里设置的事务不应影响任何挂起的动画。

我猜这种奇怪的行为仍然与你在previous question中的要求有关,其中三个动画以某种方式相互干扰。正如我在那里评论的那样,这就像是将三个动画添加到一个层中,而不是三个独立的层。我会检查并确保没有发生这种情况的可能性(交叉指针等)。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4410969

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档