UICollisionBehavior not working in landscape mode

122 views Asked by At

I have two methods, setupAnimation and startAnimating. I am using UIDynamics, specifically 4 separate UIDynamicAnimators with 4 separate UIGravityBehaviorseach containing just one item (because the gravity is in different directions for each), and one additional UIDynamicAnimator with all my items and a UICollisionBehavior.

Here is my code:

This is my viewWillAppear method. I've put the two animation methods here in an attempt to use the updated landscape bounds, but it still does not work.

-(void)viewWillAppear:(BOOL)animated
{

    [self displayNextQuestion];
    self.questionLabel.text = question[@"Question"];
    [self setupAnimation];

    [self startAnimating];


    self.bounceAnimator = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];
    self.collisions = [[UICollisionBehavior alloc]initWithItems:self.answers];
    self.collisions.translatesReferenceBoundsIntoBoundary = YES;
    [self.bounceAnimator addBehavior:self.collisions];






    NSLog(@"VIEW WILL APEAR");
    if(self.bounceAnimator)
        NSLog(@"Animator exists");
    else
        NSLog(@"Animator doesnt exist");
    if([self.bounceAnimator.behaviors containsObject:self.collisions])
        NSLog(@"Collisions is in Animtor");
    else
        NSLog(@"Collission is NOT in animator");
}

My viewDidLoad method:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupQuestions];
    [self performSelectorInBackground:@selector(getNextQuestion) withObject:self];
    [self addToSubviews];
}

And here are my two animation methods:

setupAnimation

-(void)setupAnimation
{
    self.animator1 = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];
    self.animator2 = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];
    self.animator3 = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];
    self.animator4 = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];


    self.gravity1 = [[UIGravityBehavior alloc]init];
    self.gravity2 = [[UIGravityBehavior alloc]init];
    self.gravity3 = [[UIGravityBehavior alloc]init];
    self.gravity4 = [[UIGravityBehavior alloc]init];

    [self.animator1 addBehavior:self.gravity1];
    [self.animator2 addBehavior:self.gravity2];
    [self.animator3 addBehavior:self.gravity3];
    [self.animator4 addBehavior:self.gravity4];



    [self.gravity1 setGravityDirection:CGVectorMake(0.0, 0.05)];

    //G1

 [self.gravity2 setGravityDirection:CGVectorMake(0.0, -0.05)];

    //G2
 [self.gravity3 setGravityDirection:CGVectorMake(0.05, 0.0)];

    //G3

  [self.gravity4 setGravityDirection:CGVectorMake(-0.05, 0.0)];

    //G4






}

startAnimating

-(void)startAnimating
{
    [self.gravity1 addItem:self.answers[0]];
    [self.gravity2 addItem:self.answers[1]];
    [self.gravity3 addItem:self.answers[2]];
    [self.gravity4 addItem:self.answers[3]];
    [self.collisions addItem:self.answers[0]];
    [self.collisions addItem:self.answers[1]];
    [self.collisions addItem:self.answers[2]];
    [self.collisions addItem:self.answers[3]];

}

I'm encountering problems regarding landscape mode and the view bounds.

Basically, the items appear on screen correctly, and the gravity behaviors apply on them correctly. However, no collisions happen when they hit the view "walls" or bounds. I also have a modal unwind segue on this view controller. I have one item whose gravity goes upward. When I tap the button to start the segue while the item is moving upward, I see a collision with the view bounds.

I suspect that this has something to do with the view controller life cycle and that the view bounds are not set in viewDidLoad, but I am not entirely sure how to fix it. Please help!

EDIT: This is what the NSLogs show:

2014-08-16 19:02:37.504 FermiGame[9709:60b] VIEW DID LOAD
2014-08-16 19:02:37.504 FermiGame[9709:60b] Animator doesnt exist
2014-08-16 19:02:37.504 FermiGame[9709:60b] Collission is NOT in animator
2014-08-16 19:02:37.506 FermiGame[9709:60b] VIEW WILL APEAR
2014-08-16 19:02:37.506 FermiGame[9709:60b] Animator exists
2014-08-16 19:02:37.507 FermiGame[9709:60b] Collisions is in Animtor
2014-08-16 19:02:38.024 FermiGame[9709:60b] VIEW DID APPEAR
2014-08-16 19:02:38.025 FermiGame[9709:60b] Animator exists
2014-08-16 19:02:38.026 FermiGame[9709:60b] Collisions is in Animtor
0

There are 0 answers