0

I have a view with the following hierarchy:

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)

When a gesture recogniser begins i detect the view and keep the attachment and set its anchor point, based on the original view center at the begin state of the recogniser. This works fine if the animator doesnt have any intermediate view between it, however if it has, the drag gesture does not map to its position correctly.

The code that changes handles all this is:

@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;

        }
    }

}

I have a couple issues:
- the container view is always in front of my animated buttons (there is no behaviors associated with this view), why does this happen, is it caused by the animator being associated with the containerView superview? how can this be fixed if need the container view to position UI elements centered in the screen?
- the attachment behaviour doesnt work properly as it causes the the view to swing (like a pendulum) with a big offset margin.

Any help would be appreciated.

Thanks

RicardoDuarte
  • 728
  • 7
  • 23
  • I would just create another view on top of the other one only for user interaction. Then it would always be in front and receive touch events. – mhillsman Oct 15 '15 at 02:26
  • sorry what you mean? – RicardoDuarte Oct 15 '15 at 07:58
  • Based on your description of the problem, it looks like one of your views is intercepting the touch events. This can be solved by either disabling the touch events in the view that is intercepting them through `view.userInteractionEnabled = false` or but placing a view, whose sole purpose is to receive touch events, on top of the obstructing view. – mhillsman Oct 16 '15 at 18:35

0 Answers0