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.
Related
I have UIscrollview in which I have placed images.I use the buttons(touch down) to move the position of the images by 5pixels inside the scroll view…..Button fires the action only if I repeatedly touches the buttons ( it moves the images to 5pixels if I touches the button again and again) I want my button to fire the action until I keep the button down (it should moves the images until I release the button)…
Is there any possibility to scroll the UIscrollview by means of button click?
You'll have to listen to the TouchDown and TouchUp events. When a TouchDown event is fired you start a timer (or other) that periodically calls a method, and when you receive a TouchUp event you stop it.
It might looks something like that:
-(IBAction)touchDownAction:(id)sender
{
self.yourtimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(aselector) userInfo:nil repeats:YES];
}
-(IBAction)touchUpAction:(id)sender
{
if ([yourtimer isValid])
{
[yourtimer invalidate];
}
}
(You just have to link your button with these methods via IB)
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
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");
}
}
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
}
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)")
}
}