我正在尝试同时执行几个动画。一个是从一个uimageview转换到另一个uimageview,另一个是动画标签的翻译。标签位于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。结合上述动画的修复方法是什么,还有更精确的原因是什么?
答案 0 :(得分:0)
我会将整个代码包装在CATransaction中,以将不同的CAAnimations分组到一个组中。
与CAKeyFrameAnimation一起使用它的Psuedo代码如下所示:
[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];