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