Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Photo Browser iPhone App UIKit Dynamics Attaching Snap Behavior to Views

Detail View Controller dismisses before UISnapBehavior completes.

Is anyone else running into this issue? I have tried to add resistance by adding a UIDynamicItemBehavior to my view (in this case, a button named "myPhoto"). But, that did not help either. Here's my code:

- (IBAction)dismissButtonTapped:(id)sender {
    self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
    UIDynamicItemBehavior *resistance = [[UIDynamicItemBehavior alloc] initWithItems:@[self.myPhoto]];
    resistance.resistance = 1000.0f;
    [self.animator addBehavior:resistance];
    UISnapBehavior *snap = [[UISnapBehavior alloc] initWithItem:self.myPhoto
                                                    snapToPoint:CGPointMake(CGRectGetMidX(self.view.bounds), (CGRectGetMaxY(self.view.bounds) +180.0f))];
    [self.animator addBehavior:snap];
    [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}

EDIT to add that if I comment out the code to dismiss the view controller, the snap behavior works as you would expect.

EDIT, again, to add that I found a work around using a half-second delay between the snap behavior and dismissing the view controller. But I have a feeling that this isn't the best way to do things.