[英]How is length in UIAttachmentBehavior determined?
UIAttachmentView中的length屬性的文檔如下:
創建附件后,可以根據需要使用此屬性來調整附件的長度。 系統會根據您初始化附件的方式自動設置初始長度。
我的問題是關於最后一句話:初始長度是如何計算的?
首次創建附件時,初始長度由錨的選擇決定。 例如,當您調用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
就會正確更新。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.