How to Implement the Uitapgesturerecognizer into My Application

Swift3 iOS - How to make UITapGestureRecognizer trigger function

From your code you are using swift 3.0 so change your selector syntax like this

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.tapBlurButton(_:)))

and Your function like this

func tapBlurButton(_ sender: UITapGestureRecognizer) {
print("Please Help!")
}

Edit:

Not idea that you are using button with tap gesture, instead of that use inbuilt method addTarget for button no need to create tap gesture for it like this

qsBlurButton.addTarget(self, action: #selector(self.tapBlurButton(_:)), forControlEvents: .TouchUpInside)

func tapBlurButton(_ sender: UIButton) {
print("Please Help!")
}

iOS - UITapGestureRecognizer - Selector with Arguments

You're almost there. UIGestureRecognizer has a view property. If you allocate and attach a gesture recognizer to each image view - just as it appears you do in the code snippet - then your gesture code (on the target) can look like this:

- (void) imageTapped:(UITapGestureRecognizer *)gr {

UIImageView *theTappedImageView = (UIImageView *)gr.view;
}

What's less clear from the code you provided is how you associate your Plant model object with it's corresponding imageView, but it could be something like this:

NSArray *myPlants;

for (i=0; i Plant *myPlant = [myPlants objectAtIndex:i];
UIImage *image = [UIImage imageNamed:myPlant.imageName]; // or however you get an image from a plant
UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; // set frame, etc.

// important bit here...
imageView.tag = i + 32;

[self.view addSubview:imageView];
}

Now the gr code can do this:

- (void) imageTapped:(UITapGestureRecognizer *)gr {

UIImageView *theTappedImageView = (UIImageView *)gr.view;
NSInteger tag = theTappedImageView.tag;
Plant *myPlant = [myPlants objectAtIndex:tag-32];
}

table view with UITapGestureRecognizer

You should implement UIGestureRecognizerDelegate and add the following:

//inside view did load

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(dismissKeyboard)];
tap.delegate = self;
[self.view addGestureRecognizer:tap];

// UIGestureRecognizerDelegate methods

#pragma mark UIGestureRecognizerDelegate methods

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ([touch.view isDescendantOfView:yourTableView]) {

// Don't let selections of auto-complete entries fire the
// gesture recognizer
return NO;
}

return YES;
}

UITapGestureRecognizer swallows all gestures

Try setting the view controller to be a delegate of UIGestureRecognizerDelegate, and implement gestureRecognizer:shouldRecognizeSimulaneouslyWithGestureRecgonizer to always return 'YES'. Example:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}


Related Topics



Leave a reply



Submit