In addition to gravity and other physics properties, UIKit Dynamics also allows your physics objects to interact with one another as they would in the real physical world. For example, we can use the UIAttachmentBehavior
method to link items together as if they are attached with an invisible brace. Let's have our application create a new square view and then attach it to our food image view, but only when a collision occurs. Because our food image view will bounce a couple of times, the collision will be detected each time. To keep from creating multiple squares, let's create another instance variable to keep track of the first bounce.
Add the following line of code in the implementation block in FoodDetailViewController.m
:
BOOL firstBounce;
Now replace our delegate method code with the following code:
- (void)collisionBehavior:(UICollisionBehavior *)behavior beganContactForItem:(id<UIDynamicItem>)item withBoundaryIdentifier:(id<NSCopying>)identifier...