iosobjective-cuiviewuikit-dynamics

Trying to get UIView to bounce using UIKit Dynamics


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?


Solution

  • 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.