iOS组件封装与自动布局自定义表情键盘

2020-01-15 13:48:14王振洲

3.当横竖屏幕切换时设置自定义键盘的高度


-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
 //纵屏
 if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
 CGRect frame = self.functionView.frame;
 frame.size.height = 216;
 self.functionView.frame = frame;
 self.moreView.frame = frame;
 
 }
 //横屏
 if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
 CGRect frame = self.functionView.frame;
 frame.size.height = 150;
 self.functionView.frame = frame;
 self.moreView.frame = frame;
 }
}

4.当键盘出来的时候,改变toolView的位置,通过键盘的通知来实现。当横屏的时候键盘的坐标系和我们当前的Frame的坐标系不一样所以当横屏时得做一坐标系的转换,代码如下:


//当键盘出来的时候改变toolView的位置(接到键盘出来的通知要做的方法)
-(void) keyNotification : (NSNotification *) notification
{
 NSLog(@"%@", notification.userInfo);
 
 self.keyBoardDic = notification.userInfo;
 //获取键盘移动后的坐标点的坐标点
 CGRect rect = [self.keyBoardDic[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];
 
 //把键盘的坐标系改成当前我们window的坐标系
 CGRect r1 = [self.view convertRect:rect fromView:self.view.window];
 
 [UIView animateWithDuration:[self.keyBoardDic[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{
 
 //动画曲线
 [UIView setAnimationCurve:[self.keyBoardDic[UIKeyboardAnimationCurveUserInfoKey] doubleValue]];
 
 CGRect frame = self.toolView.frame;
 
 frame.origin.y = r1.origin.y - frame.size.height;
 
 //根据键盘的高度来改变toolView的高度
 self.toolView.frame = frame;
 }];
}

5.系统键盘和自定义键盘切换的代码如下:


//切换键盘的方法
-(void) changeKeyboardToFunction
{
 if ([self.myTextView.inputView isEqual:self.functionView])
 {
 self.myTextView.inputView = nil;
 [self.myTextView reloadInputViews];
 }
 else
 {
 self.myTextView.inputView = self.functionView;
 [self.myTextView reloadInputViews];
 }
 
 if (![self.myTextView isFirstResponder])
 {
 [self.myTextView becomeFirstResponder];
 }
}

以上就是上面展示效果的核心代码了,在做的时候感觉难点在于如何进行屏幕适配,尤其是当屏幕横过来的时候键盘的坐标系和我们frame的坐标系不同,得做 一个转换。发表文章的目的是想起到抛砖引玉的左右,有好的东西希望大家相互交流一下。



注:相关教程知识阅读请移步到IOS开发频道。