ObjC Button Released Touch Up Outside - iphone

I have a button, when held down motion occurs, when released it should stop. I know I could write two methods, one to start and one to stop motion, I was wondering if in the one method I could detect both based on Touch Up Outside vs. Touch Up Inside.
If Touch Up Outside do this if Inside do this.
-(IBAction) startMotion: (id)sender {
NSLog(#"Forward or back button is being pressed.");
UIButton * buttonName = (UIButton *) sender;
NSLog(#"Button Name: %#", buttonName.currentTitle);
}

Yes, you could write one method and link the IBAction "touchUpInside" and "touchUpOutside" to the same IBAction member function - via Interface Builder.
The only downside would be that you would not be able to tell if it was a touchUp inside or outside when it was called. (It doesn't sound like this matters to you.)
If the later was an issue, you could alternately implement independent touchUpInside and touchUpOutside methods which in turn call the same method - a third custom one - into which you could pass an "inside" or "outside" flag from the proper IBAction handler.

Touch up Outside won't give you the functionality you're looking for, I don't think. The person would have to move their finger outside the boundaries of the button and then lift remove the finger
Why not use touchdown to initiate the motion and touch up inside to stop it?

Related

Detecting a finger being held on an object

I am trying to have an image that when the user touches it, it wiggles and as soon as the user lifts their finger it stops.
Is there a gesture that I can use to detect when the finger is down, not just on the initial touch, or when the user moves there finger?
I have tried a LongPress gesture, but that does not get called the entire time the finger is on the view. Can anyone help me with the best way to active this. Right now i am doing it using touchesBegin, touchesMoved, touchesEnd, but i was wondering if there is a better way.
Any suggestions are greatly appreciated.
Thanks
EDIT
Based on the comments, I slightly misunderstood the original question, so I edit my answer to a different solution, which hopefully is a bit more clear (and answers the actual question - not the one that was in my head).
A LongPress gesture is continuous (where a tap gesture is not). That means, the recognizer callback will continue to be invoked until the gesture is complete - which does not happen until the "longpress" is released. So, the following should do what you want. NOTE: I think you want to "start shaking" a view when the long-press is recognized, then "stop shaking" the view when the fingers are released. I just pretended you have functions for that. Substitute appropriately.
- (void)handleLongPress:(UILongPressGestureRecognizer*)gestureRecognizer
{
if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
StartShakingView(gestureRecognizer.view);
} else if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
StopShakingView(gestureRecognizer.view);
}
}
The Apple Touches sample includes code that demonstrate using both UIResponder and UIGestureRecognizer methods.
Either should work for what you're doing.
Simple answer - you could make the image a UIButton, and start the wiggle on TouchDown, and stop it on TouchUpInside or TouchUpOutside
It sounds like you want to subclass UIGestureRecognizer, which, as I recall, gets the touchesBegan:... and associated methods. Read the notes on subclassing in the UIGestureRecognizer reference. Or use a UIButton as SomaMan suggests.

iphone sdk how to catch a long press after 1 second?

I'm using UILongPressGestureRecognizer to catch a Long Press event. The problem is I can only respond to it after user release his finger.
How do I implement a function that will respond after user hold for x seconds?
UILongPressGestureRecognizer works for this, check your code again and set duration for long press as -
[longPressGesture setMinimumPressDuration:<#(CFTimeInterval)#>];
Instead of using a gesture recognizer, use the UIView instance methods touchesBegan:withEvent:, touchesMoved:withEvent: and touchesEnded:withEvent:. While they're not as convenient as using a gesture recognizer, you have complete control of the touch interpretation.
Note that if you every try to use these to interpret multitouch gestures, the touch screen is "bouncy", in that the reported number of touches will vary during the touch event processing. My app interprets pinches, which are reported to me as a random sequence of one and two finger touches. I managed to debounce it, but getting the code right was a real PITA.

willSelectRowAtIndexPath, didSelectRowAtIndexPath are not calling in iphone when using touches began method and performselector

I am developing an application, in which one of the screen i am using have tableview, it is tableview controller. i have written custom touchesbegan,touchesmoved,touchesended methods insside a tableview controller class, when ever user presses on any cell touchesbegan method is calling and withing this to detect long tap what i did was
[self performSelector:#selector(longTap) withObject:nil afterDelay:1.0];
if user removes his finger before 1sec, in touchesmoved and touchesended methods i am canceling the performselector.
now what happens is when i press & hold for 1min longTap method is calling and willSelectRowAtIndexPath, didselectRowAtIndexPath are not calling. but i need to get the indexpath.row value before performing any action on longTap method.
Can any one help me on this issue. please share your valuable suggestions.
Thanks in advance.
You should consider using a gesture recognizer like UILongPressGestureRecognizer. Fiddling with the touch events yourself is hard and error prone.
It is very easy to break a flow of events that UIKit classes depends upon to function. Gesture recognizers is the public API for hooking into the touch event flow, in simple and advanced easy, without breaking UIKit functionality.

How can I determine whether my UIButton's event is Touch Down?

How can I determine whether my button's event is Touch Down?
I want to do a function like this:
if(users click on touchdown event)
{
NSLog(#"a");
}
else if(users click on touchupinside event)
{
NSLog(#"b");
}
Eather you set two different IBAction methods in the InterfaceBuilder or you set two different targets via:
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
in your code while creating the button.
You "find out" by letting the button tell you when the event happens.
Add a method (or methods) like this:
- (IBAction)myButtonClick:(id)sender;
In Interface Builder, attach the method(s) to the events you're interested in.
You create a separate method for each type of event if you want different behavior for a TouchDown as opposed to TouchUpInside.
you attach each unique event to its own IBAction

Handling touchesBegan in UITableViewCell disables didSelectRowAtIndexPath

I'm implementing swipe gestures in my customtableviewcell control and so I hvae to implement touchesBegan event.
I'm able to implement the swipes, but unfortunately, because the touchesBegan gets handled in the customcell, i'm not getting a didSelectRowAtIndexPath message on the tablecontroller. If the touchesBegan method is disabled, it works.
How should this be handled? I want the touch event to bubble up the responder chain after the touchesBegan is processed. How can I do this?
Thanks.
I"m sure you can see that this is happening because you are overriding a method that was previously defined on a super class. and by doing so means that the events are not being called.
have you tried calling [super touchesBegan]? that way all upstream stuff is handled. and you can override the swipe gesture.
or another option is to call the delegate when the touch is detected in your own touches method.
something like (you will probably have implementations of the other touch events as well)
-(void) touchesBegan
{
//logic to detect tap only.
[tablecell.delegate didSelectRowAtIndexPath:(some way to determin touched row)]
}