How to add action to SneakyButtonSkinnedBase? (COCOS2d) - iphone

in one of my layer i have this button. now how can i add action on this button like jump,run etc?
SneakyButtonSkinnedBase *jumpbuttonBase=[SneakyButtonSkinnedBase new];
jumpbuttonBase.position=jumpButtonPosition;
jumpbuttonBase.defaultSprite=[CCSprite spriteWithFile:#"jumpUp.png"];
jumpbuttonBase.activatedSprite=[CCSprite spriteWithFile:#"jumpDown.png"];
jumpbuttonBase.pressSprite=[CCSprite spriteWithFile:#"jumpDown.png"];
jumpbuttonBase.button=[[SneakyButton alloc]initWithRect:jumpButtonDimensions];
jumpButton=[jumpbuttonBase.button retain];
jumpButton.isToggleable=NO;
[self addChild:jumpbuttonBase];

In your layer that you added the button to, have an update scheduled.
[self scheduleUpdate];
In the update method is where you check if the button has been pressed, and then you decide what action you want to perform.
-(void) update:(ccTime) delta
{
if (jumpButton.active)
{
// do something here
}
}

Related

how to check that any of the button is clicked or not in iphone application

I have six button i want that user must select any of the six button before moving to next screen if it does not select any then it should not move to next screen I have read to use BOOL flag but this show which button is clicked i want that if any of the six buttons is clicked then user can move to next screen
-(IBAction)locationOneButtonAction{
// your stuff
_flag = YES;
}
-(IBAction)locationTwoButtonAction{
// your stuff
_flag = YES;
}
I think the best solution this problem is to declare a bool for your class (let's say it is called "ClickViewController" like (so at the top)
#interface ClickViewController () {
bool wasClicked;
}
...
-(IBAction)locationOneButtonAction{
// your stuff
wasClicked = YES;
}
Then when the button that should move to the next page is clicked utilize a method like this.
-(void)nextPageClicked{
if (wasClicked){
[self performSegueWithIdentifier:#"segueIdentifier" sender: self];
} else {
// do something else here, like tell the user why you didn't move them
}
}
This means that you cannot draw the segue from the button to the next view. If you are using storyboards, this means that you can't draw the segue from the button that is supposed to move it to the next view, rather you should draw it from the view controller icon to the next view. Then select the segue, name it, and use the method above with the name.
I'm also not sure if you mean that you want each of the buttons to move to the next view, if that is the case, you can use basically the same code, but just put the code [self performSegueWithIdentifier:#"segueIdentifier" sender: self]; line in action for the button.
Hopefully this helps.
Intially add BOOL _flag; int curIndex; in .h file.
Now add intially _flag = FALSE; in viewDidLoad method as it indicate no button clicked.
Make userInteractionEnabled for each button to be able to click and add tag to each button.
Now in action method of each click of button do this:
(IBAction)locationOneButtonAction:(id)sender{
curIndex = [sender tag]; //which button clicked
// your stuff
_flag = TRUE;
}
-(IBAction)locationTwoButtonAction{
curIndex = [sender tag]; //which button clicked
// your stuff
_flag = YES;
}
........
........
Now check like this if button clicked:
if(_flag)
{
NSLog(#"Button clicked with tag: %d",curIndex);
}
else
{
NSLog(#"Not any Button clicked");
}

Adding "Breakpoint" or Pause in iPhone App

I have an app that is continuously taking in images from the video buffer (using the process described here: http://www.benjaminloulier.com/articles/ios4-and-direct-access-to-the-camera) and performing various processing on the most recent image in the buffer. For my particular application, when something noteworthy is found in the image, I want to display this information to the user and have the user decide whether the information is correct or not.
I want to display 2 UIButtons on the screen when this information is returned and it is at this point that I wish the code to "pause" (like a runtime breakpoint) and wait to see which button the user clicks before resuming. After clicking a button, the buttons will disappear.
The reason for this is I can't have the camera continue to acquire images and process them while I am waiting for the user input.
Thanks!
EDIT:
Here is what my code basically looks like:
if (continueRunningScript == YES) {
NSString *results = [self processImage];
[self displayResults: results];
// Show pause button
dispatch_async(dispatch_get_main_queue(), ^{
[pauseButton setHidden: NO];
});
}
and the pause button code:
- (UIAction) pauseButtonPress:(id) sender {
[pauseButton setHidden: YES];
[playButton setHidden: NO];
continueRunningScript = NO;
}
and the play button code:
- (UIAction) playButtonPress:(id) sender {
[playButton setHidden:YES];
continueRunningScript = YES;
}
Where could I add more boolean to handle the delay?

iphone button pressed for 3 seconds goes to a different view

I have a button in my app that when pressed goes to a view
but I need that when pressed for 3 seconds it would go to a different view,
like when you are on ipad on safari and you keep pressed the url, and it shows a pop up with copy etc,
but I need that when pressed for 3 second it goes to another view...
hope this makes sense, I will explain better if not understood,
thank you so much!
pd, also how to make it show the pop up style window?
cheers!
Try setting an NSTimer property in your view controller. When the button's pressed, create the timer and assign it to your property. You can detect that moment with this:
[button addTarget:self action:#selector(startHoldTimer) forControlEvents:UIControlEventTouchDown];
and assign with this:
-(void) startHoldTimer {
self.myTimer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:#selector(goToNewView:) userInfo:nil repeats:NO];
}
Then set an action to run on a canceled touch, or a touch up inside:
[button addTarget:self action:#selector(touchUp) forControlEvents:UIControlEventTouchUpInside];
[button addTarget:self action:#selector(cancelTimer) forControlEvents:UIControlEventTouchCancel];
and
//if timer fires, this method gets called
-(void) goToNewView {
[self cancelTimer];
[self loadSecondView];
}
// normal button press invalidates the timer, and loads the first view
-(void) touchUp {
[self cancelTimer];
[self loadFirstView];
}
//protected, just in case self.myTimer wasn't assigned
-(void) cancelTimer {
if (self.myTimer != nil)
if ([self.myTimer isValid]) {
[self.myTimer invalidate];
}
}
That should take care of it!
Use a UILongPressGestureRecognizer, added to the button with -addGestureRecognizer:—it'll handle timing the touch and fire an event when it recognizes that the button's been held down for a while. You might want to reconsider your interaction pattern, though—generally, things that can be long-pressed aren't buttons, they're actual pieces of data in a view, like an image or a link in Safari.
One possible approach would be to implement one of the touches events (I don't remember the name, but the method that fires when you touch down on the button), and schedule a timer to fire in three seconds. If the user lifts her finger before that time, cancel the timer and perform the normal button click. If the time does fire (i.e. 3 seconds have elapsed), ignore the touch up event and load your new view.

How to Recognize a Hold Button {iPhone SDK}

Hi i have a Button i want hold this button to write something but i don't know how can i recognize hold button , can you help me ? thank you
TouchDownInside event triggered, start a NStimer.
TouchUpInside event triggered, cancel the timer.
Make the timer call your method to execute if the user holds the button : the timer delay will be the amount of time required to recognize hold.
You can also use UILongPressGestureRecognizer.
In your initialization method (e.g. viewDidLoad), create a gesture recognizer and attach it to your button:
UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:#selector(myButtonLongPressed:)];
// you can control how many seconds before the gesture is recognized
gesture.minimumPressDuration = 2;
// attach the gesture to your button
[myButton addGestureRecognizer:gesture];
[gesture release];
The event handler myButtonLongPressed: should look like this:
- (void) myButtonLongPressed:(UILongPressGestureRecognizer *)gesture
{
// Button was long pressed, do something
}
Note that UILongPressGestureRecognizer is a continuous event recognizer.
While the user is still holding down the button, myButtonLongPressed: will be called multiple times.
If you just want to handle the first call, you can check the state in myButtonLongPressed::
if (gesture.state == UIGestureRecognizerStateBegan) {
// Button was long pressed, do something
}

resignFirstResponder for numberpad

There is a text field to enter PIN in my login form. When i press "login" button i call the following method:
* (IBAction) loginBeforeAction:(id) sender {
[pin resignFirstResponder];
[progressView performSelectorInBackground:#selector(startAnimating) withObject:nil];
[self login];
}
but i the number pad is not hiding before the control moves to login method. In effect, i am can see the progress view with the number pad up. Is there any way to hide the number pad first and then show progress view ? plz help
Yes, The UI won't update until you get through the runloop. Meaning, your UI doesn't update until your login method finishes. Also, no need to update your progressView in the background either.
So, just delay the calls:
[progressView performSelector:#selector(startAnimating) withObject:nil afterDelay:0.1];
[self performSelector:#selector(login) withObject:nil afterDelay:0.25];