iOS: How to write handler for Tap Gesture Recognizer if dragged in from storyboard? - ios

I have an image and in the storyboard I've dragged a Tap Gesture Recognizer on top of this image and changed the settings:
For my TGR:
For my image:
How do I hook this up to my controller now? I want a method to fire off when the double tap happens. Is there some sort of protocol I have to conform to? Do people normally do this from the storyboard or programmatically in viewDidLoad? I don't mind doing it another way if that's the general trend of things.

connect the gesture recognizer as a outlet to your ViewController and in your viewDidLoad :
[self.yourGesture addTarget:self action:#selector(didTapOnImage:)] ;
and then declare your method didTapOnImage method :
-(void)didTapOnImage:(UIGestureRecognizer*) recognizer
{
//do your work here
}

Related

Get reference of gesture recognizer in xib not using IBOutlet

I create a single tap and double tap gesture recognizer in a xib file, and I want to access it in a category, but the Referring Outlet of xib in property and it cannot be inserted in a category, can I use code to access gesture recognizer reference in function viewDidLoad?
Yes you can. A view keeps a list of its Gesture Recognizers so you just need to loop through them.
for (UIGestureRecognizer *recognizer in self.gestureRecognizers)
{
// You need a way to identify it here
}
In this case, if your category is extending a view controller, you just need to know which view contains the gesture recognizer you want.
#property(nonatomic, copy) NSArray<__kindof UIGestureRecognizer *> *gestureRecognizers;
https://developer.apple.com/documentation/uikit/uiview/1622542-gesturerecognizers?language=objc
Edit:
If your category is extending a ViewController change the loop to self.view.gestureRecognizers (or any other view that you attached it to)

Can I drag imageView gestureRecongnizer to .m file as Action?

First of all, I have open the image's User Interaction Enabled.
But why I can't drag the gesture ?
The gestureRecognizers in Outlet Collections is a outlet to the gesture recognizers of that view, not an action of its gesture recognizers.
What you need to do is adding a gesture recognizer to the image view by dragging one from the Object Library and adding its action
You can drag the gesture like this:
Drag a TapGesture Recognizer to the ViewController in storyboard:
You should write a IBAction method in the ViewController.m:
```
-(IBAction)tapGesture:(id)sender {
}
```
Drag the TapGesture Recognizer to ViewController, then choose the delegate and the tapGesture: method:
Then you can drag the imageView's gestureRecognizers to the gesture:
The function you write in ViewController.m is what you want.

Single tap gesture to multiple View using storyboard

How to have single tap gesture to multiple Views using storyboard.
I drag 3 views and one tap gesture into UIView class.
Contacted three view to tap gesture and added action class handleGesture Method on tap on any one the tree view it should trigger the method action.
using story board.
But i want to do it with single tap gesture is it possible or not.
try with this, define a variable UITapGestureRecognizer with your method, after that, in a method or wherever you want, you can add this gesture to your multiple views
var recognizerMovements: UITapGestureRecognizer {
get { UITapGestureRecognizer(target: self, action: #selector(self.myActionMethod)) }
}
self.myFirstView.addGestureRecognizer(myActionMethod)
self.mySecondView.addGestureRecognizer(myActionMethod)

Swipe gesture between view controllers in storyboards

I am looking to add a left and right swipe gesture to change between view controllers, is this possible and is there an easy way to do this in storyboards? Thanks :)
Storyboards allow you to set up segues between two view controllers. I would say start by attaching segues between the views, give it an identifier.
Then use something like
UISwipeGestureRecognizer * recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(myRightAction)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[self addGestureRecognizer:recognizer];
to add a swipe recognizer to a view.
Then in the myRightAction method you'd want to do your segue call like
[self performSegueWithIdentifier: #"MySegue" sender: self];
to go to a view or dismiss it to leave the view.
You can use Navigation View Controllers to have a right and left transition animation. You can also make custom segues.
on Xcode 5 it is simpler, less code needed:
storyboard -> utility panel -> object Library -> drag and drop the Swipe Gesture Recognizer inside your scene
you MUST drag it into the view itself(where the background color appears) so you can see this connection: referencing outlet collections -> view
add the delegate by dragging the delegate's connection pin to the viewController
Show assistant editor -> add an action by ctrl + drag and drop on your delegate class viewController.m
inside that action write
[self performSegueWithIdentifier:#"mySegueIdentifier" sender:self];
Oops. Posted a comment on accident. Take a look at this answer: https://stackoverflow.com/a/14125580/1126783. You could just use a swipe gesture instead of buttons, and it uses storyboards.

IBAction UIButton firing is delayed

I have a UIButton connected to an IBAction in Interface Builder.
The Problem:
The action firing and the de-highlight of the button both take a little bit to happen.
The Facts:
View structure:
View
10 Buttons (connected via UIControlEventTouchUpInside to the IBAction
View (Subview)
Gesture recognizer
Text Field
The Subview has a UITapGestureRecognizer, which delaysTouchesBegan and delaysTouchesEnded both are set to NO
The action is happening in the main thread.
Testing a simple button (with no images or title, and only a simple NSLog), the result is the same
The Question:
Why are firing and the de-highlight delayed?
In the end, I added somewhere some UIGestureRecognizer, and forgot to set delaysTouchesBegan to NO =(
Ok I think that because of the UITapGestureRecognizer .. try to do the following :
connect an IBOutlet to your button.
2.assing the UITapGestureRecognizer delegate to your ViewController.
3.Implement this gesture delegate method in yourViewController
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldReceiveTouch:(UITouch *)touch {
return (! [yourButton pointInside:[touch locationInView:yourButton] withEvent:nil]);
}
This will make the tap to be recognized to your button not to the recognizer.
Make sure your touch event is set the first contact of the button which would be the touch down event otherwise there will be a delay in the action until whichever other event you chose gets completed (i.e. touch up inside, touch up outside, etc).
In my case, there was a delay on IBAction for a button that was in a custom CalloutView of an MKAnnotationView.
In the same way there is a ~0.5sec delay between pressing the MKAnnotationView and the MKAnnotationView actually being selected, there is also a delay between any other user interactions you might add as a subview of the MKAnnotationView.
The solution is to disable the native UIGestureRecognizer within MapView that is causing the delay of any MKAnnotation view selections.
This can be done with the solution on this post:
Set isZoomEnabled = false within a gesture recognizer attached to the mapview on any tap, then set isZoomEnabled = false within a 0.5sec async dispatch.

Resources