简体   繁体   English

CollisionBehaviour不适用于自动版式

[英]CollisionBehaviour do not working with autolayout

I am trying to implement collisions between two views. 我正在尝试实现两个视图之间的冲突。

firstView is view with textfields. firstView是带有文本字段的视图。 If some textfield became firstRecognizer and keyboard frame overlap firstView . 如果某个文本字段变为firstRecognizer,并且键盘框架与firstView重叠。 It moves up and if firstView collides with secondView , it should push it up too. 它向上移动,如果firstView与碰撞secondView ,应该将其推过。 But this does not happen. 但这不会发生。 Instead firstView moves up until collides with secondView and then puts back on old place. 相反, firstView向上移动直到与secondView碰撞,然后放回原处。

Here code, which I use: 这是我使用的代码:

- (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) {
            //
        }];
    }
}

Could somebody tell my mistake? 有人可以告诉我我的错误吗? Or, may be somebody know better solution? 或者,也许有人知道更好的解决方案?

I found solution. 我找到了解决方案。 My mistake was to add collision behaviour to animator before calling layoutIfNeeded . 我的错误是在调用layoutIfNeeded之前将碰撞行为添加到动画师。 Now I call layoutIfNeeded before adding collision behaviour and it works well. 现在,我在添加碰撞行为之前调用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) {
                    //
                }
            }];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM