I see a lot of question about it on web, and particularly on StackOverflow.
I test many given answers but in my case, nothing works.
My class implements the protocol UIGestureRecognizerDelegate:
#interface CDMapViewController : CDViewController <UIGestureRecognizerDelegate>
The following method is wrote from xcode autocompletion in #implentation of my class
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldReceiveTouch:(UITouch *)touch {
NSLog(#"not called");
return NO;
}
I have correctly init the UIGestureRecognizer in first method which correctly call the second, third and fourth methods:
- (void)initGestureOnMap {
UIGestureRecognizer *gestureRecognizer = [[UIGestureRecognizer alloc] init];
gestureRecognizer.delegate = self;
[self.view addGestureRecognizer:gestureRecognizer];
}
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
gesture_dragging = NO;
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
gesture_dragging = YES;
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
if (gesture_dragging || [touches count] != 1) return;
/* bla bla bla */
}
... It doesn't log – not called ... why ?
You need to call super implementations for the touches methods.
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
gesture_dragging = NO;
}
... and so on.
These methods need to be implemented on your view, and not your view controller.
Pick what kind of gesture you want. By itself, UIGestureRecognizer doesn't do much, so pick one like UITapGestureRecognizer. Next, implement your gesture recognizer with the designated initializer.
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(myMethod:)];
Finally, implement myMethod:.
-(void)myMethod:(UITapGestureRecognizer *)recognizer
{
// Whatever this does.
}
Related
So, I have a custom gesture recognizer added to a skmap, but the behavior is not consistent but rather weird. At first, both map and my gesture recognizer respond to gesture events.
But after panning or pinch to rotate & zoom, only gesture recognizer responds to gestures. While map does not respond to any gesture like pinch/panning/tap. This I call it a "locked" state.
In this state, map does not respond to anything. Trying to rotate/pinch will "unlock" it. But trying to pan will not. So for example, the sequence is, I pan the map, it follows. Then I pan the map several times, it does not move at all. Then I try to rotate it, the map stays still. Then I pan it the third time, it can follow my gesture again.
So my question is, what is the difference between this two state, what changes happened to the map when I move/try to rotate it.
My code is very simple, just for test purpose. myGestureRecognizer.m :
#import "myGestureRecognizer.h"
#import <MapKit/MapKit.h>
#implementation myGestureRecogniczer
-(id)init;
{
self = [super init];
self.delegate = self;
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"Touches Began");
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"Touches Moved");
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"Touches Canceled");
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"Touches Ended");
}
#end
And mapViewController.m :
-(void)viewDidLoad
{
[super viewDidLoad];
self.myMapView = [[SKMapView alloc] initWithFrame:CGRectMake( 0.0f, 0.0f, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))];
self.myMapView.delegate = self;
self.myMapView.calloutView.delegate = self;
[self.view addSubview:self.myMapView];
myGestureRecogniczer *gestureRecognizer = [[myGestureRecogniczer alloc] init];
[self.myMapView addGestureRecognizer:gestureRecognizer];
}
I've a question.. I would like to do my own gesture recognizer with swipe and that you drag (or swipe) down with two fingers but I don't now how..
This is my code of GestureSwipe.h:
#import <UIKit/UIKit.h>
#interface GestureSwipe : UIGestureRecognizer
#property CGPoint startTouchPosition;
#property(nonatomic) NSUInteger numberOfTouchesRequired;
#property(nonatomic) UISwipeGestureRecognizerDirection direction;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
#end
This is my code of GestureSwipe.m:
#define VERT_SWIPE_DRAG_MAX 7
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *aTouch = [touches anyObject];
// startTouchPosition is a property
self.startTouchPosition = [aTouch locationInView:self.view];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *aTouch = [touches anyObject];
CGPoint currentTouchPosition = [aTouch locationInView:self.view];
// Check if direction of touch is horizontal and long enough
if (fabsf(self.startTouchPosition.y - currentTouchPosition.y) <=
VERT_SWIPE_DRAG_MAX)
{
// If touch appears to be a swipe
if (self.startTouchPosition.y < currentTouchPosition.y) {
[self myProcessDownSwipe:touches withEvent:event];
}
self.startTouchPosition = CGPointZero;
}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
self.startTouchPosition = CGPointZero;
}
-(void)myProcessDownSwipe:(NSSet *)touches withEvent:(UIEvent *)event {
}
How should I do to do swipe down with two fingers?
Then I have other VC where recognize my own gesture:
HelloViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
//newGesture = [[GestureSwipe alloc] init];
newGesture = [[GestureSwipe alloc] initWithTarget:self action:#selector(showImage:)];
newGesture.numberOfTouchesRequired = 2;
}
In HelloViewController you'd add something like this:
UISwipeGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
// Set the acceptable swipe direction and number of touches required to do the gesture
[recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
recognizer.numberOfTouchesRequired = 2;
// Add the gesture recogizer to the view
[self.view addGestureRecognizer:recognizer];
Then you would create a custom method (I called my one handleSwipe:, but this can be whatever suits you best) and do what you need to do.
EDIT: So your viewDidLoad method would look like this
- (void)viewDidLoad
{
[super viewDidLoad];
UISwipeGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
// Set the acceptable swipe direction and number of touches required to do the gesture
[recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
recognizer.numberOfTouchesRequired = 2;
// Add the gesture recogizer to the view
[self.view addGestureRecognizer:recognizer];
}
And the handleSwipe: method could be like this:
-(void)handleSwipe:(id)sender{
// Do something
}
There is no need to create your own recognizer. You can handle this with a standard recognizer and just state how many touches is required.
A good example can be found here: Capture only UIView 2 finger UIPanGestureRecognizer
I recognize touches in my view - if certain conditions happen I would like to call my subview with this method but it isn't working - (if I don't override hittestWithEvent - this view would have been returned)
How can I do it?
Edit:
I want the original SUBVIEW to get the "tap" so I tried to do this:
-(void)didTap:(MyTapGesture *)tap
{
// the original view that would have been returned by hit test. could be any arbitrary view
UIView *theView = [super hitTest:[tap locationInView:self] withEvent:nil];
NSSet *touchesBegan = tap.touchesBegan;
NSSet *touchesEnded = tap.touchesEnded;
UIEvent *eventBegan = tap.eventBegan;
UIEvent *eventEnd = tap.eventEnd;
NSSet *tbegan = [eventBegan touchesForView:theView];
NSSet *tend = [eventEnd touchesForView:theView];
// try to simulate the touch in the subview - not working
[theView touchesBegan:tbegan withEvent:eventBegan];
[theView touchesEnded:tend withEvent:eventEnd];
}
#interface MyTapGesture : UITapGestureRecognizer
#property (nonatomic,strong) NSSet *touchesBegan;
#property (nonatomic,strong) NSSet *touchesEnded;
#property (nonatomic,strong) UIEvent *eventBegan;
#property (nonatomic,strong) UIEvent *eventEnd;
#end
#implementation MyTapGesture
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
self.touchesBegan = touches;
self.eventBegan = event;
[super touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
self.touchesEnded = touches;
self.eventEnd = event;
[super touchesEnded:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesCancelled:touches withEvent:event];
}
#end
You can overwrite touchesBegan:withEvent on your main view and it the condition happen call it on your subview:
//In your view
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if (CONDITION)
{
//call your subview
[yourSubview touchesBegan:touches withEvent:event];
}
}
Hope this is what you are after.
//Extended
You can try add a method to your subview which is called when the condition is meet:
//In your subview
-(void)myMethod:(NSSet *)touches
{
//Your code
}
And call it in your view:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if (CONDITION)
{
//call your subview
[yourSubview myMethod:touches];
}
}
I have been trying to figure out how to forward the touches from a UIScrollView to its superview. The reason is for sort of a cursor to follow the touch. Anyway, the scrollview is populated with UIButtons. My code for forwarding the touch is to use delegation in a scrollview subclass:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesBegan:touches withEvent:event];
if ([[self tfDelegate]respondsToSelector:#selector(tfDelegateBegan:)]) {
[[self tfDelegate]tfDelegateBegan:[touches anyObject]];
}
NSLog(#"touches began");
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesMoved:touches withEvent:event];
if ([[self tfDelegate]respondsToSelector:#selector(tfDelegateMoved:)]) {
[[self tfDelegate]tfDelegateMoved:[touches anyObject]];
}
NSLog(#"touches moved");
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesEnded:touches withEvent:event];
if ([[self tfDelegate]respondsToSelector:#selector(tfDelegateEnded:)]) {
[[self tfDelegate]tfDelegateEnded:[touches anyObject]];
}
NSLog(#"touches ended");
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesCancelled:touches withEvent:event];
if ([[self tfDelegate]respondsToSelector:#selector(tfDelegateCancelled:)]) {
[[self tfDelegate]tfDelegateCancelled:[touches anyObject]];
}
NSLog(#"touches cancelled");
}
However, I have learned that UIScrollviews operate via UIGestureRecognizers, so these methods aren't even called by default. I realize that the gesture recognizers are exposed in iOS 5 but I need to support 4.0 as well. I did this instead:
NSArray *a = [[theScrollview gestureRecognizers]retain];
for (UIGestureRecognizer *rec in a) {
if ([rec isKindOfClass:[UIPanGestureRecognizer class]]) {
NSLog(#"this is the pan gesture");
rec.cancelsTouchesInView = NO;
}
}
This allows the gesture to work and the touches methods to be called simultaneously. The issue is, now if you try to scroll while touching a button, the button can be pressed while scrolling. Normally, the scroll cancels the button and the only time a button can be pressed is if the scrollview is not scrolling.
This is the desired functionality. Any suggestions on how I might achieve this?
Maybe try controlling the button action using a flag that would prevent the event from firing if the scroll view is scrolling.
BOOL isScrolling = NO;
- (void) scrollViewWillBeginDragging:(UIScrollView *)scrollView {
isScrolling = YES;
}
- (void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
isScrolling = NO;
}
- (void) didTapButton {
if(isScrolling)
return;
//Do Button Stuff
}
How do we detect a tap & hold on a UITableViewCell?
In iOS 3.2 or later you can use UILongPressGestureRecognizer
Here's the code lifted straight from my app. You should add these methods (and a boolean _cancelTouches member) to a class you derive from UITableViewCell.
-(void) tapNHoldFired {
self->_cancelTouches = YES;
// DO WHATEVER YOU LIKE HERE!!!
}
-(void) cancelTapNHold {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:#selector(tapNHoldFired) object:nil];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
self->_cancelTouches = NO;
[super touchesBegan:touches withEvent:event];
[self performSelector:#selector(tapNHoldFired) withObject:nil afterDelay:.7];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[self cancelTapNHold];
if (self->_cancelTouches)
return;
[super touchesEnded:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
[self cancelTapNHold];
[super touchesMoved:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[self cancelTapNHold];
[super touchesCancelled:touches withEvent:event];
}
//Add gesture to a method where the view is being created. In this example long tap is added to tile (a subclass of UIView):
// Add long tap for the main tiles
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longTap:)];
[tile addGestureRecognizer:longPressGesture];
[longPressGesture release];
-(void) longTap:(UILongPressGestureRecognizer *)gestureRecognizer{
NSLog(#"gestureRecognizer= %#",gestureRecognizer);
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
NSLog(#"longTap began");
}
}
You should probably handle the UIControlTouchDown event and depending on what you mean by "hold", fire a NSTimer that will count an interval since you initiated the touch and invalidate upon firing or releasing the touch (UIControlTouchUpInside and UIControlTouchUpOutside events). When the timer fires, you have your "tap & hold" detected.