When my hitView comes partially from the superview after a UIPanGestureRecognizer, the UILongPressGestureRecognizer don't work. Why?
- (id)initWithFrame:(CGRect)frame
{
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(pan:)];
[panGesture setMaximumNumberOfTouches:1];
[panGesture setDelegate:self];
[_glassesImage addGestureRecognizer:panGesture];
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(enchance:)];
longPressGesture.minimumPressDuration = 0.2;
[_glassesImage addGestureRecognizer:longPressGesture];
[self addSubview:_glassesImage];
}
- (void)enchance:(UILongPressGestureRecognizer *)gestureRecognizer
{
UIView *hitView = [gestureRecognizer view];
hitView.alpha=0.6;
inLongPress=YES;
gestureRecognizer.allowableMovement = 200;
if ([gestureRecognizer state] == UIGestureRecognizerStateEnded){
hitView.alpha=1.0;
inLongPress=NO;
}
}
Try defining a delegate for your gesture recognizers and then provide an implementation for:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
(refs)
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
Add delegate to longPressGesture.
longPressGesture.delegate = self;
I think it will be helpful to you.
Related
I am adding a UIImageView as a subview to a UIScrollView then i set the image.
Im trying to to use UITapGestureRecognizer.
The selector of the UITapGestureRecognizer is never called in iOS 5 (in iOS 6 it DOES!).
Tried many variations. this is my code:
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(index*(IMAGE_WIDTH+10), 10.0, IMAGE_WIDTH, IMAGE_HEIGHT)];
[imgView setImageWithURL:[NSURL URLWithString:meal.RecommendedImageURL] placeholderImage:[UIImage imageNamed:#""]];
imgView.layer.cornerRadius = 4;
[imgView.layer setMasksToBounds:YES];
[imgView setUserInteractionEnabled:YES];
[imgView setMultipleTouchEnabled:YES];
[scrollView addSubview:imgView];
if (IOS_NEWER_OR_EQUAL_TO_5)
{
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)];
[imgView addGestureRecognizer:tapGestureRecognizer];
tapGestureRecognizer.numberOfTapsRequired = 1;
tapGestureRecognizer.enabled = YES;
tapGestureRecognizer.delegate = self;
[tapGestureRecognizer setCancelsTouchesInView:NO];
}
this is my selector which is only called in iOS5:
- (void) imageTapped: (UITapGestureRecognizer *)recognizer
{
//Code to handle the gesture
UIImageView *tappedImageView = (UIImageView*)recognizer.view;
GGFullscreenImageViewController *vc = [[GGFullscreenImageViewController alloc] init];
vc.liftedImageView = tappedImageView;
vc.liftedImageView.contentMode = UIViewContentModeScaleAspectFit;
if (IOS_NEWER_OR_EQUAL_TO_5) {
[self.parentViewController presentViewController:vc animated:YES completion:nil];
}
else
{
[self.parentViewController presentModalViewController:vc animated:YES];
}
}
In addition to that, i tried to setCancelsTouchesInView to YES in my UIScrollView but it doesn't work either.
Thanks for your help!
try to add Gesture in Scrollview then check iskind of class image view
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)];
[scrollview addGestureRecognizer:tapGestureRecognizer];
tapGestureRecognizer.numberOfTapsRequired = 1;
tapGestureRecognizer.enabled = YES;
tapGestureRecognizer.delegate = self;
[tapGestureRecognizer setCancelsTouchesInView:NO];
- (BOOL)gestureRecognizer:(UITapGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
// test if our control subview is on-screen
if ([touch.view isKindOfClass:[UIImageView class]]) {
// we touched a button, slider, or other UIControl
return YES;
}return NO;
}
Implement the 3 methods of UIGestureRecognizerDelegate & return default values:
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;
Ok i solved it very EASILY!
the problem was setting the delegate of the UITapGestureRecognizer to self.
when i removed the delegate = self, it started working :)
Thanks for your assistance
The following code doesn't work on iOS 5 and iOS 5.1 (but works on iOS 6):
- (void)viewDidLoad {
...
UILongPressGestureRecognizer* gesture = [[[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(handleLongPress:)] autorelease];
[myWebView addGestureRecognizer:gesture];
}
- (void)handleLongPress:(UIGestureRecognizer*)gestureRecognizer {
...
}
How to fix the issue? Thanks a lot for the help!
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;
}
try this code..
UILongPressGestureRecognizer *longpressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPressHandler:)];
longpressGesture.minimumPressDuration = 5;
[longpressGesture setDelegate:self];
[myWebView addGestureRecognizer:longpressGesture];
[longpressGesture release];
- (void)longPressHandler:(UILongPressGestureRecognizer *)gestureRecognizer {
NSLog(#"longPressHandler");
}
I want the user to be able to pan from each thumb simultaneously but I can't figure out how to detect it with uigesturerecognizer. I can detect a tap and a pan simultaneously no problem.
It appears that the second pan will block the first.
Any help is appreciated.
I solved it by defining shouldReceiveTouch like so:
-(BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if (gestureRecognizer == singleTap) {
return YES;
}
if (gestureRecognizer == pan1 && [touch locationInView:self].x > 160) {
return YES;
}
if (gestureRecognizer == pan2 && [touch locationInView:self].x <= 160) {
return YES;
}
return FALSE;
}
And initWithFrame has the following code:
self.userInteractionEnabled = YES;
singleTap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(handleSingleTap:)];
singleTap.numberOfTapsRequired = 1;
[self addGestureRecognizer:singleTap];
[singleTap release];
NSLog(#"tap: %p", singleTap);
pan1 = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:#selector(handlePan1:)];
[self addGestureRecognizer:pan1];
NSLog(#"pan1: %p", pan1);
pan2 = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:#selector(handlePan2:)];
[self addGestureRecognizer:pan2];
for (UIGestureRecognizer *recognizer in self.gestureRecognizers) {
recognizer.delegate = self;
}
NSLog(#"pan2: %p", pan2);
I've made a custom gesture recognizer and im adding it to my customView .
The custom gesture is a subclass of UIPanGestureRecognizer .
The other gesture that im adding is LongPressGestureRecognizer
CustomGestureRecognizer *pan;
pan = [[CustomGestureRecognizer alloc] initWithTarget:[self viewController] action:#selector(dragImage:)];
[pan setDirection:DirectionPangestureRecognizerVertical];
[pan setMinimumNumberOfTouches:1];
[pan setMaximumNumberOfTouches:1];
[custom addSubview:custom.imageView];
[custom addGestureRecognizer:pan];
[pan release];
UILongPressGestureRecognizer *highLight = [[UILongPressGestureRecognizer alloc] initWithTarget:[self viewController]
action:#selector(highlightImage:)];
[highLight setDelaysTouchesBegan:0.1];
[tempView addGestureRecognizer:highLight];
[highLight release];
Also i have implemented
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
if (![gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]] && ![otherGestureRecognizer isKindOfClass:[UITapGestureRecognizer class]])
{
return YES;
}
return YES;
}
Both im my [self viewController] and in the self class but im still not getting both the gesture to work simultaniiosly .
You forgot to set the delegates of your gesture recognizers...that method will never be called.
In iOS, is it possible to put a tap recognizer on a UIWebView, so that when someone single-taps the web view, an action gets performed?
Code below doesn't seem fire handleTap when I tap my webView.
Thanks.
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(handleTap)];
tap.numberOfTapsRequired = 1;
[webView addGestureRecognizer:tap];
[tap release];
-(void) handleTap {
NSLog(#"tap");
}
I found that I had to implement this method to get it to work:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
Your UIViewController subclass should implement UIGestureRecognizerDelegate and return YES from gestureRecognizer: shouldReceiveTouch: when appropriate.
Then you can assign it to the delegate property of your UIGestureRecognizer.
tap.delegate = self;
Add UIGestureRecognizerDelegate to view controller
<UIGestureRecognizerDelegate>
Add Tap Gesture to Webview.
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(flip)];
tap.numberOfTapsRequired = 1;
tap.delegate = self;
[_webview addGestureRecognizer:tap];
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}