1

The documentation of the length property in UIAttachmentView is the following:

Use this property to adjust the attachment length, if you want to, after creating an attachment. The system sets initial length automatically based on how you initialize the attachment.

My question is regarding the last sentence: how is the initial length calculated?

4

1 回答 1

2

初始长度由您首次创建附件行为时选择的锚点决定。例如,它是您调用时offsetFromCenter到的距离。attachedToAnchorinitWithItem: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 回答