iOS虚拟键盘上添加动态按钮


之前在 在iOS虚拟键盘上添加动态隐藏按钮一文中描叙了关于键盘上添加动态按钮的操作,发现键盘上的按钮显示出来的时候很僵硬,此处做了改进,添加了动画过渡,更换了图片,能够让人感觉按钮是随着键盘的动画显示而显示,随着键盘的动画退出而退出,看上去更加流畅些;

创新互联公司是一家集网站建设,策勒企业网站建设,策勒品牌网站建设,网站定制,策勒网站建设报价,网络营销,网络优化,策勒网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。


效果图:

iOS虚拟键盘上添加动态按钮  iOS虚拟键盘上添加动态按钮


iOS虚拟键盘上添加动态按钮  iOS虚拟键盘上添加动态按钮

 

 

- (void)viewDidLoad
{
    NSLog(@"%@",NSStringFromSelector(_cmd));
    [super viewDidLoad];
	exitButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [exitButton setImage:[UIImage p_w_picpathNamed:@"down.png"] forState:UIControlStateNormal];
    CGRect exitBtFrame = CGRectMake(self.view.frame.size.width-48, self.view.frame.size.height , 48.0f, 30.0f);
    
    [exitButton setFrame:exitBtFrame];
      
   
    [self.view addSubview:exitButton];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardDidShow:) name:UIKeyboardWillShowNotification object:nil];  
    
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

 

- (void)handleKeyboardDidShow:(NSNotification *)notification 
{
    NSLog(@"%@",NSStringFromSelector(_cmd));
    NSDictionary *info = [notification userInfo];
    NSLog(@"-->info:%@",info);
    CGRect keyboardFrame;
    [[info objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrame];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size;
    NSValue *animationDurValue = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval animationDuration;
//    copy value
    [animationDurValue getValue:&animationDuration];
    
//    让键盘弹起的时候添加一个动画
    [UIView beginAnimations:@"animal" context:nil];
    [UIView setAnimationDuration:animationDuration];
    
    CGFloat distanceToMove = kbSize.height;
    NSLog(@"---->动态键盘高度:%f",distanceToMove);
    [self adjustPanelsWithKeyBordHeight:distanceToMove];
    [UIView commitAnimations];
    exitButton.hidden=NO;
    [exitButton addTarget:self action:@selector(CancelBackKeyboard:) forControlEvents:UIControlEventTouchDown];
    

}

 

- (void)handleKeyboardWillHide:(NSNotification *)notification 
{
    NSLog(@"%@",NSStringFromSelector(_cmd));

    NSDictionary *info = [notification userInfo];
    CGRect keyboardFrame;
    [[info objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrame];
    NSValue *animationDurValue = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval animationDuration;
    //   把animationDurvalue 值拷贝到animationDuration中
    [animationDurValue getValue:&animationDuration];
    
    [UIView beginAnimations:@"animal" context:nil];
    [UIView setAnimationDuration:animationDuration];

    if (exitButton) {

        CGRect exitBtFrame = CGRectMake(self.view.frame.size.width - 48, self.view.frame.size.height, 48.0f, 30.0f);
        exitButton.frame = exitBtFrame;
        [self.view addSubview:exitButton];

    }
    [UIView commitAnimations];
    
}

 

-(void)adjustPanelsWithKeyBordHeight:(float) height
{
   
    NSLog(@"%@",NSStringFromSelector(_cmd));
    if (exitButton) {

       CGRect exitBtFrame = CGRectMake(self.view.frame.size.width - 48, self.view.frame.size.height - height-30, 48.0f, 30.0f);
        exitButton.frame = exitBtFrame;

        [self.view addSubview:exitButton];


    }
    
    
}

 

-(void)CancelBackKeyboard:(id)sender
{
    NSLog(@"%@",NSStringFromSelector(_cmd));
    
    [textField resignFirstResponder];
    
}


- (void)viewDidUnload
{
    [self setTextField:nil];
    exitButton=nil;
    [super viewDidUnload];
    
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (void)dealloc {
    [textField release];
    [exitButton release];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

 

 

 

源码:http://download.csdn.net/detail/duxinfeng2010/4858444

 

网页名称:iOS虚拟键盘上添加动态按钮
路径分享:https://www.cdcxhl.com/article40/pesdeo.html

成都网站建设公司_创新互联,为您提供品牌网站建设做网站定制开发网站制作外贸网站建设网站策划

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都网页设计公司