Xcode Add Delay So Button Press Can Be Registered - iphone

So I have an application that takes in images from the video camera and displays information to the screen after some processing on the image. I've added a pause button so the user can 'freeze' the most recent results on the screen.
The pause works, but it is one cycle behind. By the time the button press is recorded, the next image is already being processed so the results that are being locked on the screen are actually from the subsequent image. I've tried adding a delay to allow the user time to press pause using:
[self performSelector:#selector(waitForPause) withObject:nil afterDelay:2];
but this does not perform as I was expecting. Any ideas on how to handle this pause correctly?
EDIT: I've noticed that the AppDelegate file has some functions that appear to handle this sort of thing. Namely, applicationWillResignActive can supposedly be used to pause a game based on the apple docs. Has anyone used this for such a purpose? The method is currently empty. What would I have to add to this method to get it to temporarily pause the app based on a button press?
UPDATE: Here is how I currently implement and handle the pause button press.
//Pause Button
self.pauseButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[pauseButton addTarget:self
action:#selector(pauseButtonPress:)
forControlEvents:UIControlEventTouchDown];
[pauseButton setTitle:#"Pause" forState:UIControlStateNormal];
pauseButton.frame = CGRectMake(80.0, 100.0, 160.0, 40.0);
[self.view addSubview:pauseButton];
- (void) pauseButtonPress:(id) sender {
[pauseButton setHidden: YES];
[playButton setHidden: NO];
continueRunningScript = NO;
NSLog(#"paused");
NSLog(#"Current Result: %#", result_string);
}
Thanks.

You don't need to implement delays. Try this:
Each time you render a new image to the screen, save the old image in a previousImage variable. When the user hits the pause button, simply swap out what is currently on-screen for with the contents of previousImage.

Related

How to have the selected state of a UIButton change before the method is called?

I'm calling a method from a uibutton (an array of uibuttons in this case). The problem is that the button should change its UIControlStateSelected immediately and display a new uiimage for the button. It shows the image but only after running the method. The method downloads some data and takes a second or two. The user obviously isn't sure if they touched the button or not in that case until the method is called and the button image selected state changes the image.
Here's the code I'm using for the button(s) selected state:
[catBtn setTitle:[NSString stringWithFormat:#"Button %d", i] forState:UIControlStateNormal];
[catBtn setImage:[UIImage imageNamed:[categoryBtnImages objectAtIndex:i]] forState:UIControlStateNormal];
[catBtn setTitle:#"I'm selected" forState:UIControlStateSelected];
[catBtn setImage:[UIImage imageNamed:[categoryBtnImagesSel objectAtIndex:i]] forState:UIControlStateSelected];
// NSLog(#"%#",[categoryBtnImagesSel objectAtIndex:i]);
[catBtn addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
[catButtonArray addObject:catBtn];
thanks for any help with this.
Best guess given only partial IBACTION code:
If you are performing an synchronous download in the button's IBACTION method that is keeping the UI from updating the button. Perform the download with an asynchronous download method so the IBACTION method can complete and the button can update.
The NSURLConnection method sendAsynchronousRequest is an easy way to perform an async download:
+ (void)sendAsynchronousRequest:(NSURLRequest *)request
queue:(NSOperationQueue *)queue
completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*))handler
The interface should never be blocked like that. Imagine the frustration of the user wondering what is happening while the button gets pressed. Imagine yourself thinking whether you pressed it or not. If it takes a second or two, it should be done outside of the main thread, so the interface doesn't freeze and the user immediately knows what is going on.
If you are trying to download something, you should certainly look at the NSURLConnection
docs page, and use the sendAsynchronousRequest:queue:completionHandler: method.
You could move the 'loading' code into a seperate method.
Then you call this loading method from within the action method by
[self performSelector:#selector(loadingMethod) withObject:nil afterDelay:0.0];

MPMoviePlayerController Add custom Play button

Currently i am working on a iPhone Application Which is displaying video. I have used MPMoviePlayController to load the Video from the local folder.
Can we customize the Standard media player ? What i want to do is to hide all the standard elements (Play, Forward, Rewind, Done) and just to build the player with a single play button with a customized image . Can anybody help me ?
You can surely add custom controls for MPMoviePlayerController. For that first hide existing controls using, MPMovieControlStyle, set this to MPMovieControlStyleNone
Now add your custom control buttons and handle all the MoviePlayer events over there, like;
Play
Pause
Stop
Prev
Next, etc.
Or you can add your own controls like jump to this time (+10 sec, +20 sec), movie speed control (1x, 2x, ..), etc.
Hope this will be useful for you.
if you want to have any controlStyle of apple and still want to add some custom buttons on movie view. You can subclass MPMoviePlayerViewController.
Let say you have MyCustomMoviePlayerViewController as subclass of MPMoviePlayerViewController then in MyCustomMoviePlayerViewController.m
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"MyCustomButton" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview :button];
}

Temporarily Stop UIButton from responding

I have a button in my app that calls a long asynchronous function that we do not want to have called multiple times concurrently. However when I start mashing the button, it always sends as many messages as taps. I want to stop the button from responding while the function is running. I currently have a flag that stops execution of the function when the function is active in another thread, and the button is set to be hidden as soon as it enters this function, but these don't have any effect.
Thanks for any help!
Assuming:
Your button has an IBOutlet reference.
You have an IBAction on the button.
Then you can simply set the button's Enabled property to NO, and re-enabled when you receive notification from your ASYNC task that its done.
-(IBAction) buttonClicked {
[myButton setEnabled:NO];
//Do stuff
}
-(void) notificationHandlerMethodForAsyncTaskDone:(NSNotification *)notification {
[myButton setEnabled:YES];
//Do stuff
}
Inside the method that handle the touch event you can put disable the button:
- (void)handleTouch:(id)sender {
// Do your asynchronous call
[sender setEnabled:NO];
}
Instead of thinking about disabling the button why not make the screen inactive. Show some message like "Processing..." or a Spinner. That way the user will know the something is happening & at the same time your problem is solved.
DSActivityView is a good library for this.
You can use the enabled property to turn the button off:
[myButton setEnabled:NO]
Documentation

Touch Event on image , achieved by using UIButton but show up delayed compare to UIImageView

Sorry for the messy title, I just don't know how to describe the problem in a delicate way.
I'm writing a album-like app to display a bunch of image in my scrollview and do something when a image is touched.
I followed this question : how can i detect the touch event of an UIImageView and use button with background image to handle touch event.
My original method is using NSOperation to concurrently fetch image from internet and put it io a imageview and add to my scrollview, and the speed is quite ok because each imageview shows right after each NSOperation callback.
Then I change imageview to uibutton, the strange thing is that when a NSOperation callback, that button does not show in my view. They show up at once when all the NSOperation callback is done. That makes the user experience become unacceptable.
This is my NSOperation callback function, it will pass a button that contains the image fetched from internet
- (void)imageLoaded:(UIButton*)button;
{
NSLog(#"Done");
[button addTarget:self action:#selector(buttonPressed:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
The buttons will only displa after the last "Done" appear instead of one by one, is that normal? or I messed up something?
==== update ========
I think I'm running the NSOperation on my viewcontroller. I have a imageLoadOperation class, I'll pass my viewcontroller to it
imageLoadOperation *ilo = [[imageLoadOperation alloc] initWithURLString:[NSString stringWithFormat:#"link for the image"];
[ilo setParent:self];
[queue addOperation:ilo];
[ilo release];
And in the main function of imageLoadOperation I'll do
[parentViewController performSelectorOnMainThread:#selector(imageLoaded:) withObject:button waitUntilDone:NO];
Do you mean I need to move these code to my AppDelegate instead of running in my viewcontrollor?
You can use a button of custom type over your image view instead of using button with background image, or you can use touch event on an UIImageView
(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
What if instead of converting to a button you just allowed your UIImageView to handle the touch event?
To add a touch event to a UIImageView use the following format in your .m
UITapGestureRecognizer *newTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(newTapMethod)];
[_imageButtonName setUserInteractionEnabled:YES];
[_imageButtonName addGestureRecognizer:newTap];
then create the newTapMethod (or what ever you name it)
-(void)newTapMethod{
//...
}
hope that helps.

How to design a Navigation-Based app for the iPhone?

I am a new iPhone developer learning Objective-C, and trying to build an iPhone app using a Navigation-Based template. The way I want this application to function is to have a button, which takes the user to the second screen after being pressed. Unfortunately, I have not seen any examples of this online, and am unsure how to do this.
I realize that I won't be using a table view, where I would select a particular row that would then take me to another screen. What view would be appropriate to have a button, and possibly a search bar on the first screen, which would then allow me to navigate over to a second screen? I would need the second screen to have a table view which would hold a list of rows retrieved from a sqlite database. What method in the RootViewController (i.e. the first screen) would I use to place the code to execute when the button is selected (i.e. fires an event)?
Not sure I understand you question correctly, but...
In RootViewController:
- (void) viewDidLoad
{
UIButton* button = [UIButton buttonWithStyle:...];
[button setFrame:CGRectMake(0, 0, 10, 10)];
[button addTarget:self action:#selector(onButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[[self view] addSubview:button];
}
- (void) onButtonPressed
{
SecondViewController* controller = ...;
[[self navigationController] pushViewController:controller animated:YES];
}
The addTarget:action:forControlEvent: selector registers the 'callback' that should be called every time the button is pressed. In the callback (onButtonPressed) you just create new controller and push it into the navigation controller.
Usually you should do it in the Interface Builder. Just add IBAction and connect it to the event.