How to find touches on button using timer - iphone

I have button event. If single tap(touch) on button i am doing Flip action . If Double tap on button i am doing another action that is unhidden other button. Both same button event.i tried in image touches but not getting. How to find single tap or double Tap on button . how to implement timer to find touches any sample or tutorial ..

This might get you started..
(i have used 0.5 for a timeout value, but you might want to change this)
-(void)butonEvent:(id)sendr{
if(buttonTimer)[self doublePress];
else
{
//buttonTimer is a local NSTimer
buttonTimer = [NSTimer scheduledTimerWithTimeInterval: 0.5
target: self
selector: #selector(handleTimer:)
userInfo: nil
repeats: NO];
}
}
-(void)singlePress{
[buttonTimer invalidate];buttonTimer=nil;//edit
NSLog(#"Single Press");
}
-(void)doublePress{
[buttonTimer invalidate];buttonTimer=nil;//edit
NSLog(#"doublePress");
}
-(void)handleTimer:(id)sendr
{
[self singlePress];
}

There is two options in UIButton Events,
You can use the following to detect,
single touch through TouchDown,
double touch through TouchDownRepeat.
You can connect ur UIButtons using IB same way as like TouchUpInside and write ur coding accoring to that.

Related

Is it possible to move the view in a particular interval and also with button action

I am developing an application for both iPhone and iPad. Here, i would like to load a url in the uiwebview, at ( 0.5) time interval. After finish loading of web view, i want to navigate to the next view. I have implemented this, by adding the navigation coding to [webViewDidFinishLoad:] and it works fine. Now, my client needs to add a button in the view to navigate if the user not interested to stay in the web view for (0.5 ) sec.
Is i should use separate (selector:) action for after delay : 0.5 and for round rect button ?
if it so, how can i implement this,
I googled it, but could not find the solution. Any help appreciated. Thanks.
Thanks so much for the responses. Here, i have implemented "NSTimer" concept instead of using "PerformSelector:afterDelay". Here is the code,
timer = [NSTimer scheduledTimerWithTimeInterval: 5.0
target: self
selector: #selector(goToNextView)
userInfo: nil
repeats: NO];
// here i have set the time interval of '5.0' and after that taking into next view.
-(void)goToNextView
{
newtest15 *nt=[[newtest15 alloc]init];
[self.navigationController pushViewController:nt animated:YES];
}
And i have the button at the toolbar which is also used for navigating to the next view.
-(IBAction)btn:(id)sender{
if ( [timer isValid])
{
[timer invalidate], timer=nil;
}
newtest15 *nt=[[newtest15 alloc]init];
[self.navigationController pushViewController:nt animated:YES];
}
/* here, when the user wants to move to next view before the time interval of '5', and presses the button at the bottom, this action will be performed, it will invalidate the timer and navigate to the next view*/
You can implement performSelector: withObject: afterDelay: 0.5 sec in the viewdidload method.
Then define a method that display alert message buttons stay out or leave the page. then do some coding on alert view buttons

how to detect button pressed and released in iphone

how to detect a button is pressed and released because i want to perform two actions
first is auto increment when a button is pressed and hold a...
and to stop the auto increment when a button is released .......
please tell me what button actions are to be used to do this ....
i tried with touchup inside touch down touch up out side but it is not working correctluy can any one please help me how to make it working correctly.....
thank you.
Ok, first you need an interface button. The Button's target action for Touch Down should trigger an action like this:
if (someTimer == nil) {
someTime = [NSTimer scheduledTimerWithTimeInterval:.03 target:self selector:#selector(theDecreasingMethod) userInfo:nil repeats:YES];
}
Then you need another IBAction set to that same button (YES you can set multiple actions to one button) and link that action to Touch Up Inside
Then in the method associated with the touch up inside action:
if (someTimer != nil) {
[someTimer invalidate];
someTimer = nil;
}
Finally, in theDecreasingMethod, you need this
-(void)theDecreasingMethod{
//do what ever you need to incrementally do
//ie. someInt--;
}
I hope this helps; comment if you need anything else

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.

detecting long tap on iPhone

I am working on an iPhone app which requires me to check if the button has been tapped & held pressed for 6 seconds & then fire an action which is playing some sort of sound.
How should I detect this 6 second tap?
On the other hand the user can also keep on tapping button for 6 seconds & then the same action should fire.
What should I do with multiple taps, how would I know that all the taps fall under the 6 second bracket?
For a six second long press, use a UILongPressGestureRecognizer with its minimumPressDuration property set to 6.
Write your own gesture recognizer (say, LongTappingGestureRecognizer) for continuous tapping for a given period; it shouldn't be too tricky. Give it a property like UILongPressGestureRecognizer's minimumPressDuration (say, minimumTappingDuration) and a property (say, maximumLiftTime) that determines how long a finger can be lifted off before it's not considered to be a long tapping gesture.
When it first receives touchesBegan:withEvent:, record the time.
When it receives touchesEnded:withEvent:, start an NSTimer (the lift timer) that sends the gesture recognizer a cancel message (e.g. cancelRecognition) after maximumLiftTime.
When it receives touchesBegan:withEvent: when there's a start time, cancel the lift timer (if any).
The cancelRecognition will transition to the failed state.
There are various strategies for handling recognizing when the end of the gesture is reached, after minimumTappingDuration. One is to check in both the touchesBegan:withEvent: and touchesEnded:withEvent: handlers if the difference between the current time and the start time is >= minimumTappingDuration. The problem with this is that it will take longer than minimumTappingDuration to recognize the gesture if the user is tapping slowly and hir finger is down when the minimumTappingDuration is reached. Another approach is to start another NSTimer (the recognition timer) when the first touchesBegan:withEvent: is received, one that will cause transition to the recognized state and that is cancelled in cancelRecognition. The tricky thing here is what to do if the finger is lifted when the timer fires. The best approach might be a combination of the two, ignoring the recognition timer if the finger is lifted.
There's more to the details, but that's the gist. Basically, it's a long press recognizer that lets the user lift hir finger off the screen for brief periods. You could potentially use just the tapping recognizer and skip the long press recognizer.
I realize this is quite dated question, however answer should be pretty simple.
In your View controller viewDidLoad:
//create long press gesture recognizer(gestureHandler will be triggered after gesture is detected)
UILongPressGestureRecognizer* longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(gestureHandler:)];
//adjust time interval(floating value CFTimeInterval in seconds)
[longPressGesture setMinimumPressDuration:6.0];
//add gesture to view you want to listen for it(note that if you want whole view to "listen" for gestures you should add gesture to self.view instead)
[self.m_pTable addGestureRecognizer:longPressGesture];
[longPressGesture release];
Then in your gestureHandler:
-(void)gestureHandler:(UISwipeGestureRecognizer *)gesture
{
if(UIGestureRecognizerStateBegan == gesture.state)
{//your code here
/*uncomment this to get which exact row was long pressed
CGPoint location = [gesture locationInView:self.m_pTable];
NSIndexPath *swipedIndexPath = [self.m_pTable indexPathForRowAtPoint:location];*/
}
}
Here is my solution.
- (IBAction) micButtonTouchedDownAction {
self.micButtonTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:#selector(micButtonAction:) userInfo:nil repeats:YES];
self.micButtonReleased = FALSE;
}
- (IBAction) micButtonTouchedUpInsideAction {
self.micButtonReleased = TRUE;
}
- (IBAction) micButtonTouchedUpOutsideAction {
self.micButtonReleased = TRUE;
}
- (void) micButtonAction:(NSTimer *)timer {
[self.micButtonTimer invalidate];
self.micButtonTimer = nil;
if(self.micButtonReleased) {
NSLog(#"Tapped");
}
else {
NSLog(#"Touched");
}
}

Way to make a UIButton continuously fire during a press-and-hold situation?

You know how Mario just keeps running to the right when you press and hold the right-button on the D-Pad? In the same manner, I want my UIButton to continuously fire its action for the duration that it is held down. Is this possible for a UIButton? If not, is this possible to do with a UIImageView by overriding a touch handling method in a certain way? Actually, before trying to do get this done with UIButton I had some UIImageViews (Arranged to function as a D-Pad) that were checked by touch handling methods but things started to get messy so I thought this could be done easier with UIButton and thus switched over. Anybody who knows how to get recognition of a continuous, stationary (not-moved) down-touch, please share.
You can also do similar to what is shown in the previous answer and still use a UIButton.
Just have the timer started on the "Touch Down" and have the timer stopped on either "Touch Up Inside" or "Touch Up Outside".
Personally, I like using UIButtons because they offer some built in visual enhancements you don't have to code on your own.
Don't use a button, use multi-touch and NSTimer:
Make a view-local NSTimer object inside your interface, then use it to start/cancel the timer
-(void)movePlayer:(id)sender {
<Code to move player>
}
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
timer = [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:#selector(movePlayer:) userInfo:nil repeats:YES];
}
-(void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
if (timer != nil)
[timer invalidate];
timer = nil;
}
-(void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
if (timer != nil) {
[timer invalidate];
timer = nil;
}
}
This way, you can repeat the event at a predefined interval, and not have to rely on a button, and get the repeat behaviour you're looking for.
Note the touchesMoved trigger - if they move their finger, this cancels the timer, and the player stops moving.
For me the following works:
Create a button.
Create 2 methods (stop touching and start touching) either in view controller or to a subclass.
Add 3 Control Events. Touch Up Inside and Touch Drag Exit that both of them go to stop touching method and Touch Down goes with start touching method.
When start touching method invokes we should start an NSTimer with interval approximately 0.2 (it's up to you how fast you would like to be invoked), repeat true and as a selector a method that you want to be invoked (having the actual stuff you want to execute when user hits the button).
When stop touching method invokes we should invalidate timer
(.invalidate()) and assign timer as nil.
That's all!
And Now for Something Completely Different:
ReactiveCocoa 6.
self.button.reactive
.controlEvents([.touchDown])
.observeValues { button in
SignalProducer.timer(interval: .milliseconds(500), on: QueueScheduler.main)
.take(until: button.reactive.controlEvents([.touchDragOutside, .touchDragExit, .touchUpInside, .touchUpOutside, .touchCancel]).map { _ in return })
.prefix(value: Date())
.startWithValues { date in
NSLog("\(date)")
}
}