5

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

动画片

我希望以最终大小将整个事物绘制一次,然后在动画时进行缩放。和属性似乎是我正在寻找的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];
4

1 回答 1

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...
于 2013-03-20T16:50:28.860 回答