He estado siguiendo un tutorial de como implementar un login con parse con función de loguearse con Facebook y Twitter, sin embargo, me encuentro con un detalle.
En principio los elementos de la vista principal se esconden por medio de la función viewDidLayoutSubviews
, es decir, se envían hacia la parte inferior de la pantalla y posteriormente se desplazan hacia arriba en forma de animación, esta animación se ejecuta en la función viewDidAppear
.
Hasta ahi todo es perfecto, sin embargo cuando selecciono alguno de los textFields
que forman parte de la vista para introducir un texto, como es obvio el teclado aparece y en teoría debería desplazar los elementos hacia arriba para no ocultarlos cuando se escribe, aquí supongo que se vuelve a activar la función viewDidLayoutSubviews
y ejecuta el fragmento de código que me esconde todos los elementos en la parte inferior tal como se realiza cuando se carga la aplicación para hacer posible la animación.
¿Cómo puedo evitar que los elementos desaparezcan cada vez que intente escribir en alguno de los textFields
?
override func viewDidLoad() {
super.viewDidLoad()
// create an array of all the views we want to animate in when we launch the screen
viewsToAnimate = [self.logInView?.usernameField, self.logInView?.passwordField, self.logInView?.logInButton, self.logInView?.passwordForgottenButton, self.logInView?.facebookButton, self.logInView?.signUpButton, self.logInView?.logo]
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
// We to position all the views off the bottom of the screen
// and then make them rise back to where they should be
// so we track their final position in an array
// but change their frame so they are shifted downwards off the screen
viewsFinalYPosition = [CGFloat]();
for viewToAnimate in viewsToAnimate {
//print(viewToAnimate.frame)
let currentFrame = viewToAnimate.frame
print(viewsFinalYPosition.count)
viewsFinalYPosition.append(currentFrame.origin.y)
viewToAnimate.frame = CGRectMake(currentFrame.origin.x, self.view.frame.height + currentFrame.origin.y, currentFrame.width, currentFrame.height)
}
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
// Now we'll animate all our views back into view
// and, using the final position we stored, we'll
// reset them to where they should be
if viewsFinalYPosition.count == self.viewsToAnimate.count {
UIView.animateWithDuration(1, delay: 0.0, options: .CurveEaseInOut, animations: { () -> Void in
for viewToAnimate in self.viewsToAnimate {
print(viewToAnimate.frame)
let currentFrame = viewToAnimate.frame
print(self.viewsFinalYPosition.count)
viewToAnimate.frame = CGRectMake(currentFrame.origin.x, self.viewsFinalYPosition.removeAtIndex(0), currentFrame.width, currentFrame.height)
}
}, completion: nil)
}
}
https://dl.dropboxusercontent.com/u/4915071/viewsDissapear.mov