I have a simple viewController. The View controller use animation. If I am displaying list, it will animate to map. At that time it will also change the button that points to map to point to list instead.
So by keep pressing the same button (it's 2 different button actually, but users see that as the same button), user can keep switching from map to list and via versa.
Works fine. With some catch.
What about if users tap too fast? Let's just say that canning or punishing them is not an option given that I want my application to indeed be used.
This is the current program:
-(void) transitiontoViewController:(UIViewController *)toViewController duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL))completion1
{
[self enableButtonWithBool:false];
UIViewController * fromController = self.last2ViewsInTheContainer.lastObject;
if (fromController.view.superview!= self.ContainerView)
{
return;
}
[self transitionFromViewController:fromController toViewController:toViewController duration:duration options:options animations:animations completion:^(BOOL finished) {
if (completion1 == Nil) //can't execute nil block. can be more elegantly done with [completion1 invoke] if there is such function. Anyway, do nothing if completion1 is nil
{
}
else
{
completion1 (finished);
}
[self.last2ViewsInTheContainer addObject:toViewController];
if (self.last2ViewsInTheContainer.count>2)
{
[self.last2ViewsInTheContainer removeObjectAtIndex:0];
}
}];
[self enableButtonWithBool:true];
[self ReloadorRedraw];
}
Simple transition. I just want to point out at the end of the function 2 controllers view have the same subview. It looks like the transition is not yet complete at the end of the function. The old view controller is not really removed, etc.
The content of ReloadorRedraw is the following:
-(void)ReloadorRedraw;
{
BGCRBadgerStandardViewViewController * listOrMap = nil;
if ([self.last2ViewsInTheContainer lastObject]==self.filterController)
{
[self.changeFilterButton setBackgroundImage:[UIImage imageNamed:filterBarOpenImageString] forState:UIControlStateNormal];
listOrMap = self.last2ViewsInTheContainer[0];
}
else
{
[self.changeFilterButton setBackgroundImage:[UIImage imageNamed:filterBarCloseImageString] forState:UIControlStateNormal];
listOrMap = self.last2ViewsInTheContainer.lastObject;
}
if (listOrMap==self.listBusinessViewController)
{
self.navigationItem.rightBarButtonItem=self.mapButton;
}
else
{
self.navigationItem.rightBarButtonItem=self.listButton;
}
}
If I clicked too fast, I got this message: Unbalanced calls to begin/end appearance transitions for . Unbalanced calls to begin/end appearance transitions for .
after which the app is in an invalid state.
One way to remedy the situation is to "disable" button until animation finish.
-(void) transitiontoViewController:(UIViewController *)toViewController duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL))completion1
{
[self enableButtonWithBool:false];
UIViewController * fromController = self.last2ViewsInTheContainer.lastObject;
if (fromController.view.superview!= self.ContainerView)
{
return;
}
[self transitionFromViewController:fromController toViewController:toViewController duration:duration options:options animations:animations completion:^(BOOL finished) {
if (completion1 == Nil) //can't execute nil block. can be more elegantly done with [completion1 invoke] if there is such function. Anyway, do nothing if completion1 is nil
{
}
else
{
completion1 (finished);
}
[self.last2ViewsInTheContainer addObject:toViewController];
if (self.last2ViewsInTheContainer.count>2)
{
[self.last2ViewsInTheContainer removeObjectAtIndex:0];
}
[self enableButtonWithBool:true];
[self ReloadorRedraw];
}];
}
Basically I am moving
[self enableButtonWithBool:true];
[self ReloadorRedraw];
to the completion block.
the function enableButtonWithBool is this simple function:
-(void)enableButtonWithBool: (BOOL) enable
{
self.mapButton.enabled=enable;
self.listButton.enabled= enable;
self.changeFilterButton.enabled =enable;
self.searchBar.enabled =enable;
}
This works well.
My partner doesn't like it. That's because disabled button looks ugly even though only for .3 seconds. Users won't like it even though it's for their own good.
So basically I got to enable reenable the button quickly rather than wait till application is ended.
But how to do that and still make a robust application that can withstand users' rapid clicking?
I think it would help if someone can also explain when exactly the "real" transition complete. I mean the actual remove from superview of old stuff and add to subview the new stuff. Is the animation just a "movie" that doesn't really change transition or view structure?
Instead of actually enabling or disabling your buttons, set an internal Boolean variable to true or false. Then the code that executes when your button is pushed will check that variable to decide whether to ignore the button push or actually do something.