UILabel UILongPressGestureRecognizer not working? - iphone

How can I get the UILongPressGestureRecognizer on uilabel.when i implement the following code it doesn't call the function. So please tell me what wrong i did ?
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(LabelLongPressed:)];
longPress.minimumPressDuration = 0.5; // Seconds
longPress.numberOfTapsRequired = 0;
[objlblDuplicate addGestureRecognizer:longPress];
[longPress release];

By default UILabel is not able to get touch events.
objlblDuplicate.userInteractionEnabled = YES;

Related

Adding Alertview with coordinates of the Pressed place in Mapview

Im Having MapView.. I added this as a subview of ViewController's View. I have the following Code in ViewDidLoad:
[self.view addSubview:mapView];
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(mapLongPress:)];
longPressGesture.minimumPressDuration = 2;
[mapView addGestureRecognizer:longPressGesture];
[longPressGesture release];
And ,
- (void)mapLongPress:(UILongPressGestureRecognizer *)gestureRecognizer{
NSLog(#"Gesture");
if(gestureRecognizer.state == UIGestureRecognizerStateBegan){
CGPoint touchLocation = [gestureRecognizer locationInView:mapView];
CLLocationCoordinate2D coordinate;
coordinate = [mapView convertPoint:touchLocation toCoordinateFromView:mapView];
These i Got from StackOverFlow.. But Its not Working.. Did i need to do anything more in that?
Try adding [self.view addSubview:mapView]; after [mapView addGestureRecognizer:longPressGesture];
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(mapLongPress:)];
longPressGesture.minimumPressDuration = 2;
longPressGesture.delegate = self;
[self.mapView addGestureRecognizer:longPressGesture];
[self.view addSubview:mapView];
[longPressGesture release];
Just add the UIGestureRecognizerDelegate to ViewController and do the above code given by me!!!
Try this:
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:#selector(mapLongPress:)];
lpgr.minimumPressDuration = 2.0;
[self.mapView addGestureRecognizer:lpgr];
This is using ARC, so I am not sure if you should release the gesture at this moment right now. Try it without releasing it, then try it with releasing it. See if that affects the gesture.

UIWebview Gesture iOS

How can I identify the user touch, tap & double tap in the UIWebview. Is there any delegates available like touches begin etc?
here is the code to implement the single tap and double tap on webview
UITapGestureRecognizer *singleTap = [[[UITapGestureRecognizer alloc] initWithTarget: self action:#selector(doSingleTap)] autorelease];
singleTap.numberOfTapsRequired = 1;
[self.myWebView addGestureRecognizer:singleTap];
UITapGestureRecognizer *doubleTap = [[[UITapGestureRecognizer alloc] initWithTarget: self action:#selector(doDoubleTap)] autorelease];
doubleTap.numberOfTapsRequired = 2;
[self.myWebView addGestureRecognizer:doubleTap];
The below 3 links will solve your problem
how-to-add-a-gesture-recognizer-to-a-uiwebview-subclass
iphone-custom-gestures-on-your-uiwebview
does-uigesturerecognizer-work-on-a-uiwebview
About number of taps, you can set it through the property numberOfTapsRequired
Eg:
UITapGestureRecognizer *myGusture = [[[UITapGestureRecognizer alloc] initWithTarget: self action:#selector(doTap)] autorelease];
myGusture.numberOfTapsRequired = 1; //you can change it to any number as per your requirement.
[self.myWebView addGestureRecognizer:myGusture];
[myGusture release];
myGusture = nil;

detect Swipe gesture in UIWebview

I am new to iPhone developer,
I made epub reader and loaded each page of epub in my webview
What i want to is, when user does right swipe gesture 2nd time then i want to navigate to new page, i do not want to do anything when user does right swipe gesture for first time.
UISwipeGestureRecognizer *swipeRight
Is there any method something like,
if(swipeRight.touch.count > 2)
{
// do this
}
Any help will be appriciated.
Thanks In Advance !
EDIT
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
float scrollViewHeight = scrollView.frame.size.height;
float scrollContentSizeHeight = scrollView.contentSize.height;
float scrollOffset = scrollView.contentOffset.y;
if (scrollOffset == 0)
{
swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeLeftAction:)];
swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
swipeUp.numberOfTouchesRequired=2;
swipeUp.delegate = (id<UIGestureRecognizerDelegate>)self;
swipeUp.cancelsTouchesInView=YES;
[_webview addGestureRecognizer:swipeUp];
}
else if (scrollOffset + scrollViewHeight == scrollContentSizeHeight)
{
swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeRightAction:)];
swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
swipeDown.numberOfTouchesRequired=2;
swipeDown.delegate = (id<UIGestureRecognizerDelegate>)self;
swipeDown.cancelsTouchesInView=YES;
[_webview addGestureRecognizer:swipeDown];
}
You can tell the UIWebView's UIScrollView that its UIPanGestureRecognizer should only fire when your own UISwipeGestureRecognizer has failed.
This is how you do it:
UISwipeGestureRecognizer *rightSwipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeGesture:)];
UISwipeGestureRecognizer *leftSwipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeGesture:)];
rightSwipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
leftSwipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:rightSwipeGesture];
[self.view addGestureRecognizer:leftSwipeGesture];
[_webView.scrollView.panGestureRecognizer requireGestureRecognizerToFail:rightSwipeGesture];
[_webView.scrollView.panGestureRecognizer requireGestureRecognizerToFail:leftSwipeGesture];
That should do the trick for you.
Just attach UIGestureRecognizer subclass to that view and hold on for calls...
UISwipeGestureRecognizer* rightSwipeRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(someAction)];
rightSwipeRecognizer.numberOfTouchesRequired = 2;
rightSwipeRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
rightSwipeRecognizer.cancelsTouchesInView = YES;
[self.webView addGestureRecognizer:rightSwipeRecognizer]; // add in your webviewrightSwipeRecognizer
Try like below it will help you
UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(rightSwipeHandle:)];
rightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
rightRecognizer.numberOfTouchesRequired = 2;
[self.view addGestureRecognizer:rightRecognizer];
[rightRecognizer release];
I don't think that swipe gestures offer support for the kind of behavior you are aiming at, but you can easily accomplish it by doing the following:
on the first swipe, set a flag and start a timer; for the rest do nothing;
on the second swipe,
a. if the timer has fired (when firing, the timer reset the flag), do as per point 1.
b. is the timer has not fired (the flag is still set), then do you action and cancel the timer.
You might event think of defining a subclass of UISwipeGestureRecognizer to encapsulate all this behavior.

