0

I am trying to get a UIView that is in the shape of a circle to "bounce" using UIKit Dynamics. It is supposed to bounce when it reaches the bottom of the screen. The circle is a standard UIView that I create in the viewDidLoad method as follows:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.orangeBall = [[UIView alloc] initWithFrame:CGRectMake(100.0, 100.0, 50.0, 50.0)];
    self.orangeBall.backgroundColor = [UIColor orangeColor];
    self.orangeBall.layer.cornerRadius = 25.0;
    self.orangeBall.layer.borderColor = [UIColor blackColor].CGColor;
    self.orangeBall.layer.borderWidth = 0.0;
    [self.view addSubview:self.orangeBall];

    // Initialize the property UIDynamicAnimator
    self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];

    [self demoGravity];

}

I am trying to get the bounce effect done inside the demoGravity method as follows:

-(void)demoGravity{

UIGravityBehavior *gravityBehavior = [[UIGravityBehavior alloc] initWithItems:@[self.orangeBall]];

[self.animator addBehavior:gravityBehavior];

UICollisionBehavior *collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[self.orangeBall]];

[collisionBehavior addBoundaryWithIdentifier:@"myView"
                                   fromPoint:self.orangeBall.frame.origin
                                     toPoint:CGPointMake(self.orangeBall.frame.origin.x, self.orangeBall.frame.origin.y + self.view.frame.size.height)];

[self.animator addBehavior:collisionBehavior];
}

I want the "ball" to fall straight down on the screen, and once it reaches the bottom of the view, to stop, and "bounce". However, when I run my app, all I see is the circle at the top of the screen fall off the screen. What am I doing wrong?

halfer
  • 19,824
  • 17
  • 99
  • 186
syedfa
  • 2,801
  • 1
  • 41
  • 74
  • I originally asked why the ball was not falling and bouncing. Not simply why it was motionless. I edited the code in the question, as well as the concluding problem to incorporate your suggestion, but unfortunately I still was experiencing trouble. You pointed to your sample code in your book, which I did, and saw the relevant block of code where you added the behaviour in your "doButton" method, but I unfortunately still haven't found a solution. – syedfa Jun 18 '15 at 01:28

1 Answers1

0

The reason why my ball was not bouncing was two fold:

  1. It wasn't falling because I did not have a gravityBehaviour attached to my UIDynamicAnimator object:

    UIGravityBehavior *gravityBehavior = [[UIGravityBehavior alloc] initWithItems:@[self.orangeBall]];
    
    [self.animator addBehavior:gravityBehavior];
    
  2. It wasn't bouncing because I needed to set the boolean property of CollisionBehaviour: "TranslatesReferenceBoundsIntoBoundary" to "YES".

    [collisionBehavior setTranslatesReferenceBoundsIntoBoundary:YES];
    

A special thanks to Ray Wenderlich where the following article provided me with the solution, and the following paragraph is a direct quote from the article:

Rather than explicitly adding boundary co-ordinates, the above code sets the translatesReferenceBoundsIntoBoundary property to YES. This causes the boundary to use the bounds of the reference view supplied to the UIDynamicAnimator.

halfer
  • 19,824
  • 17
  • 99
  • 186
syedfa
  • 2,801
  • 1
  • 41
  • 74