Search code examples
iosswiftuikituikit-dynamicsuidynamicanimator

Adding more than 32 UIFieldBehaviors to a UIDynamicAnimator causes a SIGABRT


I have the following code. When I iterate 25 times (5 * 5), it works fine. When I iterate 36 times (6 * 6 in the for loops), it crashes with SIGABRT. I've pasted an image of the error (Playgrounds in Xcode won't let me copy paste it). Is this an internal bug, is there a limit of 32 behaviours defined somewhere, or is this a bug in my code? Thank you! enter image description here

//: A UIKit based Playground for presenting user interface

import UIKit
import PlaygroundSupport

class MyViewController : UIViewController {
    var animator : UIDynamicAnimator!

    var springfields : [UIFieldBehavior] = []

    override func loadView() {
        let view = UIView()
        view.backgroundColor = .white
        animator = UIDynamicAnimator(referenceView: view)

        for i in 1...6 {
            for j in 1...6 {
                let newView = UIView()
                let dotwidth = 12
                newView.layer.cornerRadius = CGFloat(dotwidth/2);
                newView.frame = CGRect(x:20+i*25, y:20+j*25, width:dotwidth, height:dotwidth)
                newView.clipsToBounds = true
                newView.backgroundColor = .black

                let newSpringField = UIFieldBehavior.springField()
                springfields.append(newSpringField)
                newSpringField.addItem(newView)
                newSpringField.position = newView.center

                view.addSubview(newView)

                animator.addBehavior(newSpringField)
            }
        }

        self.view = view
    }
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()
PlaygroundPage.current.needsIndefiniteExecution = true

Solution

  • Running your code in an app, the Debug console output is:

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