detección gesto de agitar en UIWindow subclase se hace cargo de director de texto predeterminado de deshacer
-
28-09-2019 - |
Pregunta
Lo que quiero es que mi aplicación para el iPhone consciente de agitación gesto todo el tiempo excepto cuando cualquier UITextfield
o UITextview
convierten firstResponder.
(I siguió esto: https: //stackoverflow.com/questions/150446/how-do-i-detect-when-someone-shakes-an-iphone/1351486#1351486I )
Me subclase método UIWindow
y motionEnded:withEvent:
implementado y funciona incluso si me puse un UITextView
ser firstResponder. Mis llamadas personalizados UIWindow
motionEnded:withEvent:
incluso si no es firstResponder.
Así que el problema es undomanager por defecto de mi UItextView
no responde a las sacudidas más. El UIWindow
se hace cargo de todo el manejo de Shake-Gesto independientemente de cómo se han cambiado la vista-jerarquía o firstResponders.
¿Alguien podría arrojar alguna luz sobre cómo desactivar temporalmente UIWindow de detección o tener la firstResponder actual se hace cargo de la Gesto Sacudiendo basado en la implementación tengo?
Gracias de antemano!
Solución
De todos modos he encontrado una solución.
El valor predeterminado de deshacer gerente es en realidad todavía existe ahorro de todas las acciones realizadas a un UITextView en secreto.
Desde UIWindow
se hace cargo de la gestión de movimiento, primero trato de tomar de nuevo reemplazando motionEnded:withEvent:
en el viewController que contiene mi UITextView
.
En segundo lugar, obtener el UndoManager por [myTextView undoManager]
entonces usted puede enviar mensajes undo
o redo
a él.
Ahora para imitar el defecto de deshacer gerente alertview, el uso redoMenuItemTitle
y undoMenuItemTitle
para obtener título del botón, a continuación, utilizar canUndo
y canRedo
para decidir si botón Mostrar o no.
Editar : Aquí está el código de mi aplicación:
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (event.type == UIEventTypeMotion && event.subtype == UIEventSubtypeMotionShake) {
//textUndoManager is an ivar but it was just a reference to the undoManager
//textUndoManager = [myTextView undoManager]; <--- in viewDidLoad:
NSString *undoButtonTitle = nil;
NSString *redodoButtonTitle = nil;
NSString *alertViewTitle = nil;
if ([textUndoManager canUndo])
undoButtonTitle = [NSString stringWithString:[textUndoManager undoMenuItemTitle]];
if ([textUndoManager canRedo])
redodoButtonTitle = [NSString stringWithString:[textUndoManager redoMenuItemTitle]];
if (!undoButtonTitle && !redodoButtonTitle)
alertViewTitle = @"Nothing to Undo";
UIAlertView *alertView;
if (undoButtonTitle == nil) {
alertView = [[UIAlertView alloc] initWithTitle:alertViewTitle message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:redodoButtonTitle, nil];
} else {
alertView = [[UIAlertView alloc] initWithTitle:alertViewTitle message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:undoButtonTitle, redodoButtonTitle, nil];
}
[alertView show];
}
}
//UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if ([[alertView buttonTitleAtIndex:buttonIndex] isEqualToString:[textUndoManager undoMenuItemTitle]]) {
[textUndoManager undo];
}
if ([[alertView buttonTitleAtIndex:buttonIndex] isEqualToString:[textUndoManager redoMenuItemTitle]]) {
[textUndoManager redo];
}
}