str*_*mes 3 macos core-animation nsview facebook-pop
请注意,这是与Mac OS X(NSView)相关的问题.
我正在尝试使用Facebook的POP动画从中心扩展NSView(到其大小的75%),然后再回到100%,但是我无法让它工作.鉴于kPOPLayerScaleXY似乎不起作用,我做了以下但是这给了我不正确的结果(因为它似乎从左上角向下缩小,而当zoomIn它为假时,它变得太大:
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"];
Run Code Online (Sandbox Code Playgroud)
我最终通过使用两个动画来实现它:
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"];
Run Code Online (Sandbox Code Playgroud)