首页 技术 正文
技术 2022年11月15日
0 收藏 481 点赞 4,598 浏览 3638 个字

最近做的项目中,有一个类似微博中的评论转发功能,屏幕底端有一个输入框用textView来做,当textView成为第一响应者的时候它的Y值随着键盘高度的改变而改变,保证textView紧贴着键盘,但又不会被键盘挡住。

下面是我实现的方法:(利用通知)

123456789101112 //
键盘通知
    //
键盘的frame发生改变时发出的通知(位置和尺寸)
    //   
UIKeyboardWillChangeFrameNotification
    //   
UIKeyboardDidChangeFrameNotification
    //
键盘显示时发出的通知
    //   
UIKeyboardWillShowNotification
    //   
UIKeyboardDidShowNotification
    //
键盘隐藏时发出的通知
    //   
UIKeyboardWillHideNotification
    //   
UIKeyboardDidHideNotification
         [[NSNotificationCenter
defaultCenter] addObserver:self selector:
@selector(keyboardWillChangeFrame:)
name:UIKeyboardWillChangeFrameNotification object:nil];
//在这里注册通知

下面是监听通知:

12345678910111213141516171819202122232425262728293031323334353637 #pragma
mark - 监听方法
/** *
键盘的frame发生改变时调用(显示、隐藏等)
 */-
(
void)keyboardWillChangeFrame:(NSNotification
*)notification
{    //   
if (self.picking) return;
    /**     notification.userInfo
= @{
     //
键盘弹出\隐藏后的frame
     UIKeyboardFrameEndUserInfoKey
= NSRect: {{0, 352}, {320, 216}},
     //
键盘弹出\隐藏所耗费的时间
     UIKeyboardAnimationDurationUserInfoKey
= 0.25,
     //
键盘弹出\隐藏动画的执行节奏(先快后慢,匀速)
     UIKeyboardAnimationCurveUserInfoKey
= 7
     }     */         NSDictionary
*userInfo = notification.userInfo;
         //
动画的持续时间
    doubleduration
= [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
         //
键盘的frame
    CGRect
keyboardF = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
         //
执行动画
    [UIView
animateWithDuration:duration animations:^{
        //
工具条的Y值 == 键盘的Y值 - 工具条的高度
        if(keyboardF.origin.y
> self.view.height) {
//
键盘的Y值已经远远超过了控制器view的高度
            self.toolbar.y
= self.view.height - self.toolbar.height;
//这里的<span
style="background-color: rgb(240, 240, 240);">self.toolbar就是我的输入框。</span>
         }else{            self.toolbar.y
= keyboardF.origin.y - self.toolbar.height;
        }    }];}

当然,这里我为UIView写了一个类别,实现如下:

.h文件中声明

12345678910 @interfaceUIView
(Extension)
@property(nonatomic,
assign) CGFloat x;
@property(nonatomic,
assign) CGFloat y;
@property(nonatomic,
assign) CGFloat width;
@property(nonatomic,
assign) CGFloat height;
@property(nonatomic,
assign) CGFloat centerX;
@property(nonatomic,
assign) CGFloat centerY;
@property(nonatomic,
assign) CGSize size;
@property(nonatomic,
assign) CGPoint origin;
@end

.m文件中实现(重写setter 和 getter)

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 @implementationUIView
(Extension)
 -
(
void)setX:(CGFloat)x{    CGRect
frame = self.frame;
    frame.origin.x
= x;
    self.frame
= frame;
} -
(
void)setY:(CGFloat)y{    CGRect
frame = self.frame;
    frame.origin.y
= y;
    self.frame
= frame;
} -
(CGFloat)x
{    returnself.frame.origin.x;} -
(CGFloat)y
{    returnself.frame.origin.y;} -
(
void)setCenterX:(CGFloat)centerX{    CGPoint
center = self.center;
    center.x
= centerX;
    self.center
= center;
} -
(CGFloat)centerX
{    returnself.center.x;} -
(
void)setCenterY:(CGFloat)centerY{    CGPoint
center = self.center;
    center.y
= centerY;
    self.center
= center;
} -
(CGFloat)centerY
{    returnself.center.y;} -
(
void)setWidth:(CGFloat)width{    CGRect
frame = self.frame;
    frame.size.width
= width;
    self.frame
= frame;
} -
(
void)setHeight:(CGFloat)height{    CGRect
frame = self.frame;
    frame.size.height
= height;
    self.frame
= frame;
} -
(CGFloat)height
{    returnself.frame.size.height;} -
(CGFloat)width
{    returnself.frame.size.width;} -
(
void)setSize:(CGSize)size{    CGRect
frame = self.frame;
    frame.size
= size;
    self.frame
= frame;
} -
(CGSize)size
{    returnself.frame.size;} -
(
void)setOrigin:(CGPoint)origin{    CGRect
frame = self.frame;
    frame.origin
= origin;
    self.frame
= frame;
} -
(CGPoint)origin
{    returnself.frame.origin;}@end

有需要的朋友可以直接用

版权声明:本文为博主原创文章,未经博主允许不得转载。

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