【问题标题】:Scaling layer contents when animating layer-backed view动画支持图层的视图时缩放图层内容
【发布时间】:2013-03-19 18:34:51
【问题描述】:

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

我希望以最终大小绘制一次整个内容,然后根据动画进行缩放。 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];

【问题讨论】:

  • 简短回答:不要将视图大小调整为动画。改为使用变换对其进行缩放。

标签: cocoa animation core-animation autolayout appkit


【解决方案1】:

感谢 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...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多