2013-03-19 69 views
4

我正在为包含多个子视图的层次视图设置动画效果。一切都使用自动布局布局。目前,我动画视图,子视图没有被缩小与它一起:动画层次支持视图时缩放图层内容

animation

我想整个事情被抽一次,他们的最终尺寸,然后缩放,因为它的动画。 layerContentsPlacementlayerContentsRedrawPolicy属性似乎是我正在寻找,但更改它们似乎没有任何影响。

这里是我如何做动画基本运行通过:

// Add itemView to canvas 
NSView *itemView = // ... 
itemView.layerContentsPlacement = NSViewLayerContentsPlacementScaleProportionallyToFit; 
itemView.layerContentsRedrawPolicy = NSViewLayerContentsRedrawBeforeViewResize; 
[parentView addSubview:itemView]; 

// Add constraints for the final itemView frame ... 

// Layout at final state 
[parentView layoutSubtreeIfNeeded]; 

// Set initial animation state 
itemView.alphaValue = 0.0f; 
itemView.frame = // centered zero'ed frame 

// Set final animation state 
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) { 
    context.duration = 0.25f; 
    context.allowsImplicitAnimation = YES; 

    itemView.alphaValue = 1.0f; 
    [parentView layoutSubtreeIfNeeded]; 
} completionHandler:nil]; 
+2

简答:不要将视图的大小调整为动画。请使用变换对其进行缩放。 – 2013-03-19 19:09:55

回答

4

感谢David的评论,我切换到使用视图的层上的转换。

基本的想法是像平常一样创建和布局视图,然后创建一些CA动画并将其添加到视图的图层中。

// Add item view to canvas 
NSView *itemView = // ... 
[parentView addSubview:itemView]; 

// Add constraints for the final item view positioning 

// Layout at final state 
[parentView layoutSubtreeIfNeeded]; 

// Animate 
CABasicAnimation *animation = [CABasicAnimation animation]; 
CATransform3D transform = CATransform3DMakeScale(0.5f, 0.5f, 0.5f); 
animation.fromValue = [NSValue valueWithCATransform3D:transform]; 
animation.duration = 1.0f; 
[itemView.layer addAnimation:animation forKey:@"transform"]; 

// create any other animations...