1

UIAttachmentView の length プロパティのドキュメントは次のとおりです。

必要に応じて、アタッチメントの作成後にアタッチメントの長さを調整するには、このプロパティを使用します。アタッチメントの初期化方法に基づいて、初期の長さが自動的に設定されます。

私の質問は最後の文に関するものです。最初の長さはどのように計算されますか?

4

1 に答える 1

2

最初の長さは、アタッチメント動作を最初に作成するときに選択したアンカーによって決定されます。たとえば、を呼び出しoffsetFromCenterattachedToAnchorときの と の間の距離です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が正しく更新されます。

于 2014-05-10T18:39:55.630 に答える