2011-07-18 25 views
3

コアアニメーションを使用したカカオアプリケーション用のコードスニペットは、どういうわけかアニメーションが表示されません。アニメーションが表示されません

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"]; 
[animation setDelegate:self]; 

NSRect pos = [imageView frame]; 
[animation setFromValue:[NSValue valueWithRect:pos]]; 
NSPoint point = NSMakePoint(pos.origin.x-40, pos.origin.y); 

[animation setToValue:[NSValue valueWithPoint:point]]; 
[animation setDuration:2.0]; 

[[imageView animator] addAnimation:animation forKey:@"myTest"]; 

これは、作業コードである間:

NSRect position = [imageView frame]; 
position.origin.x -= 40; 
[[imageView animator] setFrame:position]; 

しかし、オートリバースは動作しません。

最初のものは何も間違っていますか?そして、逆の動きを2番目のものにするには?ありがとう!

答えて

0

確かにわかりませんが、最初のものはCGPointなので、そのタイプをfromValueとtoValueに使ってみてください。あなたは現在、NSRectとNSPointを使用しています(後者は動作するはずですが、前者についてはわかりません)。

次に、自動リバースをどのように指定していますか? +アニメーションブロック内から+ setAnimationRepeatAutoreversesを呼び出す必要があります(「beginAnimations」の後)

0

(注:私は主にiOS開発者ですから、経験はあまりありません。次のもの)

私は、あなたがCoreAnimationとAnimator Proxyを混ぜようとしているというのが問題だと思います。あなたはアニメーターに、しかし、レイヤーにアニメーションを追加しないでください:

[[imageView layer] addAnimation:animation forKey:@"myTest"]; 

別の可能性がNSViewAnimationおよびそれらを一緒にチェーンを使用するかもしれません。 Animation Programming Guide for Cocoa(13ページ)を参照してください.1つのアニメーションを一方向に移動し、終了すると2つ目のアニメーションをトリガーします。このように動作しているようです:

NSMutableDictionary *firstDict = [NSMutableDictionary dictionary]; 
[firstDict setObject:imageView forKey:NSViewAnimationTargetKey]; 
[firstDict setObject:[NSValue valueWithRect:originalFrame] forKey:NSViewAnimationStartFrameKey]; 
[firstDict setObject:[NSValue valueWithRect:targetFrame] forKey:NSViewAnimationEndFrameKey]; 

NSMutableDictionary *secondDict = [NSMutableDictionary dictionary]; 
[secondDict setObject:imageView forKey:NSViewAnimationTargetKey]; 
[secondDict setObject:[NSValue valueWithRect:targetFrame] forKey:NSViewAnimationStartFrameKey]; 
[secondDict setObject:[NSValue valueWithRect:originalFrame] forKey:NSViewAnimationEndFrameKey]; 

NSViewAnimation *firstAnimation = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:firstDict]]; 
[firstAnimation setDuration:2.0]; 

NSViewAnimation *secondAnimation = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:secondDict]]; 
[secondAnimation setDuration:2.0]; 

[secondAnimation startWhenAnimation:firstAnimation reachesProgress:1.0]; 
[firstAnimation startAnimation]; 

その後、ライオン(OS X 10.7)にあなたがcompletion handler when using Animator Proxyを設定することができます。これは次のように動作します:

[NSAnimationContext beginGrouping]; 
[[NSAnimationContext currentContext] setDuration:2.0]; 
[[NSAnimationContext currentContext] setCompletionHandler:^(void) { 
    // Here comes your code for the reverse animation. 
    [NSAnimationContext beginGrouping]; 
    [[NSAnimationContext currentContext] setDuration:2.0]; 
    [[aView animator] setFrameOrigin:originalPosition]; 
    [NSAnimationContext endGrouping]; 
}]; 
[[aView animator] setFrameOrigin:position]; 
[NSAnimationContext endGrouping]; 
関連する問題