【问题标题】:Animate adding height constraint to keyboard extension view向键盘扩展视图添加高度约束的动画
【发布时间】:2018-09-29 20:31:29
【问题描述】:

在我的键盘扩展中,有一个特定的 viewController 我想让键盘增加高度。在呈现这个 viewController 之前,主 UIInputViewController 自己调用这个方法,传递一个值 400.0:

- (void)setKeyboardHeight:(CGFloat)height {
    if (self.heightConstraint) {
        [self.view removeConstraint:self.heightConstraint];
        self.heightConstraint = nil;
   }

    self.heightConstraint = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:height];

    [self.view addConstraint:self.heightConstraint];

    [UIView animateWithDuration:0.5 animations:^{
        [self.view layoutIfNeeded];
    }];
}

键盘的高度确实更改为指定值,但没有动画。这是不可能完成的,还是我只是在键盘扩展中使用了错误的方法?

【问题讨论】:

    标签: ios autolayout ios-autolayout ios-keyboard-extension


    【解决方案1】:

    这是我想出的一个解决方案。调用animateKeyboardHeightTo: speed:并传入你希望键盘改变的高度为targetHeight,每毫秒的点数为speed,键盘将增加或减少到指定的高度。我尝试使用持续时间而不是速度,每次迭代增加一个点并计算dispatch_after 的持续时间,以便整个动画将在指定的持续时间内发生,但我无法让dispatch_after 足够快地执行。我不知道它是否不够精确,或者它是否不能以小于 1 秒的间隔执行,但这是我能想到的唯一解决方案。

    如果要在大小之间切换,可以在初始调用animateKeyboardHeightTo: speed:之前将self.view.frame.size.height存储在属性中,然后再次调用此方法并传入存储的高度以将键盘恢复到初始高度.

    - (void)animateKeyboardHeightTo:(CGFloat)targetHeight speed:(CGFloat)speed {
        CGFloat initialHeight = self.view.frame.size.height;
    
        if (targetHeight < initialHeight) {
            speed = -speed;
        }
        [self setKeyboardHeight:initialHeight];
        [self increaseKeyboardHeightToTargetHeight:targetHeight speed:speed];
    }
    
    - (void)increaseKeyboardHeightToTargetHeight:(CGFloat)targetHeight speed:(CGFloat)speed {
        CGFloat currentHeight = self.heightConstraint.constant;
        CGFloat nextHeight = currentHeight + speed;
        if ((speed > 0 && nextHeight > targetHeight) || (speed < 0 && nextHeight < targetHeight)) {
            nextHeight = targetHeight;
        }
        [self setKeyboardHeight:nextHeight];
    
        if ((speed > 0 && nextHeight < targetHeight) || (speed < 0 && nextHeight > targetHeight)) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_MSEC)), dispatch_get_main_queue(), ^{
                [self increaseKeyboardHeightToTargetHeight:targetHeight speed:speed];
            });
        }
    }
    
    - (void)setKeyboardHeight:(CGFloat)height {
        [self removeHeightConstraint];
        [self addHeightConstraintWithHeight:height];
    }
    
    - (void)removeHeightConstraint {
        if (self.heightConstraint) {
            [self.view removeConstraint:self.heightConstraint];
            self.heightConstraint = nil;
        }
    }
    
    - (void)addHeightConstraintWithHeight:(CGFloat)height {
        self.heightConstraint = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:height];
    
        [self.view addConstraint:self.heightConstraint];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-30
      • 1970-01-01
      相关资源
      最近更新 更多