你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS虛擬鍵盤上添加動態按鈕

iOS虛擬鍵盤上添加動態按鈕

編輯:IOS開發綜合

之前在 在iOS虛擬鍵盤上添加動態隱藏按鈕一文中描敘了關於鍵盤上添加動態按鈕的操作,發現鍵盤上的按鈕顯示出來的時候很僵硬,此處做了改進,添加了動畫過渡,更換了圖片,能夠讓人感覺按鈕是隨著鍵盤的動畫顯示而顯示,隨著鍵盤的動畫退出而退出,看上去更加流暢些;

效果圖:
 

 

\  \

 

\  \

- (void)viewDidLoad
{
    NSLog(@"%@",NSStringFromSelector(_cmd));
    [super viewDidLoad];
 exitButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [exitButton setImage:[UIImage imageNamed:@"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];
}

 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved