4

NSImageView で png シーケンスをアニメーション化したいのですが、うまくいきません。アニメーションを表示したくないだけです。なにか提案を?

これは私のコードです:

- (void) imageAnimation {
  NSMutableArray *iconImages = [[NSMutableArray alloc] init];
  for (int i=0; i<=159; i++) {
    NSString *imagePath = [NSString stringWithFormat:@"%@_%05d",@"clear",i];
    [iconImages addObject:(id)[NSImage imageNamed:imagePath]];
    //NSImage *iconImage = [NSImage imageNamed:imagePath];
    //[iconImages addObject:(__bridge id)CGImageCreateWithNSImage(iconImage)];
  }


  CALayer *layer = [CALayer layer];
  CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"contents"];
  [animation setCalculationMode:kCAAnimationDiscrete];
  [animation setDuration:10.0f];
  [animation setRepeatCount:HUGE_VALF];
  [animation setValues:iconImages];

  [layer setFrame:NSMakeRect(0, 0, 104, 104)];
  layer.bounds = NSMakeRect(0, 0, 104, 104);
  [layer addAnimation:animation forKey:@"contents"];

  //Add to the NSImageView layer
  [iconV.layer addSublayer:layer];
}
4

2 に答える 2

5
tl;dr:

を呼び出して、ビューをレイヤーホスティングにします

[iconV setLayer:[CALayer layer]];
[iconV setWantsLayer:YES];

なぜ何も起こらないのか

何も起こらない理由は、画像ビューにレイヤーがないためです。呼び出し時に[iconV.layer addSublayer:layer];メッセージを送信しnilても何も起こりません (サブレイヤーは画像ビューに追加されません)。

OS X のビューは、下位​​互換性のために、デフォルトでバッキング ストアとしてコア アニメーション レイヤーを使用しません。レイヤーを含むビューは、layer-backedまたはlayer-hostingのいずれかになります。

レイヤーに裏打ちされたビューを直接操作してはならず、レイヤーをホストするビューにビューを追加するべきではありません (ただし、レイヤーの追加は問題ありません) 。レイヤーを画像ビュー レイヤーに追加する (したがって直接操作する) ため、レイヤー ホスティング ビューが必要です。

それを修正する

[iconV setLayer:[CALayer layer]];最初に を使用してレイヤーを指定し、次に (順序が重要です) を使用してレイヤーが必要であることをビューに伝えることで、ビューがレイヤーをホストする必要があることを伝えることができます[iconV setWantsLayer:YES];

レイヤーに基づくビューとレイヤーをホストするビューの詳細については、 のドキュメントをwantsLayer参照してください。

于 2012-10-22T13:41:53.157 に答える