gpt4 book ai didi

cocoa - 对图层支持的 View 进行动画处理时缩放图层内容

转载 作者:行者123 更新时间:2023-12-03 16:14:09 25 4
gpt4 key购买 nike

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

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

最佳答案

感谢 David 的评论,我转而在 View 层上使用变换。

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

// 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...

关于cocoa - 对图层支持的 View 进行动画处理时缩放图层内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15507753/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com