Gesture recognizer on button - iphone

I'd like to implement a gesture recognizer (swipe action) for a button. The problem is, the buttons are create programmatically and are or aren't existent based on a few conditions. So, I don't know if there are buttons, or how many.
I know I need something like:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if (touch.view == aButtonView) {
//get the button's tag
}
}
Of course, the if-statement should return Yes when any button view is pressed...
Anyone has any idea on what the word aButtonView should be? Or if it's even possible? Thanks in advance.

You should think about using UISwipeGestureRecognizer instances. Attach the gesture recognizer to the button objects -
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:#selector(handleSwipe:)];
swipe.direction = UISwipeGestureRecognizerDirectionUp;
[button addGestureRecognizer:swipe];
[swipe release];
and in handleSwipe:
- (void) handleSwipe:(UISwipeGestureRecognizer *)swipe {
NSInteger tag = swipe.view.tag;
}
it should be if ( [gestureRecognizer.view isKindOfClass:[UIButton class]] ) {

Related

How to create Tap event on Long Tap gesture on UIWebView?

I have added Long Tap gesture on UIWebView. But I want UIWebView to process a standard Tap event before my Long Tap will be recognized. (Two gestures should be processed on Long Tap - a simple Tap and my Long Tap). How to do this?
I think it's required to send Tap event to UIWebView on TouchBegin. Is it correct?
The correct code:
- (void)viewDidLoad {
UILongPressGestureRecognizer* gesture = [[[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(handleLongPress:)] autorelease];
gesture.delegate = self;
[myWebView addGestureRecognizer:gesture];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
Please refer this section UIGestureRecognizerDelegate
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIGestureRecognizerDelegate_Protocol/Reference/Reference.html
you found this is called when 2 gesture simultaneous work.
gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:

UILongPressGestureRecognizer stop handle without stop touching

I'm using UILongPressGestureRecognizer class to handle if one item is being selected.
The logic is as follows: User press during 1 second an item (UIView subclass). Once the gesture is detected, the item is highlighted and moveable.
The user must move this item across the screen without stop touching it.
The problem I'm facing is the gesture recognized shadows touchesBegan/Move/Ended necessary for the item class to arrange the movement.
I tried to remove the gesture recognized once is detected and the item selected. But still sending messages to the handle of gesture instead of call touches methods.
Anyone knows any way to stop "listening" the gesture recognizer without leave the finger of the screen?
Thanks.
Here the code:
-(void)addGestures
{
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleLongPress:)];
longPress.minimumPressDuration = iItemLongPressTime;
[self addGestureRecognizer:longPress];
[longPress release];
}
- (void)handleLongPress:(UILongPressGestureRecognizer*)sender {
if (sender.state == UIGestureRecognizerStateEnded) {
NSLog(#"Long press Ended");
}
else {
if (self.isSelected) return;
if ([delegate respondsToSelector:#selector(singleTouch:)])
[delegate singleTouch:self];
[self removeGestureRecognizer:[self.gestureRecognizers objectAtIndex:0]];
NSLog(#"Long press detected.");
}
}
As you can see in the else branch the delegate calls enables all procedures to mark this item as selected, and just after remove the recognizers.
What I'm missing?
--EDIT--
Done! This works:
#pragma mark Gesture Functions
-(void)addGestures
{
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleLongPress:)];
longPress.minimumPressDuration = iItemLongPressTime;
[self addGestureRecognizer:longPress];
[longPress release];
}
- (void)handleLongPress:(UILongPressGestureRecognizer*)sender {
if (sender.state == UIGestureRecognizerStateEnded) {
NSLog(#"Long press Ended");
}
else {
NSLog(#"Long press detected.");
if (self.isSelected) return;
if ([delegate respondsToSelector:#selector(singleTouch:)])
[delegate singleTouch:self];
[sender removeTarget:self action:#selector(handleLongPress:)];
sender.enabled = NO;
[self removeGestureRecognizer:sender];
}
}
Regards!
Does the custom UIView class have its own touch handling code? If not, a simple solution is to set the allowableMovement property of the UILongPressGestureRecognizer to CGFLOAT_MAX, or some big number, and use the gesture update callbacks to drag your custom view around. You can get the displacement using the - (CGPoint)locationInView:(UIView *)view method on the superview, and compare its position to when the recognizer began.
There are two solutions in my mind.
For animating uiview, please wrote a new class which is inherited from the UIView class and implement the touch delegates instead of writing the Gustures to handle animation(if the touch delegates are not triggering in the current class).
2.I have successfully removed the UILongPressGestureRecognizer after triggered it once.
Please refer the below code .ask me if you have any queries
Steps I have Done
I have added a UIView as "myView" to my main-view when main-view loads.
I have given the Tag to the myView (you can give 1,2,3…etc) to differentiate the tapped view from the main-view subviews.
Assigned the UILongPressGestureRecognizer gesture to myView and assigned target as "moveMe" method.
When user Pressed the myView long, the "moveMe" method will trigger.
Then I iterated the mainView Subviews with the condition Tag == 1
I have removed the UILongPressGestureRecognizer from the subview.As we can know that Tagged 1 main-view subView is myView.
So the NSLog(#"gesture removed"); and NSLog(#"moveMe"); will log in console only at one time.
The NSLog(#"touchesBegan"); will trigger first instead of triggering the "moveMe" method.
Then NSLog(#"touchesBegan"); will trigger always after removed the gesture . "moveMe" method will not trigger ever.
Code
- (void)viewDidLoad {
//Adding to UIView to main view when application is loading.
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 80, 80)];
myView.backgroundColor = [UIColor viewFlipsideBackgroundColor];
myView.tag = 1; //adding a tag to identify it.
//Adding Long Press Gesture to the UIView.
UILongPressGestureRecognizer *myGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(moveMe:)];
[myView addGestureRecognizer:myGesture];
[myGesture release];
myGesture = nil;
[self.view addSubview:myView];
[myView release];
myView = nil;
[super viewDidLoad];
}
//Method to trigger when user pressed long on the added UIView.
-(void)moveMe:(id)sender
{
for (UIView *subViews in [self.view subviews])
{
if (subViews.tag == 1) {
[subViews removeGestureRecognizer:sender];
NSLog(#"gesture removed");
}
}
NSLog(#"moveMe");
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"touchesBegan");
}
or please refer Disable gesture recognizer iOS

iPhone iOS how to add a UILongPressGestureRecognizer and UITapGestureRecognizer to the same control and prevent conflict?

I'm building an iPhone app that would let the user rearrange some of the UI elements on the screen.
How can I add a tap gesture recognizer and a long press gesture recognizer to the same UIView? When I lift up the finger from the long press, the tap gesture recognizer fires. How can I temporarily disable the tap gesture recognizer or prevent it from firing when the user is performing a long press?
Thank you!
To allow both gestures to work together, implement the following delegate method:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
To make it so that the long press has first priority, do:
[tapGesture requireGestureRecognizerToFail:longPress];
To combine successfully both you need:
1º Add to interface gesture delegate at header
#interface ViewController : ViewController <UIGestureRecognizerDelegate>
2º Create gesture events and add to a view into source file:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(touch:)];
[tap setNumberOfTapsRequired:1]; // Set your own number here
[tap setDelegate:self]; // Add the <UIGestureRecognizerDelegate> protocol
UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longTouch:)];
[longTap setNumberOfTapsRequired:0]; // Set your own number here
[longTap setMinimumPressDuration:1.0];
[longTap setDelegate:self]; // Add the <UIGestureRecognizerDelegate> protocol
[tap requireGestureRecognizerToFail:longTap]; // Priority long
[self.view addGestureRecognizer:tap];
[self.view addGestureRecognizer:longTap];
3º Add callbacks in source file:
- (void) touch: (UITapGestureRecognizer *)recognizer
{
CGPoint location = [recognizer locationInView: self.HUDview];
if (recognizer.state == UIGestureRecognizerStateBegan)
{
NSLog(#"touch UIGestureRecognizerStateBegan");
}
if (recognizer.state == UIGestureRecognizerStateEnded)
{
NSLog(#"touch UIGestureRecognizerStateEnded");
//NSLog(#"Position of touch: %.3f, %.3f", location.x, location.y); // Position landscape
}
}
- (void) longTouch: (UILongPressGestureRecognizer *)recognizer
{
if (recognizer.state == UIGestureRecognizerStateBegan)
{
NSLog(#"longTouch UIGestureRecognizerStateBegan");
}
if (recognizer.state == UIGestureRecognizerStateEnded)
{
NSLog(#"longTouch UIGestureRecognizerStateEnded");
}
}
4º Set gesture recognizer available:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
As an alternative approach, don't have two separate recognisers - just use the LongPress recogniser for both events:
Configure as follows:
UILongPressGestureRecognizer* longPress = [ [ UILongPressGestureRecognizer alloc ] initWithTarget:self.nextResponder action:#selector(longPressEvent:)];
categoryPanelDrag.minimumPressDuration = 0.0;
Then handle as follows:
- (BOOL)longPressEvent:(UILongPressGestureRecognizer *)gesture {
// _dragStarted is a class-level BOOL
if(UIGestureRecognizerStateBegan == gesture.state) {
_dragStarted = NO;
}
if(UIGestureRecognizerStateChanged == gesture.state) {
_dragStarted = YES;
// Do dragging stuff here
}
if(UIGestureRecognizerStateEnded == gesture.state) {
if (_dragStarted == NO)
{
// Do tap stuff here
}
else
{
// Do drag ended stuff here
}
}
return YES;
}
I did try moby and journeyman's approach but somehow they didn't fit my project well, so I solved like below,
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
NSLog(#"%# %ld",touch.description, touch.phase);
[self performSelector:#selector(checkTouch:) withObject:touch afterDelay:0.5];
return YES;
}
and
- (void)checkTouch:(UITouch *)touch{
NSLog(#"touch phase = %ld",touch.phase);
if (touch.phase == UITouchPhaseStationary) {
//still holding my hand and this means I wanted longPressTouch
}
if (touch.phase == UITouchPhaseEnded){
//I released my finger so it's obviously tap
}
}
It could be simpler solution but of course it depends to project.
You could take care of it in the code, that during the long press, set a flag, and if the tap gets called while the flag is true or whatever then don't execute the tap code and reset the flag. I don't know a better way

UITapGestureRecognizer not doing anything

I've got an app that displays a page of text with the ability to tap a button or swipe in a view to advance or retreat through various pages. The container view has two UISwipeGestureRecognizers attached, for swipe left and swipe right. No trouble with these gestures. But now I'm trying to add UITapGestureRecognizer to another view, providing an ability similar to iBooks or the Kindle app, tap the left to go back, the right to go forward. Nothing I do can get the gesture to fire. No hint that it is ever being triggered, even if I put the gesture on my topmost view and disable other gestures.
The view controller implements UIGestureRecognizerDelegate, though I've not needed to implement delegate methods. I did try implementing shouldReceiveTouch: without success.
Here's a snippet of code where I create and attach the gesture recognizer:
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapGestureTurnPage:)];
[self.view addGestureRecognizer:recognizer];
[recognizer release];
Try this on the view you're adding the gesture recognizers:
view.userInteractionEnabled = YES;
You mention that you've tried this, but just in case go through it again.
Try using the delegate with <UIGestureRecognizerDelegate> in the header and then setting the delegate:
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapGestureTurnPage:)];
[self.view addGestureRecognizer:recognizer];
recognizer.delegate = self;
[recognizer release];
Then implement this method:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldReceiveTouch:(UITouch *)touch {
return YES;
}
Then use a debugger or toss an NSLog into the above method and also tapGestureTurnPage: to see if these methods are being called.
Add this where you initialize the gesture:
recognizer.delegate = self;
and add this in the "self" class:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
This allows me to have gestures recognized on the UIWebView, and the UIWebView will still respond to the taps. (which i wanted - you may not)

How do you stop UITapGestureRecognizer from catching EVERY tap?

Hello I have an opengl view and on that I have a tab bar. I'm using a tap recognizer to tap different 3d objects on screen. In the tab bar I have a button but it doesn't work because the tap recognizer catches these taps too. How do I stop this? I've already tried this:
- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ([touch.view isKindOfClass:[UIBarButtonItem class]]) return FALSE;
return TRUE;
}
I think I am somehow comparing wrong classess because when I debug it returns TRUE always.
Or you can just do [singleTap setCancelsTouchesInView:NO]. Example:
UITapGestureRecognizer *singleTap = [
[UITapGestureRecognizer alloc]
initWithTarget: self
action: #selector(yourSelector:)
];
[singleTap setCancelsTouchesInView:NO];
[[self view] addGestureRecognizer: singleTap];
if ([touch.view.superview isKindOfClass:[UIToolbar class]]) return FALSE;
This is how I got it to work. The superview is a UIToolbar, probably UIBarButtonIttem is a view after all.