UIAttachmentView の length プロパティのドキュメントは次のとおりです。
必要に応じて、アタッチメントの作成後にアタッチメントの長さを調整するには、このプロパティを使用します。アタッチメントの初期化方法に基づいて、初期の長さが自動的に設定されます。
私の質問は最後の文に関するものです。最初の長さはどのように計算されますか?
UIAttachmentView の length プロパティのドキュメントは次のとおりです。
必要に応じて、アタッチメントの作成後にアタッチメントの長さを調整するには、このプロパティを使用します。アタッチメントの初期化方法に基づいて、初期の長さが自動的に設定されます。
私の質問は最後の文に関するものです。最初の長さはどのように計算されますか?
最初の長さは、アタッチメント動作を最初に作成するときに選択したアンカーによって決定されます。たとえば、を呼び出しoffsetFromCenter
たattachedToAnchor
ときの と の間の距離ですinitWithItem:offsetFromCenter:attachedToAnchor:
。
たとえば、次のようなジェスチャ レコグナイザーを考えてみましょう。
- (void)handlePan:(UIPanGestureRecognizer *)gesture
{
static UIAttachmentBehavior *attachment;
CGPoint location = [gesture locationInView:self.animator.referenceView];
if (gesture.state == UIGestureRecognizerStateBegan) {
attachment = [[UIAttachmentBehavior alloc] initWithItem:self.viewToAnimate attachedToAnchor:location];
NSLog(@"before adding behavior to animator: length = %.0f", attachment.length); // this says zero, even though it's not really
[self.animator addBehavior:attachment];
NSLog(@"after adding behavior to animator: length = %.0f", attachment.length); // this correctly reflects the length
} else if (gesture.state == UIGestureRecognizerStateChanged) {
attachment.anchorPoint = location;
NSLog(@"during gesture: length = %.0f", attachment.length); // this correctly reflects the length
} else if (gesture.state == UIGestureRecognizerStateEnded || gesture.state == UIGestureRecognizerStateCancelled) {
[self.animator removeBehavior:attachment];
attachment = nil;
}
}
これは次のように報告します。
2014-05-10 14:50:03.590 MyApp[16937:60b] アニメーターに動作を追加する前: 長さ = 0 2014-05-10 14:50:03.594 MyApp[16937:60b] アニメーターに動作を追加した後: 長さ = 43 2014-05-10 14:50:03.606 ジェスチャ中の MyApp[16937:60b]: 長さ = 43 2014-05-10 14:50:03.607 ジェスチャ中の MyApp[16937:60b]: 長さ = 43
をインスタンス化したlength
直後UIAttachmentBehavior
(ただし動作をアニメーターに追加する前) を見ると、 はlength
ゼロのように見えます。しかし、アニメーターに動作を追加するとすぐに、length
が正しく更新されます。