2013-01-05 13 views
0

我正在使用以下代码来转储层的边界前后状态&我正在进行动画处理的视图的表示层。我没有看到任何改变 - 看起来像我在理解方面缺少了一些东西 - 已经阅读了CA编程指南几次,但无济于事。任何指导将理解iPhone CABasicAnimation - 其中是在transform.scale后设置的新边界

- (void) shrink{ [Dumper dumpRect:@"BEFORE layer bounds" :[iv.layer bounds]]; [Dumper dumpRect:@"BEFORE p-layer bounds" :[iv.layer.presentationLayer bounds]];

[UIView animateWithDuration:1.5 animations:^{ 
     CABasicAnimation *scaleAnim = [CABasicAnimatianimationWithKeyPath:@"transform.scale"]; 

     scaleAnim.fromValue = [NSNumber numberWithFloat:1]; 
     scaleAnim.toValue = [NSNumber numberWithFloat:0.5]; 

    scaleAnim.autoreverses=NO; 
    [scaleAnim setDuration:1]; 
    // these need to be invoked to retain the scaled version 
    scaleAnim.removedOnCompletion = NO; 
    [scaleAnim setFillMode:kCAFillModeForwards]; 

    [iv.layer addAnimation:scaleAnim forKey:@"scale"]; 

}completion:^(BOOL finished){ 

    NSLog(@"Animation done - %d",finished); 
    [Dumper dumpRect:@"AFTER layer bounds" :[iv.layer bounds]]; 
    [Dumper dumpRect:@"AFTER p-layer bounds" :[iv.layer.presentationLayer bounds]]; 

}]; 

}

在运行该代码 - 我得到以下: BEFORE层边界 - 矩形X = 0.000000 Y = 0.000000 W = 100.000000 H = 200.000000 BEFORE p层边界 - 矩形x = 0.000000 y = 0.000000 w = 100.000000 h = 200.000000 动画完成 - 1 AFTER图层边界 - 矩形x = 0.000000 y = 0.000000 w = 100.000000 h = 200.000000 p层边界 - 矩形x = 0.000000 y = 0.000000 w = 100.000000 h = 200.000000

所以之前没有变化&之后?

即使进行“transform.translation”转换,我也观察到了同样的行为。

我需要最后的界限,以便我可以设置UIView,即,iv将被设置为适当的帧值。

相关的问题是 - 我明白,当图层被动画时,它只是完成了绘图,但底层视图仍然与原始帧一起设置 - 设置UIView帧的正确方法是什么。

回答

0

您不应该在UIViewanimateWithDuration方法中放置该代码。如果只想制作动画,您可以使用CABasicAnimation;如果您想要改变某些内容并希望制作动画,请使用animateWithDuration。例如,如果你想改变帧动画,你写的:

[UIView animateWithDuration:1.5 animations:^{ 
    CGRect newFrame = CGRectMake(0, 0, 50, 100); 
    iv.frame = newFrame; 
}completion:^(BOOL finished){ 
    // Code to be executed after the animation 
}]; 

这将改变你的框架,这将是动画。希望这可以帮助!

+0

感谢您的回应 - 您提供的代码完美工作。 我也想尝试CABasicAnimation。下面的代码用CABasicAnimation我已经放在一起确保了视图的动画效果,但是和之前的图层的最终帧值一样,表现层保持不变....所以动画被绘制为动画的一部分,但是图层(&表示层)值不反映它? 请指导...... TIA –

+0

好的 - 终于找到了问题(感觉就像在背后踢自己:-)。将“转储后调用”移动到委托方法,并且p层具有正确的框架值。 (void)animationDidStop:(CABasicAnimation *)theAnimation finished:(BOOL)flag NSLog(@“animationDidStop”); [Dumper dumpRect:@“AFTER layer frame”:[iv.layer frame]]; [Dumper dumpRect:@“AFTER p-layer frame”:[iv.layer.presentationLayer frame]]; [Dumper dumpRect:@“AFTER iv frame”:iv.layer.frame]; } –