2014-12-07 81 views
3

请注意,这是一个Mac OS X(NSView)相关问题。我想使用Facebook的POP动画从中心(到75%的大小),然后回到100%,但我不能让它的工作。鉴于kPOPLayerScaleXY似乎不工作,我做了以下内容,但是这给了我不正确的结果(因为它似乎从左上角的缩小,而当zoomIn是假的,它会太大:如何使用Facebook流行动画从中心缩放NSView?

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"]; 
+0

同样的问题。设置'frame'和'anchorPoint'后,所有工作都正常。 – Sk0prion 2016-11-01 16:35:44

回答

5

我最后通过使用两个动画代替它:

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