Two UITapGestureRecognizer in on UIView

I want to add to my UIViewController :
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:tapGesture];
[tapGesture release];
UITapGestureRecognizer *tapGesture2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGesture2:)];
tapGesture2.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:tapGesture2];
[tapGesture2 release];
the problem is if the user tap twice the two methods are called,and i want that if the user make double tap only the first(handleTapGesture) will be called and if he make one tap it will call only the second one(handleTapGesture2)
use this one..
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGesture)];
tapGesture.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:tapGesture];
[tapGesture release];
UITapGestureRecognizer *tapGesture2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGesture2)];
tapGesture2.numberOfTapsRequired = 1;
[tapGesture2 requireGestureRecognizerToFail: tapGesture];
[self.view addGestureRecognizer:tapGesture2];
[tapGesture2 release];
you can use the code i posted on here
in this the method requireGestureRecognizerToFail: is used in viewcontroller.m this will solve your problem

UILongPressGestureRecognizer of Superview gets called instead of Subview

i got a strange problem. I got a Superview with customcontrols as subviews.
Their are GestureRecognizer both in superview and subviews.
If i tap a subview its GestureRecognizer gets called and on tap on the superview its tap gets called.
but on long press in the subview SOMETIMES the GestureRecognizer of the superview gets called. I add the GestureRecognizers in the same functions but there is a different attitude.
Superview
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapPiece:)];
tapGesture.numberOfTapsRequired = 1;
//contenView is the area where my controls can be
[self.contentView addGestureRecognizer:tapGesture];
[tapGesture release];
UILongPressGestureRecognizer *longTapGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longTapPiece:)];
tapGesture.numberOfTapsRequired = 1;
[self.contentView addGestureRecognizer:longTapGesture];
[longTapGesture release];
Subviews
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(selectPiece:)];
tapGesture.numberOfTapsRequired = 1;
[self addGestureRecognizer:tapGesture];
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPiece:)];
longPressGesture.numberOfTapsRequired = 1;
[self addGestureRecognizer:longPressGesture];
Can anyone tell me why the longtap doen't response to my subview and how to fix it.
THANK YOU
got a solution but this is not what i wanted i set the duration of the control lower than the one of the superview [longPressGesture setMinimumPressDuration:0.4]; But the gestureRecognizer should be independend