The initial length is dictated by the choice of anchor when you first create the attachment behavior. For example, it's the distance between the offsetFromCenter
to the attachedToAnchor
when you call initWithItem:offsetFromCenter:attachedToAnchor:
.
For example, consider a gesture recognizer like so:
- (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;
}
}
This reports:
2014-05-10 14:50:03.590 MyApp[16937:60b] before adding behavior to animator: length = 0 2014-05-10 14:50:03.594 MyApp[16937:60b] after adding behavior to animator: length = 43 2014-05-10 14:50:03.606 MyApp[16937:60b] during gesture: length = 43 2014-05-10 14:50:03.607 MyApp[16937:60b] during gesture: length = 43
It appears that if you look at the length
immediately after instantiating the UIAttachmentBehavior
(but before adding the behavior to the animator), the length
appears to be zero. But as soon as you add the behavior to the animator, the length
is correctly updated.