UIAttachmentBehavior的平移手势与子视图不兼容

时间:2015-10-14 21:25:38

标签: ios objective-c uikit-dynamics

我有一个具有以下层次结构的视图:

self.view (with animator)
  |-> self.view (centered) 
       |-> (view with gravity+push+dynamic+collision+(attachment disabled) behaviors)
       |-> (view with gravity+push+dynamic+collision+(attachment disabled) behaviors)
       |-> (view with gravity+push+dynamic+collision+(attachment disabled) behaviors)
       |-> (view with gravity+push+dynamic+collision+(attachment disabled) behaviors)

当手势识别器开始时,我基于识别器的开始状态下的原始视图中心检测视图并保持附件并设置其锚点。如果动画师之间没有任何中间视图,这可以正常工作,但如果有,则拖动手势不会正确映射到其位置。

更改处理所有这些的代码是:

@IBAction private func handlePanGesture(recognizer: UIPanGestureRecognizer)
{
    let translationPoint:CGPoint = recognizer.translationInView(self.containerView);
    let point:CGPoint = recognizer.locationInView(self.containerView);

    if recognizer.state == UIGestureRecognizerState.Began
    {
        let attachment:UIAttachmentBehavior?;
        let index:Int = self.viewIndexFromPoint(point);
        if (index >= 0)
        {
            attachment = self.attachmentBehaviors[index];
            let view:RoundView = self.buttons[index];

            self.currentAttachmentBehavior = attachment;
            let anchor:CGPoint = CGPointMake(view.center.x + translationPoint.x, view.center.y + translationPoint.y);
            attachment?.anchorPoint = anchor;
            view.panGestureStartPoint = anchor;
            self.animator?.addBehavior(attachment!);
            print("added Attachment \(attachment)");

        }
        else
        {
            attachment = nil;
        }
    }
    else if ((recognizer.state == UIGestureRecognizerState.Ended ||
        recognizer.state == UIGestureRecognizerState.Cancelled ||
        recognizer.state == UIGestureRecognizerState.Failed) && self.currentAttachmentBehavior != nil)
    {
        self.animator?.removeBehavior(self.currentAttachmentBehavior!);
    }
    else
    {
        if let attachment:UIAttachmentBehavior = self.currentAttachmentBehavior
        {

            let index:Int = self.attachmentBehaviors.indexOf(attachment)!;
            let view:RoundView = self.buttons[index];
            let anchor:CGPoint = CGPointMake(view.panGestureStartPoint!.x + translationPoint.x, view.panGestureStartPoint!.y + translationPoint.y);
            attachment.anchorPoint = anchor;

        }
    }

}

我有几个问题:
- 容器视图总是在我的动画按钮前面(没有与此视图关联的行为),为什么会发生这种情况,是因为动画师与containerView超级视图相关联?如果需要容器视图来定位屏幕中心的UI元素,如何解决这个问题呢? - 附件行为无法正常工作,因为它会导致视图摆动(如钟摆),并具有较大的偏移边距。

任何帮助将不胜感激。

由于

0 个答案:

没有答案