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

Jos*_*rth 5 cocoa animation core-animation appkit autolayout

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

动画片

我希望将整个事物绘制一次,以它们的最终尺寸绘制,然后在动画播放时进行缩放。该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];
Run Code Online (Sandbox Code Playgroud)

Jos*_*rth 4

感谢大卫的评论,我转而在视图层上使用变换。

基本思想是像平常一样创建和布局视图,然后创建一些 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...
Run Code Online (Sandbox Code Playgroud)