2015-10-30 106 views
0

我想实现两个视图之间的碰撞。CollisionBehaviour不能使用自动布局

firstView是带有文本框的视图。如果某些文本框成为firstRecognizer和键盘框架重叠firstView。它向上移动,如果firstViewsecondView相冲突,它也应该推高它。但是这并没有发生。而是firstView向上移动,直到与secondView碰撞,然后放回旧地方。

这里的代码,我用:

- (void) keyboardChangeValueWithFrame:(CGRect)keyboardFrame isOpening:(BOOL)isOpening isClosing:(BOOL)isClosing { 
    CGFloat animTime = .3f; 
    if (isOpening && !isClosing) { 
     CGRect authFillerViewFrame = self.regFillerView.frame; 
     CGFloat rectDif = keyboardFrame.origin.y - authFillerViewFrame.origin.y - authFillerViewFrame.size.height; 
     if ([_animator.behaviors containsObject:_collision]) { 
      [_animator removeBehavior:_collision]; 
     } 
     if (rectDif < 0) { 
      [self.view layoutIfNeeded]; 
      self.regFillerViewHorizCenterConst.constant = rectDif; 
      [self.logoView layoutIfNeeded]; 
      [UIView animateWithDuration:animTime animations:^{ 
       _collision = [[UICollisionBehavior alloc] initWithItems:@[self.regFillerView, self.logoView]]; 
       _collision.translatesReferenceBoundsIntoBoundary = NO; 
       _collision.collisionDelegate = self; 
       _collision.collisionMode = UICollisionBehaviorModeItems; 
       [_animator addBehavior:_collision]; 
       _collision.action = ^{ 

       }; 
       [self.view layoutIfNeeded]; 
      } completion:^(BOOL finished) { 
       if (finished) { 
        // 
       } 
      }]; 
     } 
    } else if (!isOpening && isClosing) { 
     [self.view layoutIfNeeded]; 
     self.regFillerViewHorizCenterConst.constant = 0.f; 
     [UIView animateWithDuration:animTime animations:^{ 
      [self.view layoutIfNeeded]; 
     } completion:^(BOOL finished) { 
      // 
     }]; 
    } 
} 

有人能告诉我的错误?或者,可能有人知道更好的解决方案?

+0

看起来你可以把这些视图放在滚动视图中,然后当键盘显示从超级视图调整滚动视图底部距离。 –

回答

0

我找到了解决方案。我的错误是在致电layoutIfNeeded之前向动画师添加碰撞行为。 现在我在添加碰撞行为之前先打电话layoutIfNeeded,它运作良好。

[UIView animateWithDuration:animTime animations:^{ 
       _collision = [[UICollisionBehavior alloc] initWithItems:@[self.regFillerView, self.logoView]]; 
       [self.view layoutIfNeeded]; 
       _collision.translatesReferenceBoundsIntoBoundary = NO; 
       _collision.collisionDelegate = self; 
       _collision.collisionMode = UICollisionBehaviorModeItems; 
       [_animator addBehavior:_collision]; 
       _collision.action = ^{ 

       }; 
      } completion:^(BOOL finished) { 
       if (finished) { 
        // 
       } 
      }];