ios - UIAttachmentBehavior 中的长度是如何确定的?

标签 ios cocoa-touch ios7 uikit-dynamics

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: 时,它是 offsetFromCenterattachedToAnchor 之间的距离。

例如,考虑这样一个手势识别器:

- (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/

相关文章:

iphone - UIAlertView 按钮( subview )未在 iOS 7 中显示

iOS - 将图像裁剪到检测到的面部时出现问题

objective-c - 从未调用可达性通知

cocoa-touch - 使用 NSURLConnection 忽略证书错误

objective-c - 真实设备上的 NSDictionary 错误

iphone - iOS 7 - 自定义 UIView 的动态壁纸的行为

ios - CLLocationManager 委托(delegate)方法未被调用

ios - UILocalNotification : detecting alert display

ios - CATransform3DRotate 之后的 View 裁剪

iphone - 当带有核心数据的 UITableView 部分的最后一行被移动时 -> NSRangeException