iosuigravitybehavior

why my UIGravityBehavior isn't working


i create UIDynamicAnimator and UIGravityBehavior add gravity to animator but gravity doesn't work. and there display my dropView.

-(UIDynamicAnimator *)animaotr
{
    if(!_animator)
    {
        _animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.gameView];
    }
    return _animator;
}
-(UIGravityBehavior *)gravity
{
    if(!_gravity)
    {
        _gravity = [[UIGravityBehavior alloc] init];
        _gravity.magnitude = 1;
        [self.animator addBehavior:_gravity];
    }
    return _gravity;
}

-(void)drop{
CGRect frame;
frame.origin = CGPointZero;
frame.size = DROP_SIZE;
int x = (arc4random()%(int)self.gameView.bounds.size.width) / DROP_SIZE.width;
frame.origin.x = x * DROP_SIZE.width;

UIView *dropView = [[UIView alloc] initWithFrame:frame];
dropView.backgroundColor = [self randomColor];
[self.gameView addSubview:dropView];
[self.gravity addItem:dropView];

}


Solution

  • The problem is this declaration:

    -(UIDynamicAnimator *)animaotr {
    

    That method doesn't match the name self.animator, so it won't be called, and thus your gravity behavior is not being added to any animator.