动画图层支持的视图时缩放图层内容

时间:2013-03-19 18:34:52

标签: cocoa animation core-animation autolayout appkit

我正在为包含多个子视图的图层支持的视图设置动画。一切都使用自动布局布局。目前,在我为视图设置动画时,子视图不会随之缩放:

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];

1 个答案:

答案 0 :(得分: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...