iphone - 动画跳回原始状态

时间:2012-01-01 11:30:27

标签: ios

以下是我使用的代码。动画有效。然而,在它之后它又回到了原来的状态。我的代码有什么问题吗?感谢。

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    CABasicAnimation *expand=[CABasicAnimation animationWithKeyPath:@"transform"];
    expand.toValue=[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1.0)];
    expand.autoreverses=NO;
    expand.removedOnCompletion=YES;  

    CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    opacityAnimation.toValue  = [NSNumber numberWithFloat:0.0f];
    opacityAnimation.autoreverses=NO;
    opacityAnimation.removedOnCompletion=YES;

    CAAnimationGroup *group=[CAAnimationGroup animation];
    group.animations=[[NSArray alloc] initWithObjects:expand,opacityAnimation, nil];
    group.duration=1.0;
    group.fillMode=kCAFillModeForwards;
    group.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    [view1.layer addAnimation:group forKey:@"expand"];

}

3 个答案:

答案 0 :(得分:1)

您将removedOnCompletion设置为YES,指定在完成后移除动画。请改为NO

答案 1 :(得分:0)

摆脱跳回的一种简单方法是使用CATransaction在未启用操作的情况下使用您动画的值显式更新模型图层:

[CATransaction begin];
[CATransaction setDisableActions:YES];

//this will update the model layer
view1.layer.transform = CATransform3DMakeScale(1.2, 1.2, 1.0);
view1.layer.opacity = 0.f;

//your basic animations will animate the presentation layer
/* your CABasicAnimations here */

[CATransaction commit];

不要removedOnCompletion设置为NO以保持视图到位,因为这会使动画永远继续,并且模型图层永远不会更新表示层正在显示的位置。您可能还会因为从未删除动画而导致性能下降,因为GPU正在为一个并非真正移动的视图设置动画。

答案 2 :(得分:0)

CAAnimationGroup *group=[CAAnimationGroup animation];
group.animations=[[NSArray alloc] initWithObjects:expand,opacityAnimation, nil];
group.duration=1.0;
group.removedOnCompletion=NO;
group.fillMode=kCAFillModeForwards;
group.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
[view1.layer addAnimation:group forKey:@"expand"];

请尝试使用 removedOnCompletion 作为群组动画中的“否”,它可能适合您。