初始长度由您首次创建附件行为时选择的锚点决定。例如,它是您调用时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
就会正确更新。