首页 技术 正文
技术 2022年11月20日
0 收藏 388 点赞 2,978 浏览 2906 个字

1.当view是非可以滚动的view时,

// 添加对键盘的通知
- -(void)viewDidLoad{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- -(void)keyboardWillShow:(NSNotification *)sender{
{ // 得到键盘的高度
NSDictionary *dict = [sender userInfo];
CGSize keyboardSize = [dict[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue].size; // 得到输入框CGRectGetMaxY(<#CGRect rect#>)+40到底部的高度
CGFloat height = (SCREEN_H - (_IPText.frame.origin.y + _IPText.frame.size.height + 40));
CGFloat newY = -keyboardSize.height + height; // 如果键盘的高度大于上面的高度
if (keyboardSize.height > height) { [UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.2];
self.view.frame = CGRectMake(0, newY, SCREEN_W, SCREEN_H);
[UIView commitAnimations]; }
}- (void)keyboardWillHide:(NSNotification *)sender
{
[UIView beginAnimations:nil context:NULL];
// 动画时间3s
[UIView setAnimationDuration:0.2]; self.view.frame = CGRectMake(0, 0, SCREEN_W, SCREEN_H); [UIView commitAnimations];
}- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[_IPText resignFirstResponder];
}

2.类似于聊天框被遮挡。

- (void)viewDidLoad
{
// 监听键盘的弹出与隐藏
// 利用消息中心,首先监听UIKeyboardWillChangeFrameNotification 键盘frame改变的消息
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
}
//监听键盘弹出与退出的方法
-(void)keyboardChangeFrame:(NSNotification *)sender
{
/*
UIKeyboardAnimationCurveUserInfoKey = 7;
UIKeyboardAnimationDurationUserInfoKey = "0.25";
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {375, 216}}";
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {187.5, 775}";
UIKeyboardCenterEndUserInfoKey = "NSPoint: {187.5, 559}"; //弹出键盘
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 667}, {375, 216}}";
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 451}, {375, 216}}"; //隐藏键盘
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 451}, {375, 216}}";
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 667}, {375, 216}}";
*/ //transform是相对位移 // 设置窗口的颜色
self.view.window.backgroundColor = self.tableView.backgroundColor; // 0.取出键盘动画的时间
CGFloat duration = [sender.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; // 1.取得键盘最后的frame
CGRect keyboardFrame = [sender.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; // 2.计算控制器的view需要平移的距离
CGFloat transformY = keyboardFrame.origin.y - self.view.frame.size.height; // 3.让UITableView的最后一个cell滚到键盘最上面
// self.view.transform = CGAffineTransformMakeTranslation(0, transformY);
NSIndexPath *lastIndex = [NSIndexPath indexPathForRow:self.messagesFrame.count - 1 inSection:0];
[self.tableView scrollToRowAtIndexPath:lastIndex atScrollPosition:UITableViewScrollPositionBottom animated:YES]; // 4.执行动画(self.view.transform是tableView加上textFiled一起向上移动,也就是整个view)
[UIView animateWithDuration:duration animations:^{
self.view.transform = CGAffineTransformMakeTranslation(0, transformY);
}]; // self.view.transform = CGAffineTransformMakeTranslation(0, -216);}

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,909
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,434
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,249
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,060
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,692
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,730