IPhone : iPhone clearing uitextfield causing view unexpected animate

on Wednesday, February 25, 2015

I'm building a chat room where focus on a uitextfield would push view up and clicking send button will set the textfield back to empty. Everything works fine, however, every time after clicking the send button it will automatically push the view back to original view. I did not set anything function for editingchange. here is my code and how it looks:



-(void) viewDidLoad{
[super viewDidLoad];
last_msg_id =[[NSString alloc]init];
//init text field
[TextField setReturnKeyType:UIReturnKeySend];
UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismisskeyboard)];
[self.view addGestureRecognizer:tap];
tap.delegate =self;
TextField.autocorrectionType =UITextAutocorrectionTypeNo;
//init variable
if (!messages) {
messages =[[NSMutableArray alloc]init];
}

//settitles
self.nav.topItem.title=Name;
if (!c_id) {
[More setEnabled:NO];
}
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(keyboardDidShow:)
name:UIKeyboardDidChangeFrameNotification object:nil];
[center addObserver:self selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification object:nil];
}



- (void)keyboardWillHide:(NSNotification *)notification{
keyboar_flag = YES;

}

- (void)keyboardDidShow:(NSNotification *)notification{
NSDictionary *userInfo = [notification userInfo];

// Get the duration of the animation.
NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];

// Animate the resize of the text view's frame in sync with the keyboard's appearance.


CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;

const int movementdistance =keyboardSize.height - key_height;
key_height =keyboardSize.height;
const float movementduration =0.3f;

if (keyboar_flag) {
key_height=0;
keyboar_flag = NO;
}
int movement = (YES?-movementdistance:movementdistance);
[UIView beginAnimations:@"anim" context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:movementduration];
self.table.frame=CGRectMake(self.table.frame.origin.x, self.table.frame.origin.y, self.table.frame.size.width, self.table.frame.size.height+movement);
self.BottomToolBar.frame=CGRectOffset(self.BottomToolBar.frame, 0, movement);
[UIView commitAnimations];
if (table.contentSize.height>table.frame.size.height) {
CGPoint offset =CGPointMake(0, table.contentSize.height-table.frame.size.height);
[self.table setContentOffset:offset animated:NO];
}


}



-(BOOL) textFieldShouldReturn:(UITextField *)textField{
NSString *msg=self.TextField.text;
TextField.text=@"";
BOOL flag =[self sendmsg:msg];
if (flag) {
[table reloadData];
if (table.contentSize.height>table.frame.size.height) {
CGPoint offset =CGPointMake(0, table.contentSize.height-table.frame.size.height);
[self.table setContentOffset:offset animated:NO];
}

}
return YES;


}


In addition, i identify the problem is TextField.text=@""; if i delete this line the view will not animate. However, i do not know where i get wrong images: before clicking send button after clicking send button


0 comments:

Post a Comment