2012-08-10 96 views
0

我正在使用CABasicAnimation來更改邊界屬性,然後使用它來更改圖層的位置屬性。是否可能同時做到這兩點?有點像改變UIView的框架?同時縮放和移動圖層

CGRect oldBounds = mask.bounds; 
CGRect newBounds = CGRectMake(0,0, rect.size.width * scale, rect.size.height * scale); 
NSLog(@"%@", NSStringFromCGRect(newBounds)); 
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"bounds"]; 

animation.fromValue = [NSValue valueWithCGRect:oldBounds]; 
animation.toValue = [NSValue valueWithCGRect:newBounds]; 

mask.bounds = newBounds; 
[mask addAnimation:animation forKey:@"bounds"]; 



CGPoint oldPos = mask.position; 
CGPoint newPos = CGPointMake(rect.origin.x * scale, rect.origin.y * scale); 

CABasicAnimation *animation2 = [CABasicAnimation animationWithKeyPath:@"position"]; 

animation2.fromValue = [NSValue valueWithCGPoint:oldPos]; 
animation2.toValue = [NSValue valueWithCGPoint:newPos]; 

mask.position = newPos; 
[mask addAnimation:animation2 forKey:@"position"]; 

回答

0

編輯:改到給出更爲精確的動畫...

您可以一次添加2個動畫...

像這樣:

CALayer * myLayer ; 

{ 
    CABasicAnimation * anim = [ CABasicAnimation animationWithKeyPath:@"position" ] ; 
    anim.fromValue = [ NSValue valueWithCGPoint:layer.position ] ; 
    [ layer addAnimation:anim forKey:nil ] ; 
} 

layer.position = <#newPosition#> ; 

{ 
    CABasicAnimation * anim = [ CABasicAnimation animationWithKeyPath:@"bounds" ] ; 
    anim.fromValue = [ NSValue valueWithCGRect:layer.bounds ] ; 
    [ layer addAnimation:anim forKey:nil ] ; 
} 

layer.bounds = <#newBounds#> ; 
+0

請檢查我的代碼。你的意思是嗎?用這種方式,它看起來像是先縮放然後移動。 – 0xSina 2012-08-10 00:25:23

+0

好像你回答了你自己的問題? – nielsbot 2012-08-10 00:26:32

+0

如果您將「layer.bounds」和「layer.position」的分配移除,會發生什麼情況?另外,使用'addAnimation:forKey:','key'參數可供您自己參考 - 圖層不關心它。 – nielsbot 2012-08-10 00:30:04