不同视图上同时运行多个CAAnimations

3

我试图同时执行几个动画。其中一个是从一个UIImageView过渡到另一个,另一个是在标签的translation.x上进行动画处理。该标签位于UIimageView的顶部。

但我得到的要么是平移正常工作并立即发生过渡,要么是基于隐藏属性的过渡也适用于我的标签,而标签应该只被移动(它也从隐藏变为可见)。 我无法使用CAAnimationGroup,因为它们适用于不同的视图。

// CAKeyframeAnimation用于滑动标签

...

CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.x"];

NSArray *xValues = @[[NSNumber numberWithFloat:myLabel.bounds.origin.x],
                [NSNumber numberWithFloat:myLabel.bounds.origin.x + screenHalf],
                [NSNumber numberWithFloat:myLabel.bounds.origin.x + screenHalf * 4]];
[anim setValues:xValues];

NSMutableArray *timeFrames = [NSMutableArray array];

CGFloat timeStep = 1.0 / ([xValues count] - 1);

for (NSInteger i = 0; i < [xValues count]; i++)
{
    [timeFrames addObject:[NSNumber numberWithFloat:timeStep * i]];
}

[anim setKeyTimes:timeFrames];

[anim setDuration:duration];
[anim setFillMode:kCAFillModeForwards];
[anim setRemovedOnCompletion:FALSE];

[myLabel.layer addAnimation:anim forKey:nil];
...

//从一个UIImageView过渡到另一个UIImageView

...
CATransition *transition = [CATransition animation];
[transition setDuration:duration];
[transition setType:kCATransitionFade];

//These two are uiimageviews i'm switching from and to
initial.hidden = TRUE;
next.hidden = FALSE;

//Initial and next are subviews of container which itself is a subview of viewcontroller's main view
[container.layer addAnimation:transition forKey:@""]; 

如果我像上面那样调用动画,转换会立即发生,标签会正确地滑过屏幕。如果我将最后一行改为:
[self.view.layer addAnimation:transition forKey:@""];

然后隐藏动画也适用于我的标签。如何修复像上面这样组合动画的问题,更详细地说是什么原因?


你还需要答案吗? - kelin
1个回答

0
我会将整个代码包装在CATransaction中,以将不同的CAAnimations分组为单个组。
使用CAKeyFrameAnimation的伪代码如下:
[CATransaction begin];
// set completion block if you want
[CATransaction setCompletionBlock:^{ NSLog(@"I'm done"); }];

// start a keyframe animation
CAKeyframeAnimation *key1 = .....

// start another keyframe animation block
CAKeyframeAnimation *key2 = ......

// Maybe do a basic animation
CABasicAnimation *anim1 = .....

// close out all the animations and have them start
[CATransaction commit];

使用关键帧动画如何实现,您能至少给一些伪代码吗? - guenis

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接