In my app I need have a swipe gesture recogniser on my background scroller for the up direction. Here is my code below
It is in viewDidLoad
UISwipeGestureRecognizer *Swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(SwipeRecognizer:)];
Swipe.direction = UISwipeGestureRecognizerDirectionUp;
[backgroundScroller addGestureRecognizer:Swipe];
and it is the SwipeRecognizer:
- (void) SwipeRecognizer:(UISwipeGestureRecognizer *)sender {
if (sender.direction | UISwipeGestureRecognizerDirectionUp){
NSLog(#" *** SWIPE UP ***");
}
}
The problem is I cant enable scrolling and capture the gesture simultaneously. when I said scrolling is not enabled, I can recognise the gesture. But I need to to scrolling and gesture recognition simultaneously. isn't it possible?
Override the gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: method to don't block the UIScrollViews Pan recognizer
And it will work…
Dont forget to add delegate to self for gesture recogniser. As mentioned in #death7eater's comment.
I solved my problem like that:
This is for the viewDidLoad:
UISwipeGestureRecognizer *Swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(SwipeRecognizer:)];
Swipe.direction = UISwipeGestureRecognizerDirectionUp;
[backgroundScroller addGestureRecognizer:Swipe];
Swipe.delegate = self;
This is the SwipeRecognizer method:
- (void) SwipeRecognizer:(UISwipeGestureRecognizer *)sender {
if (sender.direction | UISwipeGestureRecognizerDirectionUp){
NSLog(#" *** SWIPE UP ***");
}
}
And thanks to #lukaswelte this allows to perform multiple gestures simultaneously:
- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
Related
In viewDidLoad I set:
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
[swipeGesture setDirection:(UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight)];
[self.view addGestureRecognizer:swipeGesture];
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
NSLog(#"Swipe received.");
UISwipeGestureRecognizerDirection temp = recognizer.direction;
if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft)
{
[self backCalendarPressed:nil];
}
else if (recognizer.direction == UISwipeGestureRecognizerDirectionRight)
{
[self nextCalendarPressed:nil];
}
}
but recognizer.direction is always equal to '3'.
And that's why I can't determine if it's left or right swipe.
you have to set a separate gesture recognizer for each direction if you want to discern between left and right gestures. the direction property only gives you what you set as allowed direction (3 = both directions).
You can give the same target method to both, and in that method ask for the direction of the recognizer as you are doing.
You should try this. It is working fine.
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(backCalendarPressed:)];
[swipeLeft setDirection: UISwipeGestureRecognizerDirectionLeft ];
[self.view addGestureRecognizer:swipeLeft];
swipeLeft=nil;
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(nextCalendarPressed:)];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
[self.view addGestureRecognizer:swipeRight];
Basically you cannot set multiple directions to a single Swipe gesture recognizer. Use separate gesture recognizers for Left and Right directions.
try this:
The direction property only defines the allowed directions that are recognized as swipes, not the actual direction of a particular swipe.
The easiest would be to use two separate gesture recognizers instead.
If you want to capture swipes left and right that you can differentiate between, you'll have to set up a separate recognizer for each. Apple does this in their Simple Gesture Recognizers
UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFromLeft:)];
[swipeGestureLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[self.view addGestureRecognizer:swipeGestureLeft];
UISwipeGestureRecognizer *swipeGestureRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFromRight:)];
[swipeGestureRight setDirection:UISwipeGestureRecognizerDirectionRight];
[self.view addGestureRecognizer:swipeGestureRight];
-(void)handleSwipeFromLeft:(UISwipeGestureRecognizer *)recognizer {
[self backCalendarPressed:nil];
}
-(void)handleSwipeFromRight:(UISwipeGestureRecognizer *)recognizer {
[self nextCalendarPressed:nil];
}
You should implement all the UIGestureRecognizer states in this handler method. Thus you can handle all the cases. I am stating this because I suspect the gesture recognizer has not yet properly recognized it's gesture. Try looking for the direction in the UIGestureRecognizerStateChanged case.
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.
I am trying ebook reading app. In that I want create UIActionSheet by clicking on the index page link..For that how can I identify the touch event in webView as WebView already have the inbuilt longTouch event. Is there any way to identify that longtouch event in webView?
Just overwrite below method to work our gerstures
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer
*)otherGestureRecognizer
{
return YES;
}
For adding longPress add to your view
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPress:)];
longPress.minimumPressDuration=0.8;
longPress.delegate = self;
[self addGestureRecognizer:longPress];
[longPress release];
- (void)longPress:(UILongPressGestureRecognizer*)gesture
{
}
I'm trying to implement touch event similar to a Touch Up Inside a UIButton. I've seen some codes using touchesBegan:withEvent but it looks I need to slide a bit my finger over the screen. I actually just want to touch in the image
Code:
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
NSLog(#"Image touched!");
}
Regards!
You could use the gesture to get the touch on UIImageView.
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(ClickEventOnImage:)];
[tapRecognizer setNumberOfTouchesRequired:2];
[tapRecognizer setDelegate:self];
//Don't forget to set the userInteractionEnabled to YES, by default It's NO.
myImageView.userInteractionEnabled = YES;
[myImageView addGestureRecognizer:tapRecognizer];
Implement the ClickEventOnImage function.
-(void) ClickEventOnImage:(id) sender
{
}
UIImageView is notorious regarding this as its userInteractionEnabled is set to NO by default. You will have to enable it by doing
imageView.userInteractionEnabled = YES;
And then you can use gesture recognizers or the normal touches* methods to handle touches.
I have added two gesture recognizers to a view. One will handle the view drag and the other the double tap. Something like
UITapGestureRecognizer *doubleTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(deleteThisView)];
[doubleTap setDelegate:self];
[doubleTap setCancelsTouchesInView:YES];
[doubleTap setNumberOfTapsRequired:1];
[base addGestureRecognizer:doubleTap];
[doubleTap release];
UIPanGestureRecognizer *panGesture = nil;
panGesture = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:#selector(drag:)];
[panGesture setMaximumNumberOfTouches:1];
[panGesture setDelegate:self];
[base addGestureRecognizer:panGesture];
[panGesture release];
The problem is this: as the view can move, the double tap is somehow difficult to obtain, because the view can slide one hair to one side or the other and iOS will not recognize it as double tap, and instead will see it as two moves and the drag method will run twice.
I am not seeing how this can be done. Any clues?
thanks
There's a delegate method that will tell the central gesture code that two recognizers might be going at the same time:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
if (gestureRecognizer==_panRecognizer && otherGestureRecognizer==_swipeRecognizer)
return YES;
if (gestureRecognizer==_swipeRecognizer && otherGestureRecognizer==_panRecognizer)
return YES;
return NO;
}
Maybe you can try something like that.
Here is Vagrant's answer in swift:
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
if gestureRecognizer == panRecognizer && otherGestureRecognizer == swipeRecognizer {
return true
}
if gestureRecognizer == swipeRecognizer && otherGestureRecognizer == panRecognizer {
return true
}
return false
}
Don't forget to implement the delegate protocol and set your gestures' delegates to self (I forgot this initially).