Pregunta

I'm going to describe what I'm trying to do generally (in case there's a better way) and then the stumbling block I've run into (in case my way is the best way).

What I want to do: I want to add an invocation to my undo manager that has a time limit. If the undo is not triggered within the time limit, it won't be available when the device is shaken and thus nothing will happen.

What I'm doing: My approach was to use an NSUndoManager with an NSTimer. When I add the invocation to the undo manager, I kick off a 5-second timer as well. When the timer fires it checks !self.undoManager.isUndoing and if it's true than it goes ahead and removes all actions from the undo manager. Testing it in the simulator works: a shake gesture kicks off the undo before 5 seconds, but not after.

The problem is that if I get a shake gesture under the 5 second limit, the undo manager shows the alert, but if the user waits until after the 5s limit to actually tap the undo button, nothing happens: the timer happily cleared the stack away, even though the alert view was visible.

Is there a way to check and see if the alert view is visible? Best would be if I could figure out if the user hit undo or cancel as well, and clear the undo manager's action stack if the cancel button was pressed.

Or is there a better way besides using a timer in this manner?

Thanks!

Edited to add: My other thought was to capture the shake event myself (via the motionEnded:withEvent: call) and manually manage the alert and undo stack. Thoughts on this compared to the above are also welcome.

¿Fue útil?

Solución

I ended up doing what I suggested in my edit—to use motionEnded:withEvent to manually manage the alert and undo. The downside to this is that you don't get the built-in undo alert which has a slightly different style from a UIAlertView and enters the screen with a shaking motion.

The upside is that I now have a undo that expires after 10 seconds. What follows is the general structure of the code in case you want the same.

First, make sure your app can receive shake events and that you have an NSUndoManager you can access. You also need a timer; I have my code set up with an NSTimer that kicks off when the undoable event occurs and lasts 10 seconds. Make sure you add your undo target at the same timer your timer starts so that there's actually something to undo.

Next, implement motionEnded:withEvent like so:

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if (motion == UIEventSubtypeMotionShake && [self.undoManager canUndo]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Undo something?" message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Undo", nil];
        [alert show];
        undoAlertIsVisible_= YES;
    }
}

I'm using an ivar called undoAlertIsVisible_ here to track if my alert is on screen.

In your timer's callback, do something like:

if (!self.undoManager.isUndoing && !undoAlertIsVisible_) {
    // Clear away the possible undo
    [self.undoManager removeAllActionsWithTarget:self];
}
undoTimer_ = nil;

Here we check to see we're not currently undoing and the alert isn't visible. If so, remove the undo actions and set the timer (another ivar) to nil. I'm setting the timer to nil so that I can check whether it's fired in my alert callback, which is here:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex != alertView.cancelButtonIndex) {
        if (self.undoManager.canUndo) {
            [self.undoManager undo];
        }
    }
    else {
        if (!undoTimer_) {
            // Timer fired while we were staring at the alert
            [self.undoManager removeAllActionsWithTarget:self];
        }
    }
    undoAlertIsVisible_= NO;
}

In the alert callback we either make the undo happen or, if the timer fired while the alert was visible and the alert was canceled, we clear possible undo actions. Otherwise the undo action would hang around after canceling with no timer to clear it.

Hope this helps someone!

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top