documentation UIAttachmentView 中的 length 属性如下:
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.
我的问题是关于最后一句话:初始长度是如何计算的?
最佳答案
初始长度由您首次创建附件行为时选择的 anchor 决定。例如,当您调用 initWithItem:offsetFromCenter:attachedToAnchor:
时,它是 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] 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
看来,如果您在实例化 UIAttachmentBehavior
之后(但在将行为添加到动画器之前)立即查看 length
,则 length
似乎为零。但是,一旦您将行为添加到动画制作器中,length
就会正确更新。
关于ios - UIAttachmentBehavior 中的长度是如何确定的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23584737/