2010-08-01 75 views
2

我一定是做錯了顯然。 當我設置視圖的框架時,我的委託沒有被調用。核心動畫代表不叫

框架版本不工作

-(void)someMethod { 
    CAAnimation *anim = [CABasicAnimation animation]; 
    [anim setDelegate:self]; 
    [[currentViewController view] setAnimations:[NSDictionary dictionaryWithObject:anim forKey:@"frame"]]; 
    [[newViewController view] setAnimations:[NSDictionary dictionaryWithObject:anim forKey:@"frame"]]; 

    [[[currentViewController view] animator] setFrame:currentTarget]; 
    [[[newViewController view] animator] setFrame:newTarget]; 
} 

- (void)animationDidStop:(CAAnimation *)animation finished:(BOOL)flag { 
    NSLog(@"Yep; works!"); 
} 

古怪不夠;相同的代碼使用alphaValue確實有效:

-(void)someMethod { 
    CAAnimation *anim = [CABasicAnimation animation]; 
    [anim setDelegate:self]; 
    [[currentViewController view] setAnimations:[NSDictionary dictionaryWithObject:anim forKey:@"alphaValue"]]; 
    [[newViewController view] setAnimations:[NSDictionary dictionaryWithObject:anim forKey:@"alphaValue"]]; 

    [[[currentViewController view] animator] setAlphaValue:0.5]; 
    [[[newViewController view] animator] setAlphaValue:0.5]; 
} 

- (void)animationDidStop:(CAAnimation *)animation finished:(BOOL)flag { 
    NSLog(@"Yep; works!"); 
} 

你能告訴我我在這裏失蹤了嗎?我只是不明白。 謝謝你的時間。

回答

1

您可以使用動畫製作器使用顯式動畫。在你的情況下,因爲你想-animationDidStop被調用,所以不要使用動畫。另外,你shouldn't be trying to animate the frame in an explicit animation。要麼爲動畫邊界設置動畫位置,要麼通過將它們添加到視圖的圖層來設置動畫。改變你的代碼是這樣的:

-(void)someMethod 
{ 
    CABasicAnimation *boundsAnim = 
         [CABasicAnimation animationWithKeyPath:@"bounds"]; 
    [boundsAnim setDelegate:self]; 
    [boundsAnim setFromValue:[NSValue valueWithRect:currentTarget]]; 
    [boundsAnim setToValue:[NSValue valueWithRect:newTarget]]; 

    CABasicAnimation *positionAnim = 
         [CABasicAnimation animationWithKeyPath:@"position"]; 
    [positionAnim setDelegate:self]; 
    [positionAnim setFromValue:[NSValue valueWithPoint:currentPosition]]; 
    [positionAnim setToValue:[NSValue valueWithRect:newPosition]]; 

    [[[currentViewController view] layer] addAnimation:boundsAnim forKey:nil]; 
    [[[currentViewController view] layer] addAnimation:positionAnim forKey:nil]; 
} 

因爲這是OSX,你將不得不確保你的視圖的圖層是層支持。你可以通過撥打電話:

[[currentViewController view] setWantsLayer:YES]; 
2

可以使用動畫設置器激活委託方法來設置幀。要做到這一點,你添加動畫的單獨frameSizeframeOrigin鍵:

-(void)someMethod { 
    CAAnimation *anim = [CABasicAnimation animation]; 
    [anim setDelegate:self]; 

    [[currentViewController view] setAnimations:[NSDictionary dictionaryWithObject:anim forKey:@"frameOrigin"]]; 
    [[currentViewController view] setAnimations:[NSDictionary dictionaryWithObject:anim forKey:@"frameSize"]]; 
    [[newViewController view] setAnimations:[NSDictionary dictionaryWithObject:anim forKey:@"frameOrigin"]]; 
    [[newViewController view] setAnimations:[NSDictionary dictionaryWithObject:anim forKey:@"frameSize"]]; 

    [NSAnimationContext beginGrouping]; 
    [[[currentViewController view] animator] setFrame:currentTarget]; 
    [[[newViewController view] animator] setFrame:newTarget]; 
    [NSAnimationContext endGrouping]; 
} 

- (void)animationDidStop:(CAAnimation *)animation finished:(BOOL)flag { 
    NSLog(@"Yep; works!"); 
} 

在這個例子中你的委託方法將火四個動畫。如果您未重新調整大小而重新定位視圖,則無需使用frameSize動畫。