2014-12-07 80 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"];