Uilongpressgesturerecognizer Not Calling Its Target Method

UILongPressGestureRecognizer not calling its target method

//the viewcontroller is initiated with UIGestureRecognizerDelegate
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPress))

It looks like you are trying to make longPressGesture an instance property of your UIViewController while giving it a target and action as part of its initializer. That's not going to work, because at the time it is initialized, the target, self, is not the instance. There is no instance yet; the instance is what we are in the middle of creating!

Instead, move that line into cellForRowAt:, like this:

//in cellForRowAt:
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPress))
longPressGesture.minimumPressDuration = 1.0
longPressGesture.delegate = self
longPressGesture.cancelsTouchesInView = false
cell.addGestureRecognizer(longPressGesture)

UILongPressGestureRecognizer: action method not being called

You need to change this line :

let recognizer =  UILongPressGestureRecognizer(target: mapController, action: "action:")

to

let recognizer =  UILongPressGestureRecognizer(target: self, action: "action:")

And it will work. As your method is in self ( Annotation class) itself.

UILongPressGestureRecognizer does not do anything

You just need to create custom button using UIView. Add a long press gesture to that view and upon required time triggered the delegate/Closures.

func addLongPressGesture() {
let recognizer = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(_:)))
recognizer.minimumPressDuration = 3.0 // Duration
customButton.addGestureRecognizer(recognizer)
}

@objc
func handleLongPress(_ gestureRecognizer: UILongPressGestureRecognizer) {
if gestureRecognizer.state == .began {
// Perform your functionality here
}
}

how UILongPressGestureRecognizer works

you have to use this code to intialized gesture

  UILongPressGestureRecognizer *gesture1 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(celllongpressed:)];
[gesture1 setDelegate:self];
[gesture1 setMinimumPressDuration:1];
[self addGestureRecognizer:gesture1];

and to target method use this

-(void)celllongpressed:(UIGestureRecognizer *)longPress
{
}

UILongPressGestureRecognizer issue when adding to MKMapView

You just need to add a colon after addAnotation:

var uilpgr = UILongPressGestureRecognizer(target: self, action: "addAnotation:")

In your version where you omit the colon, a method with this signature will be called. Note there are no parameters

func addAnotation()

So your UILongPressGestureRecognizer is attempting to a call the above method, which is undefined and that is causing your application to throw an exception

UILongPressGestureRecognizer will not respond to touch & HOLD

The behavior you describe, where your gesture recognizer does not receive further calls to your handler when you're not moving is the standard behavior. The state property for these gestures as you move are of type UIGestureRecognizerStateChanged, so it makes sense that if things haven't changed, your handler won't be called.

You could

  • Upon call to your gesture recognizer with state of UIGestureRecognizerStateBegan start a repeating timer;
  • Upon call to your gesture recognizer with state of UIGestureRecognizerStateCancelled, UIGestureRecognizerStateFailed, or UIGestureRecognizerStateEnded then invalidate and release the timer;
  • Make sure the gesture recognizer method is saving whatever value you're looking for in some class property (e.g. the value of locationInView or whatever)

So, maybe something like:

@interface ViewController ()

@property (nonatomic) CGPoint location;
@property (nonatomic, strong) NSTimer *timer;

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];

UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
gesture.minimumPressDuration = 0.1;
gesture.allowableMovement = 600;
[self.view addGestureRecognizer:gesture];
}

- (void)handleTimer:(NSTimer *)timer
{
[self someMethod:self.location];
}

- (void)handleGesture:(UIGestureRecognizer *)gesture
{
self.location = [gesture locationInView:self.view];

if (gesture.state == UIGestureRecognizerStateBegan)
{
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(handleTimer:) userInfo:nil repeats:YES];
}
else if (gesture.state == UIGestureRecognizerStateCancelled ||
gesture.state == UIGestureRecognizerStateFailed ||
gesture.state == UIGestureRecognizerStateEnded)
{
[self.timer invalidate];
self.timer = nil;
}

[self someMethod:self.location];
}

- (void)someMethod:(CGPoint)location
{
// move whatever you wanted to do in the gesture handler here.

NSLog(@"%s", __FUNCTION__);
}

@end

UILongPressGestureRecognizer on UIButton not working

You should have three instance of UILongPressGestureRecognizer.

Before add a gesture recognizer to a new view, the addGestureRecognizer method will remove the gesture recognizer from the view it has been attached to.



Related Topics



Leave a reply



Submit