3

我正在尝试同时执行几个动画。一个是从一个 uimageview 过渡到另一个,另一个是为标签的 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过渡到另一个

...
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:@""];

然后隐藏动画也适用于 myLabel。像上面这样组合动画的修复方法是什么,更详细地说是什么原因?

4

1 回答 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];
于 2013-04-02T13:52:55.417 回答