0

I have a UITextview, becomes first responder and keyboard is presented. Presently, I have buttons in inputAccessoryView toolbar that exchange the text forward and reverse through an array of strings. I have the process working with no issues, so of course I am inclined to break it. My wish is to slide the textview left and right like a carousel to make it more clear to the user that next or previous string is coming and going. The current system simply replaces the text with no animation.

My first thought was to create a UINavigationController, give it an array of UIViewControllers that present the UITextviews. The navigation controllers view is only as big as the textview and I add it as a subview to my full view (which is itself in a navigation controller). I got this working fairly completely, the navigation bar is hidden and it looks no different than the original textview except that the textview now slides off to the right or left, depending if I am pushing or popping. The problem with that is that the keyboard slides off along with the dismissed view controller, then the new textview in the new controller becomes first responder and the keyboard returns. Close, but no cigar.

I considered using page view controller but it seems it will have the same issue. I think I may have to go back to the single textview and animate the whole process directly with static screen grabs. That is completely beyond my experience level and I am think there must be a simpler way.

Can anyone suggest a simple way to keep that keyboard present while the views are swapped as described? Suggestions on other angles of attacking this?

Dean Davids
  • 4,174
  • 2
  • 30
  • 44

1 Answers1

0

Seems like an awful lot of overhead for a simple animation.

Try something like this (assuming ARC):

typedef enum _eDirection
{
    rightToLeft = -1,
    leftToRight = 1
} eDirection;

- (void) animateTextFields:(eDirection)direction
{
    CGFloat distance = self.window.bounds.size.width;
    UITextField *oldTextField = thisView.textField;
    CGRect oldTFRect = oldTextField.frame;
    CGRect newTFRect = CGRectOffset(oldTFRect, -direction * distance, 0);
    UITextField *newTextField = [[UITextField alloc] initWithFrame:newTFRect];
    [newTextField setText:@"whatever"];
    [self addSubview:newTextField];
    [UIView animateWithDuration:0.3f
                     animations:^{
                         [oldTextField setFrame:CGRectOffset(oldTextField.frame, direction * distance, 0)];
                         [newTextField setFrame:CGRectOffset(newTextField.frame, direction * distance, 0)];
                     } completion:^(BOOL finished) {
                         [self setTextField:newTextField];
                         [self removeSubview:oldTextField];
                         [newTextField becomeFirstResponder];
                     }];
 }

(Disclaimer: as with all code typed off the top of one's head, etc., etc.)

This method would create a new textField, animate it horizontally onto the screen while moving the existing one off the screen in the direction you give, and then assigns the new textField as the current one and makes it the firstResponder.

rsswtmr
  • 1,776
  • 5
  • 16
  • 26
  • that looks awful simple. I suspected I was overthinking it. I will give it a try and see how it plays. Thank you. – Dean Davids Oct 01 '12 at 01:30
  • I don't have all the bugs worked out but I see this is going to work. I added call to addSubview and removeFromSuperview is all it took to get the animation working. It does keep my keyboard up and gives the effect intended. Thank you. – Dean Davids Oct 01 '12 at 03:12
  • Glad it helped. I'll modify the post to reflect the add/remove subview calls. – rsswtmr Oct 01 '12 at 03:28
  • In order that I would not have to set all the textview properties line by line, I replaced your = [[UITextField alloc] initWithFrame:newTFRect]; with my [oldTextview copy]. Xcode says unrecognized selector when I go to setFrame = newTFRect. I have already worked around it but curious why I cannot set frame on a copy of old textView? – Dean Davids Oct 01 '12 at 04:26
  • `[oldTextView copy]` isn't going to give you a deep copy of the textField. If you have a lot of custom settings, you could instantiate new textFields from a nib file which would simplify the creation process. Though, if you assigned your shallow copy to a `UITextField` variable, I don't understand why you'd get an unrecognized selector error for `setFrame:`. Something else must be going on. – rsswtmr Oct 01 '12 at 04:54
  • I didn't bother with it further. I only needed to set the font and color. It is a textView, not textField. Should be the same concept? My main consideration was that, if I ever change the properties in the nib, those changes would automatically propagate. Now at some time in the future I may change the font and then wonder why it changes back on me when it becomes firstResponder. I have it all working now, testing for unforeseen issues and finding none. Thank you very much for your help. – Dean Davids Oct 01 '12 at 05:58