第11月第31天 keyboardwillshow CGAffineTransformMakeTranslation

本文介绍了一个iOS应用中处理键盘弹出和隐藏时视图变化的方法。通过注册键盘通知并在键盘显示和隐藏时调整tableView和bottomView的位置来实现平滑过渡。文章详细展示了如何使用UIKit中的UIView动画来同步视图变化,确保用户界面的流畅体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)registerNotification {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showKeyboard:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(hideKeyboard:) name:UIKeyboardWillHideNotification object:nil];
}

#pragma mark - keyboard
- (void)showKeyboard:(NSNotification *)noti {
    NSDictionary *info=[noti userInfo];
    
    NSTimeInterval duration = [info[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    UIViewAnimationOptions options = [info[UIKeyboardAnimationCurveUserInfoKey] unsignedIntegerValue] << 16;
    
    CGRect keyboardRect = [info[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat keyboardHeight = MIN(CGRectGetWidth(keyboardRect), CGRectGetHeight(keyboardRect));
    
    [UIView animateWithDuration:duration delay:0 options:options animations:^{
        CGFloat compareH = self.tableView.contentSize.height -(self.view.height - 45 - 64 - keyboardHeight);
        if (compareH >  0) {
            if (compareH < keyboardHeight) {
                self.tableView.transform = CGAffineTransformMakeTranslation(0, -compareH);
            } else {
                self.tableView.transform = CGAffineTransformMakeTranslation(0, -keyboardHeight);
            }
        }
        
        self.bottomView.transform = CGAffineTransformMakeTranslation(0, -keyboardHeight);
        
    } completion:nil];
}

- (void)hideKeyboard:(NSNotification *)noti {
    
    NSDictionary *info=[noti userInfo];
    
    NSTimeInterval duration = [info[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    UIViewAnimationOptions options = [info[UIKeyboardAnimationCurveUserInfoKey] unsignedIntegerValue] << 16;
    
    [UIView animateWithDuration:duration delay:0 options:options animations:^{
        
        self.tableView.transform = CGAffineTransformIdentity;
        self.bottomView.transform = CGAffineTransformIdentity;
        
    } completion:nil];
    
}

 

转载于:https://www.cnblogs.com/javastart/p/7457470.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值