gpt4 book ai didi

macos - 如何使用 Facebook Pop 动画从中心缩放 NSView?

转载 作者:行者123 更新时间:2023-12-01 09:37:44 25 4
gpt4 key购买 nike

请注意,这是一个与 Mac OS X (NSView) 相关的问题。

我正在尝试使用 Facebook 的 POP 动画从中心缩放 NSView(到其大小的 75%),然后再回到 100%,但我无法让它工作。鉴于 kPOPLayerScaleXY 似乎不起作用,我做了以下操作,但这给了我不正确的结果(因为它似乎从左上角缩小,并且当 zoomIn 为 false , 它太大了:

  CGRect baseRect = CGRectMake(0, 0, 30, 24);
CGFloat scale = (zoomIn) ? 0.75 : 1.0;

CGFloat x = baseRect.origin.x;
CGFloat y = baseRect.origin.y;
CGFloat width = baseRect.size.width;
CGFloat height = baseRect.size.height;

if (zoomIn) {
width -= floorf((1.0 - scale) * width);
height -= floorf((1.0 - scale) * height);

x += floorf((width * (1.0f - scale)) / 2);
y += floorf((height * (1.0f - scale)) / 2);
}

CGRect scaleRect = CGRectMake(x, y, width, height);

[myView.layer pop_removeAllAnimations];

POPSpringAnimation *animation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerBounds];
animation.springBounciness = 8;
animation.toValue = [NSValue valueWithCGRect: scaleRect];
[myView.layer pop_addAnimation:animation forKey:@"zoom"];

最佳答案

我最终通过使用两个动画来让它工作:

  CGRect baseRect = CGRectMake(0, 0, 30, 24);
CGFloat scale = (zoomIn) ? 0.80 : 1.0;

CGFloat x = baseRect.origin.x;
CGFloat y = baseRect.origin.y;

if (zoomIn) {
x = floorf((baseRect.size.width * (1.0 - scale)) / 2);
y = floorf((baseRect.size.height * (1.0 - scale)) / 2);
}

[myView.layer pop_removeAllAnimations];

POPSpringAnimation *animation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
animation.springBounciness = 8;
animation.toValue = [NSValue valueWithCGSize:CGSizeMake(scale, scale)];
[myView.layer pop_addAnimation:animation forKey:@"zoom"];

animation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerTranslationXY];
animation.springBounciness = 8;
animation.toValue = [NSValue valueWithCGPoint:CGPointMake(x, y)];
[myView.layer pop_addAnimation:animation forKey:@"translate"];

关于macos - 如何使用 Facebook Pop 动画从中心缩放 NSView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27346139/

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