3

コードを使用してレイヤー ホース ビューをアニメーションで移動できます。

[CATransaction setAnimationDuration:2];
self.imageLayer.position = CGPointMake(0, 0);

次のコードでも実行できます。

 [NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
    [context setDuration:2];
    self.imageLayer.position = CGPointMake(0, 0);
} completionHandler:nil];

CATransaction と NSAnimationContext の違いを知りたいのですが、どちらを使用してビューをアニメーション化する必要がありますか? NSAnimationContext と CATransaction で期間を設定できるのはなぜですか?

4

2 に答える 2

4

CATransaction はコア アニメーション レイヤーのアニメーションで機能します。CATransaction は、iOS と Mac OS 間のクロスプラットフォームです。

NSAnimationContext は NSAnimationContext と連動します。これは Mac OS 固有のものです。

(NS アニメーションは Mac 専用で、UIView アニメーションは iOS 専用です。)

最近は Mac OS よりも iOS で作業することが多く、常にクロスプラットフォームの方法を探しています。

CAAnimation、CALayer、CAAnimationGroup などは、Mac と iOS でほぼ同じです。いくつかの違いがあります (たとえば、quicktime レイヤーは iOS ではサポートされていない、Core Image のサポートは iOS ではより制限されているなど) が、CA のものは異なるよりも似ています。

于 2012-09-10T20:22:57.990 に答える
4

ほとんどの場合、NSAnimationContextのラッパーとして実装されますCATransactionHopperのような逆アセンブラー ツールを使用して、AppKit のコードをリバース エンジニアリングし、これを自分で確認できます。NSAnimationContext擬似Objective-Cで再構築された のメソッドの一部を次に示します。

void +[NSAnimationContext beginGrouping](void * self, void * _cmd) {
  if (([CATransaction currentState] & 0x3) != 0x0) {
    [CATransaction begin];
  } else {
    rdi = @class(CATransaction);
    if (pthread_main_np() != 0x0) {
      [rdi activate];
      [CATransaction begin];
    }
    else {
      [rdi begin];
      [CATransaction activateBackground:0x1];
    }
  }
  [CATransaction setValue:@(YES) forKey:@"NSAnimationContextBeganGroup"];
  return;
}

void +[NSAnimationContext endGrouping](void * self, void * _cmd) {
  [CATransaction commit];
  return;
}

void -[NSAnimationContext setTimingFunction:](void * self, void * _cmd, void * arg2) {
  [CATransaction setAnimationTimingFunction:arg2];
  return;
}

さらに、これNSAnimationContextには、Shift キーが押されている場合のアニメーションの長さの変更 ( NSAnimationSlowMotionOnShift) やパフォーマンスの測定など、いくつかの追加機能があります。

于 2016-09-28T07:47:50.987 に答える