Reputation: 203
I need to move text
in UITextField
(from right to left, and then back) with animation like in Safari App in iOS 7 (see the screenshots below).
Is there any way to do it?
I know that I can set textAlignment
, but it won't be animated.
Maybe I should change leftView
size in UITextField
? But I'm not sure how to calculate size, when text should be aligned on the center. Please, help.~
(source: cs424818.vk.me)
Upvotes: 3
Views: 4830
Reputation: 11429
You can achieve this behavior by doing this:
- (void)viewDidLoad
{
// add this to viewDidLoad
// keep the alignment left justified
[self.yourTextField setTextAlignment:NSTextAlignmentLeft];
[self.yourTextField addTarget:self action:@selector(textFieldDidChange)forControlEvents:UIControlEventEditingChanged];
}
-(void)textFieldDidChange
{
// this will cause your textfield to grow and shrink as text is added or removed
// note: this will always grow to the right, the lefthand side will stay anchored
[self.yourTextField sizeToFit];
}
Then when you want to move your UITextField from the center to the left you can simply do:
// 1 second animation
[UIView animateWithDuration:1.0 animations:^{
// move to the left side of some containing view
self.yourTextField.center = CGPointMake(containingView.frame.origin.x + self.yourTextField.frame.size.width/2, self.yourTextField.center.y);
}];
Likewise to move from the lefthand side to the center you can do:
[UIView animateWithDuration:1.0 animations:^{
// move to the center of containing view
self.yourTextField.center = CGPointMake(containingView.frame.size.width/2, self.yourTextField.center.y);
}];
Hope this helps!
Upvotes: 1