1

I have a UIVIew (container) and another UIView (box) the box view is inside of the ContainerView. When a UIButton is pressed I would like the box view to drop down off the bottom of the screen, and bounce with 10px left; then once the bouncing has stopped I still want the box to have 10px showing. Here is some sample code of from another question:

UIDynamicAnimator *animator = [[UIDynamicAnimator alloc] initWithReferenceView:self]; //self is the container

UIGravityBehavior* gravityBehavior = [[UIGravityBehavior alloc] initWithItems:@[box]];
[animator addBehavior:gravityBehavior];

UICollisionBehavior* collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[reportBar.bar]];
collisionBehavior.translatesReferenceBoundsIntoBoundary = YES;
[animator addBehavior:collisionBehavior];

UIDynamicItemBehavior *elasticityBehavior =
[[UIDynamicItemBehavior alloc] initWithItems:@[box]];
elasticityBehavior.elasticity = 0.7f;
[animator addBehavior:elasticityBehavior];

The code is running when it should but the box isn't dropping.

example of layout

Edit 1:

  • Self refers to the container UIView
  • I have also tried changing self for the currentViewController.view, no change.

Edit 2:

  • all of this code is inside the container view implementation file.
Community
  • 1
  • 1
Alex Pelletier
  • 4,933
  • 6
  • 34
  • 58
  • UIDynamicAnimator *animator = [[UIDynamicAnimator alloc] initWithReferenceView:self]; , what is self here? if you have written this code in controller it will be UIDynamicAnimator *animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view]; – BaSha May 28 '14 at 05:24
  • self is the container; added it in a comment in the code. – Alex Pelletier May 28 '14 at 05:27
  • The code that you posted is that inside of the containerView implementation? – Yan May 28 '14 at 05:30
  • Yes, all of the code is inside the container view .m – Alex Pelletier May 28 '14 at 05:33
  • Shouldn't this code be inside of the veiwcontroller and then you will reference the containerView – Yan May 28 '14 at 05:35

3 Answers3

3

give a try to make animator property,

@property UIDynamicAnimator *animator;

and then your code,

_animator = [[UIDynamicAnimator alloc] initWithReferenceView:self];

 ...
BaSha
  • 2,356
  • 3
  • 21
  • 38
  • Can you explain why this works, and why it didn't work before. Also thanks a ton for this. – Alex Pelletier May 28 '14 at 05:47
  • I guess issue was with instance variable, animator could not communicate with iOS physics engine. Alternatively if we make animator globally accessible then also it will work.. UIDynamicAnimator *animator; and then referring it by self.animator – BaSha May 28 '14 at 06:27
1

I think you didn't specify the correct reference view. It should be either self.view or self.containerView

UIDynamicAnimator *animator = [[UIDynamicAnimator alloc] initWithReferenceView:self];

Update I think you should put the code in the ViewController for this scene and as @BaSha suggested create a animator property and after a button click you will add the behavior and will reference self.containerView Just make sure that boxView is inside of the containerView

Yan
  • 3,533
  • 4
  • 24
  • 45
  • self is the container view, so I think just self will work. Should I pass in another value or property? – Alex Pelletier May 28 '14 at 05:35
  • Self.view , when you are inside of a view controller, returns the UIView of the view controler. self is the UIView, and if I try and use self.view I get an error. – Alex Pelletier May 28 '14 at 05:40
  • Changing the animator to a property fixed it, but how can I have the box drop partially out of the reference view? – Alex Pelletier May 28 '14 at 05:53
  • 1
    @AlexPelletier : i guess you may have to set custom boundary for that instead collisionBehavior.translatesReferenceBoundsIntoBoundary = YES; – BaSha May 28 '14 at 06:29
1

The following code snippet can provide BOUNCE EFFECT on your views.

CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath:@"position.y"];</br>
[animation setFromValue:[NSNumber numberWithFloat:y-position1]];   
[animation setToValue:[NSNumber numberWithFloat:y-position2]];
[animation setDuration:.7];    //   time gap between the bounces
animation.repeatCount=500;    //    no:of times bounce effect has to be done 
[YOURVIEW.layer addAnimation:animation forKey:@"somekey"];
Alen Alexander
  • 725
  • 6
  • 22
  • The above code is to implement Vertical Bounce effect. To make it a horizontal bounce effect,just change the 1st 3 lines as: CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath:@"position.x"]; [animation setFromValue:[NSNumber numberWithFloat:x-position1]]; [animation setToValue:[NSNumber numberWithFloat:x-position2]]; – Alen Alexander Feb 27 '15 at 05:19