2

I'm currently trying to do a login screen for an app. It is composed of a big logo, and under it are two textfields for login and passord, and a button.

When a textfield has focus, there is nice animation that makes all the elements go up so they are not hidden by the keyboard, and the logo vanishes. When the textfield loses focus, everything goes back to its normal state.

My problem is when going from one textfield to another. Both animations are triggered, so the logo briefly appears and disappears, which is not very professional.

The keyboard, homever, does not disappear and appear again, so i guess Apple uses a special trick to prevent it.

How can I achieve the same thing with my animation ?

Community
  • 1
  • 1
Pierre
  • 372
  • 2
  • 16

2 Answers2

1

There's a handy notification that gets called every time the keyboard appears and disappear's. Just register your self to these notifications in viewDidLoad or viewWillAppear and you can move the animation logic into the methods keyboardWillShow and keyboardWillHide . There by you can prevent the animations repeating

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillShow)
                                             name:UIKeyboardWillShowNotification
                                           object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
                                     selector:@selector(keyboardWillHide)
                                         name:UIKeyboardWillHideNotification
                                       object:nil];
NavinDev
  • 995
  • 1
  • 7
  • 8
  • Thanks, it works great ! Do you have any hint about making the view go up exactly the same speed as the keyboard ? I found a few answers on this site, but none really worked. – Pierre Jun 18 '14 at 18:40
  • Yeah , I actually do, But it does require you to place your textFields on a scrollView and instead of animating them in an animation block. you can just set the content offset of the scrollView and you get a smooth animation. Simpler still , have a look at this subclass helper for scrollView. Just download the files and add it to your project and set the scrollView's subclass to the TPKeyboardAvoiding scrollView and you get a smooth animation whilst the keyboard appears. – NavinDev Jun 20 '14 at 12:24
1

You can make your animation according to the keyboard appearance i.e. if your keyboard appears do your animation and when keyboard is hidden everything becomes normal.

In the ViewDidLoad method of your class set up to listen for messages about the keyboard:

// Listen for keyboard appearances and disappearances 
[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(keyboardDidShow:)
                                             name:UIKeyboardDidShowNotification
                                           object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardDidHide:)
                                             name:UIKeyboardDidHideNotification
                                           object:nil];

Then add the following methods to your code. In these metods you can handle your animation.

- (void)keyboardDidShow: (NSNotification *) notif{
    // Animate here
}

- (void)keyboardDidHide: (NSNotification *) notif{
    // Back to normal
}
Rohit tak
  • 21
  • 2