我正在尝试在两个视图之间实现冲突。
firstView
是带有文本字段的视图。如果某些文本字段成为firstRecognizer并且键盘框架重叠firstView
。它会向上移动,如果firstView
与secondView
发生碰撞,它也会向上移动。但这不会发生。相反,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) {
//
}
}];