Created
August 14, 2012 00:34
-
-
Save lqik2004/3345155 to your computer and use it in GitHub Desktop.
AutoScroll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma mark -KeyBoard Notifications | |
-(void) registerForKeyBoardNotification{ | |
//键盘出现时发出信息 | |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardWillShowNotification object:nil]; | |
//键盘消失时发出信息 | |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; | |
} | |
//called when uikeyboarddidshownotification is sent. | |
-(void)keyboardWasShown:(NSNotification *)noti{ | |
NSDictionary *dic=[noti userInfo]; | |
CGRect kbFrame=[[dic objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue]; | |
CGRect keyboardFrame = [self.view convertRect:kbFrame toView:nil]; | |
UIEdgeInsets contentInsets=UIEdgeInsetsMake(0, 0, keyboardFrame.size.height, 0); | |
scrollView.contentInset=contentInsets; | |
scrollView.scrollIndicatorInsets=contentInsets; | |
CGRect aRect=self.view.bounds; | |
aRect.size.height-=keyboardFrame.size.height; | |
CGPoint point=activeTextField.frame.origin; | |
point.y+=activeTextField.frame.size.height; | |
if(!CGRectContainsPoint(aRect, point)){ | |
CGPoint scrollPoint=CGPointMake(0,username.frame.origin.y-20); | |
[scrollView setContentOffset:scrollPoint animated:YES]; | |
} | |
} | |
//called when uikeyboardwillhidenotification is sent. | |
/* | |
*在这段代码的上一个版本是没有Animation的,当键盘消失的时候会瞬间消失和前面的动画不连贯,看起来不太爽。 | |
*我曾经尝试过使用[scrollView setContentOffset:animated:]的方法,回滚到原位置,但是发现这样有很大问题,所以就自己加入了动画。 | |
*之所以加入并且设置了一个delay是因为如果在登陆页面中可以通过软键盘从用户名区域跳转到到密码输入区域,实际上也是经过了关闭键盘再打开键盘的过程,所以页面内容会快速的跳一下,影响美观和感受,自己实在看不过去,尝试加了一个delay,效果还行。 | |
*/ | |
-(void)keyboardWillHide:(NSNotification *)noti{ | |
NSDictionary *userInfo=[noti userInfo]; | |
NSTimeInterval animationDuration; | |
NSTimeInterval delay=0.28; | |
UIViewAnimationCurve animationCurve; | |
[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve]; | |
[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration]; | |
[UIView beginAnimations:nil context:nil]; | |
[UIView setAnimationDelay:delay]; | |
[UIView setAnimationCurve:animationCurve]; | |
[UIView setAnimationDuration:animationDuration]; | |
UIEdgeInsets contentInsets=UIEdgeInsetsZero; | |
scrollView.contentInset=contentInsets; | |
scrollView.scrollIndicatorInsets=contentInsets; | |
[UIView commitAnimations]; | |
} | |
#pragma mark -UITextFieldDelegate | |
- (void)textFieldDidBeginEditing:(UITextField *)textField{ | |
activeTextField=textField; | |
} | |
- (void)textFieldDidEndEditing:(UITextField *)textField{ | |
activeTextField=nil; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment