0

So I am using UI Kit Dynamics to create a slider which can be swiped out of the way to reveal what is behind it. I am using two UIFieldBehaviour's, an attachment behaviour attached to a UIPanGestureRecognizer, and UICollisionBehaviour to achieve this. The structure in Interface Builder looks like this: Here an image of my view hierarchy

The TrayContainerView has constraints applied to it and it is also the reference view for my DynamicAnimator. trayView is the dynamicItem which dynamicAnimator acts on.

This works fine until I add a subview to trayView from within Interface Builder, at which point I get this message whenever my app switches to that particular viewController:

Terminating app due to uncaught exception 'Invalid Association', reason: 'UIDynamicAnimator supports a maximum of 32 distinct fields'

The strange thing is, if I add the subview programmatically it works fine save for some issues with the layout.

Here's the setup for my dynamics behaviours

    container = UICollisionBehavior(items: [self])

    let boundaryWidth = UIScreen.mainScreen().bounds.size.width
    container.addBoundaryWithIdentifier("upper", fromPoint: CGPointMake(0, offset), toPoint: CGPointMake(boundaryWidth, offset))

    let boundaryHeight = self.superview!.frame.size.height + self.frame.size.height - self.layoutMargins.top
    container.addBoundaryWithIdentifier("lower", fromPoint: CGPointMake(0,boundaryHeight), toPoint: CGPointMake(boundaryWidth,boundaryHeight))

    tractorField = UIFieldBehavior.linearGravityFieldWithVector(CGVectorMake(0, 30))

    tractorField.direction = CGVectorMake(0, -10)

    let referenceRegion = (superview?.frame)!
    let fieldCoefficient:CGFloat = 1
    tractorField.region = UIRegion(size: CGSize(width: referenceRegion.width, height: referenceRegion.height*fieldCoefficient))
    tractorField.position = CGPointMake((self.superview?.center.x)!, referenceRegion.height*fieldCoefficient/2)
    tractorField.addItem(self)

    gravityField = UIFieldBehavior.linearGravityFieldWithVector(CGVectorMake(0, 30))

    gravityField.direction = CGVectorMake(0, 10)


    gravityField.region = UIRegion(size: CGSize(width: referenceRegion.width, height: referenceRegion.height*fieldCoefficient))
    gravityField.position = CGPointMake((self.superview?.center.x)!, referenceRegion.height + referenceRegion.height*fieldCoefficient/2)
    gravityField.addItem(self)


    animator.addBehavior(gravityField)
    animator.addBehavior(tractorField)

    dynamicItem = UIDynamicItemBehavior(items: [self])

    animator.addBehavior(container)

    animator.addBehavior(dynamicItem)

any ideas would be greatly appreciated.

  • I've narrowed down the issue to the use of UIFieldBehaviour. uncommenting both of them will prevent the crash. Using UIGravityBehaviour curiously will not crash the app but it will result in a continuous series of log messages stating "Multiple gravity behavior per animator is undefined and may assert in the future" even though there is no other gravity or field behaviour present – Shamanth Vohra Apr 14 '16 at 19:03

1 Answers1

0

Figured it out. Problem was I tried to subclass UIView and do all of the dynamic stuff in there. Unfortunately that does not work as I suspect the dynamic animator is called to early? I instantiated it in awakeFromNib which is the reason for the crashing. Anyway implementing the whole thing in the viewDidLoad method of a viewController and it all worked as it should.