Debugging physics is a thing. This is probably not something that iOS users tend to think a lot, as they usually did very simple things using UIKit Dynamics. This is a bit of a shame, as it is one of the best aspects of the latest iOS releases, and offers a really interesting way to make an impressive user experience.
So ... how to debug physics?
One way is to mentally imagine what is happening, and then compare it with what is happening, and find the dissonance between the imaginary and the real, and then solve the problem through a combination of elimination processes, mental or real trial and error and deduction, until the problem is defined and not resolved.
Another is a visual image of everything that is created and interacts, presenting sufficient feedback to more quickly determine the nature and extent of elements, their relationships and incidents / events, as well as solve problems with a literal sight.
To this end, from the moment of their creation, various visual debuggers and developers of physical modeling have been created.
Unfortunately, iOS does not have such an on-screen editor or “scene editor” for UIKit Dynamics, and what is available for this kind of visual debugging in the sprite set and scene set is rudimentary at best.
However, there are CALayers that are present in all UIKit Views, into which CAShapeLayers can be manually created and drawn to accurately represent any physical elements, their boundaries and their bindings and relationships.
CAShapeLayers are a “container” for CGPaths and can have different colors for outline and fill and more than one CGPath element within the same CAShapeLayer.
And to quote the great Rob:
“If you add CAShapeLayer as a layer to the view, you don’t need to embed any drawing code yourself. Just add CAShapeLayer and you're done. You can even change the path later, for example, and it will automatically redraw it for you. CAShapeLayer takes you out of the weeds writing your own drawRect or drawLayer routines. "
If you have a huge number of interacting elements and you want to debug them, you may experience CAShapeLayer performance problems, after which you can use shouldRasterize to convert each to a bitmap and achieve a significant increase in performance when you reach the set limits for the "dynamic" capabilities of CAShapeLayers.
In addition, to represent things like restraints and joints, there is a simple process for creating dashed lines on CAShapeLayers by simply setting properties. Here are the basics of setting CAShapeLayer properties and how to use the array to create a 5-5-5 dashed outline with a block pitch of 3 width without filling.
CAShapeLayer *shapeLayer = [CAShapeLayer layer]; [shapeLayer setBounds:self.bounds]; [shapeLayer setPosition:self.center]; [shapeLayer setFillColor:[[UIColor clearColor] CGColor]]; [shapeLayer setStrokeColor:[[UIColor blackColor] CGColor]]; [shapeLayer setLineWidth:3.0f]; [shapeLayer setLineJoin:kCALineJoinRound]; [shapeLayer setLineDashPattern: [NSArray arrayWithObjects:[NSNumber numberWithInt:10], [NSNumber numberWithInt:5],nil]];