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


Create 'gravity' that affect buttons?

How do I create 'gravity' in my all so when a button is pressed the rest of the buttons will fall to the bottom if the screen. Also how can I have one or two buttons which are immune to this?

Language- Swift

You can add "UIGravityBehavior" to all buttons you want to fall. If you want them to have collision with the screen you need to add "UICollisionBehavior". You can do something like this:

@interface yourClass() @property (strong, nonatomic) UIDynamicAnimator *animator; @end

-(void) animateButton: (UIButton *) button toFall: (BOOL) fallingDown withMagnitud: (CGFloat) magnitude { [self.animator removeAllBehaviors];

[self getFrameFromButton:button];

UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[button]];

UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[button]];

UIDynamicItemBehavior *behavior = [[UIDynamicItemBehavior alloc] initWithItems:@[button]];

[behavior setResistance:1];

[self.animator addBehavior:behavior];

[collision setTranslatesReferenceBoundsIntoBoundary:YES];

collision.collisionDelegate = self.delegate;

collision.collisionMode = UICollisionBehaviorModeEverything;

[gravity setGravityDirection:CGVectorMake(0, 100)];

[gravity setMagnitude:5];

[self.animator addBehavior:gravity];

[self.animator addBehavior:collision];