UIButton's target is fired before animation - objective-c

I have programmed a Quiz in Xcode where I've created UIButtons programmatically displaying the answer to a question. When the user clicks the button the next question is loaded and the buttons position i shuffled to not make the right answer in the same place for each question (the right answer is however under the same UIButton for every question.)
This is how I create my buttons:
if (self.buttonRightAnswer == nil)
{
self.buttonRightAnswer = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.buttonRightAnswer addTarget:self action:#selector(rightAnswer) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:self.buttonRightAnswer];
}
self.buttonRightAnswer.frame = CGRectMake(20.0, [[randomPositionArray objectAtIndex:0] floatValue], 280.0, 40.0);
[self.buttonRightAnswer setTitle:[[self.quiz.arrayWithQuestions objectAtIndex:questionNr - 1]valueForKey:#"RightAnswer"] forState:UIControlStateNormal];
When the user clicks the right an answer the next question loads. Here it's a problem. The next question loads with the same method only shuffled positions and the animation of the press-down of the button is drawn after the buttons been moved resulting in the user seeing where the right answer is now.
How can I wait for the animation before moving the buttons?
I could solve this by having the buttons stay in one place, shuffling out the questions over the buttons and comparing the users answer with the right answer.. but this requires a hell of a lot more code.

You can use some of shit-code and insert some kind of 'sleep' before next shuffling buttons
Before you create the buttons againg (after a question was answered), you can add something like (if you use differen images for button states)
-(void)waitUntilButtonDeselected
{
bool selected = YES;
while (selected)
{
bool selected = (self.buttonRightAnswer.currentBackgroundImage == UIImageOfNotSelectedButton);
}
}
The idea here is to check if the image of the button corresponds to not selected button.

Typically when using a UIButton, events are triggered on UIControlEventTouchUpInside, or at the end of the touch.
You're triggering it on the touch down which is causing issues with the animation you want to happen.
It might be simpler for you to switch to using UIControlEventTouchUpInside instead of UIControlEventTouchDown since this should solve your problem and be more consistent with other iPhone apps.

Related

UINavigationBar Back button with no text, ever

I have a requirement in a project that the UINavigationBar Back button should never have text in it, it should always just be a left arrow.
By default iOS is going to insert the title of the previous controller in there. Is there any way I can stop this from happening across the whole app?
(I know I can do this screen by screen, but I'm working on an existing app with A LOT of screens it and this would be a big job)
You can always set an image of an arrow to left bar button of navigation bar
// ADDING IMAGE TO BUTTON
UIButton *refreshButton = [UIButton buttonWithType:UIButtonTypeCustom];
[refreshButton setFrame:CGRectMake(0,0,30,30)];
[refreshButton setImage:[UIImage imageNamed:#"arrow_image.png"] forState:UIControlStateNormal];
refreshButton.userInteractionEnabled=NO;
// ASSIGNING THE BUTTON WITH IMAGE TO LEFT BAR BUTTON
UIBarButtonItem *refreshBarButton = [[[UIBarButtonItem alloc] initWithCustomView:refreshButton] autorelease];
self.navigationItem.leftBarButtonItem = refreshBarButton;
You will have to write this in each view controller in order to disable default left bar button.
You can't stop it from happening across the whole app, you'll have to set it manually in each controller. You could use a category on UIViewController and call that method in each controller, which will get you down to 1 line of code that doesn't have to change if you change your approach. Still sucks, I know. Also, you will probably have issues with Apple if you do that. We tried that in one of our apps and when I showed it to the Apple guys at WWDC '13 they flat out told me they would reject the app if I submitted it that way. YMMV

UIButton not clickable after UITableView scrolled

I have a UITableView which is populated with some cells. I have created a UIButton using the following snippet, it is placed next to one of the section headers.
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
[addButton addTarget:self action:#selector(addButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[addButton setBackgroundImage:[UIImage imageNamed:#"add.png"] forState:UIControlStateNormal];
addButton.backgroundColor = [UIColor clearColor];
addButton.frame = CGRectMake(270, 150, 29, 29);
The button is placed and works correctly. However, after the view is scrolled (even slightly - like 1 pixel), the button works once and then ceases to respond. When it fails to respond the action for when it is clicked is not triggered and the button doesn't give the 'depressed' shadow. The rest of the application runs as normal and it does not crash.
This seems odd because after I scroll the button is clickable once more before it stops working. The button is used to insert rows into the table, so after it is pressed there is an extra row, possibly this is breaking the bounds or something?
Button pressed function:
- (void)addButtonPressed {
self.addClientTable.editing = YES;
// First figure out how many sections there are
NSInteger lastSectionIndex = [self numberOfSectionsInTableView:self.addClientTable] - 1;
// Then grab the number of rows in the last section
NSInteger lastRowIndex = [self.addClientTable numberOfRowsInSection:lastSectionIndex];
[self.addClientTable insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:lastRowIndex inSection:lastSectionIndex]] withRowAnimation:UITableViewRowAnimationRight];
self.addClientTable.editing = NO;
}
Where addClientTable is the UITableView.
What could cause a UIButton to stop responding to clicks and where in my scenario would this be caused by?
I am almost sure that your problem is that your button is out of it superview, and you are not using the clip subviews option in your view that contains the button, or in one of it superviews.
Set to true all the views property clip subviews and see if it appears your button. (We expect that the button disappear)
If you provide more code I can try to help you to solve this problem.
-
Reading again your question, another probable problem to it is that you have one view in front of your button. You can test it changing the background of your view, or something like that.

NSTextField to update before the end of a method

so here is the deal:
I have a label (NSTextField) that I want to activate after I click a button. This label will appear while the program is loading some wavs (since it usually makes a minor delay when it does). I then want it gone once this has happened (and the new View appears).
Now, the problem I have is that this update does not seem to happen when I tried this. If I don't make it disappear at the end then I can see it, but only after the delay has occured (rendering it pointless).
Currently I am using:
[label2 setHidden:NO];
I understand that this will occur once the method I called it in has finished (which is a problem). Any idea what I could do instead so that the label is shown while the program is loading wavs?
Thanks heaps!!
Ok, I guess I solved it myself - I hope this helps people.
So when I click the button I disable the buttons and replace the label temporarily. This, however, only happens in the next view (so I'm not sure how to make it occur in the same view).
I disable the buttons for about 1 second, and it is here that the label is shown.
Here's some code to show what I mean:
- (IBAction)clickedTheButton:(id)sender {
[button setEnabled:NO];
[label2 setHidden:NO];
...
//Changes the View
[self nextMethod];
}
The View has now changed, and this method is called next. This enables me to see the label.
-(void)nextMethod{
...
[self performSelector:#selector(delayedDisplay:)
withObject:#"Hi"
afterDelay:1.0]; //delay for 1 second
}
This method then puts them back to their original state (so the label is hidden and the button is activated again)
-(void) delayedDisplay:(NSString *)string{
[button setEnabled:YES];
[label2 setHidden:YES];
}

MPMoviePlayerController adding UIButton to view that fades with controls

I am trying to add a UIButton to the view of a MPMoviePlayerController along with the standard controls. The button appears over the video and works as expected receiving touch events, but I would like to have it fade in and out with the standard controls in response to user touches.
I know I could accomplish this by rolling my own custom player controls, but it seems silly since I am just trying to add one button.
EDIT
If you recursively traverse the view hierarchy of the MPMoviePlayerController's view eventually you will come to a view class called MPInlineVideoOverlay. You can add any additional controls easily to this view to achieve the auto fade in/out behavior.
There are a few gotchas though, it can sometimes take awhile (up to a second in my experience) after you have created the MPMoviePlayerController and added it to a view before it has initialized fully and created it's MPInlineVideoOverlay layer. Because of this I had to create an instance variable called controlView in the code below because sometimes it doesn't exist when this code runs. This is why I have the last bit of code where the function calls itself again in 0.1 seconds if it isn't found. I couldn't notice any delay in the button appearing on my interface despite this delay.
-(void)setupAdditionalControls {
//Call after you have initialized your MPMoviePlayerController (probably viewDidLoad)
controlView = nil;
[self recursiveViewTraversal:movie.view counter:0];
//check to see if we found it, if we didn't we need to do it again in 0.1 seconds
if(controlView) {
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[controlView addSubview:backButton];
} else {
[self performSelector:#selector(setupAdditionalControls) withObject:nil afterDelay:0.1];
}
}
-(void)recursiveViewTraversal:(UIView*)view counter:(int)counter {
NSLog(#"Depth %d - %#", counter, view); //For debug
if([view isKindOfClass:NSClassFromString(#"MPInlineVideoOverlay")]) {
//Add any additional controls you want to have fade with the standard controls here
controlView = view;
} else {
for(UIView *child in [view subviews]) {
[self recursiveViewTraversal:child counter:counter+1];
}
}
}
It isn't the best solution, but I am posting it in case someone else is trying to do the same thing. If Apple was to change the view structure or class names internal to the control overlay it would break. I am also assuming you aren't playing the video full screen (although you can play it fullscreen with embeded controls). I also had to disable the fullscreen button using the technique described here because the MPInlineVideoOverlay view gets removed and released when it is pressed: iPad MPMoviePlayerController - Disable Fullscreen
Calling setupAdditionalControls when you receive the fullscreen notifications described above will re-add your additional controls to the UI.
Would love a more elegant solution if anyone can suggest something other than this hackery I have come up with.
My solution to the same problem was:
Add the button as a child of the MPMoviePlayerController's view;
fade the button in and out using animation of its alpha property, with the proper durations;
handle the player controller's touchesBegan, and use that to toggle the button's visibility (using its alpha);
use a timer to determine when to hide the button again.
By trial-and-error, I determined that the durations that matched the (current) iOS ones are:
fade in: 0.1s
fade out: 0.2s
duration on screen: 5.0s (extend that each time the view is touched)
Of course this is still fragile; if the built-in delays change, mine will look wrong, but the code will still run.

Making a Cocoa Button look like is pressed, programmatically

I have some key events, that correspond to buttons also. What property/method to I have to set/call for a button to look depressed (change state?) for say half a second?
The way I solved this is I set the NSButton to a type of 'Push On Push Off' and then used the following code in my key event handler:
NSButton *button = [self.superview viewWithTag:event.keyCode];
if (button != nil && button.state == NSOffState) {
[button performClick:event];
[button performSelector:#selector(performClick:) withObject:event afterDelay:0.5];
}
This will highlight the button as if the user had clicked on it, and then it will click on it again in half a second.
I believe the button cell's -setHighlighted: method controls whether the button looks pressed or not. You may also need to call -setNeedsDisplay: on the button after changing it, and it's possible that the button will change its cell's highlighted state by itself, so I'm afraid you may need to fiddle around to get this working.
(I have to admit, though, that I'm not entirely certain about any of this.)