UILongPressGesture as IBAction or Property w/ handler method? - ios

This is just a general "best programming concepts," question, but if someone could answer it for me I would appreciate it. I'm trying to add a UILongPressGestureRecognizer to a button on my viewcontroller. Is is better practice to implement the longpressgesture as an IBAction, or as a property with an included longpressgesture handler method? Or does it not matter?

Assuming the button is in your storyboard. What I normally do is create the gesture recognizer in the storyboard as well. I connect the recognizer to the view and then target an #IBAction in my view controller.
It works well for me.

Related

Implementing Gesture Recognizer as Segue Between Two View Controllers

I am building an app called Single Photo. It is part of the "Learn to Code" iBook from Apple. I have built the original app, but now would like to expand on it.
I have added a second View Controller to Main.storyboard, as well as a UIImageView. However, I am not sure how to connect the two View Controllers together. I know that I have to add a segue, but I want to use a swipe gesture (like a photo gallery). It was recommended that I use UIPanGestureRecognizer as an alternative to UISwipeGestureRecognizer.
My confusion is that I am not sure how to implement either UIGestureRecognizer. Do I have to implement it as the segue between the two View Controllers? That would make sense.
tl;dr: How do I implement UIGestureRecognizer as a segue between two View Controllers?
EDIT: When I make some changes, I get the errors seen in the screenshot.
{1}: https://i.stack.imgur.com/JpViy.png {1}
I'd be using the Swipe gesture for that; for me it seems the most natural one for changing views with touch?
Whilst I don't know Swift yet sadly, this is how i would do it;
Add a gesture recogniser object onto your view using Interface Builder. You'll find it in the list of objects to add (Label, TextField, etc).
Add an IBAction method in your ViewController.m and declare it in your .h also like:
- (IBAction)handleSwipe:(UISwipeGestureRecognizer *)recognizer
Connect the gesture recogniser in Interface Builder to the IBAction as a 'Sent Action'
Make a new Segue by Control-clicking on the first view controller and dragging the blue line to the second view. Give this segue a unique identifier and set the style of segue.
Handle the gesture in the IBAction based on direction of swipe and call the segue from here;
[self performSegueWithIdentifer:#"theSegue" sender:self];
See if that gets you there
It is possible to use a UIPanGestureRecognizer or UISwipeGestureRecognizer to trigger a segue between view controllers, but it's by no means automatic.
I suggest using a UIPageViewController. That is a type of parent view controller that manages a set of child view controllers and lets you page between them, like a photo gallery or iBooks.
There is a demo application built into Xcode called PhotoScroller that will give you the idea. Search on PhotoScroller in the Xcode help system to find it. As I recall it's written in Objective-C, but it will at least give you the idea of how a page view controller works.
tl;dr: How do I implement UIGestureRecognizer as a segue between two View Controllers?
tl;dr: You would write an interactive custom view controller transition animation.
Create a segue in storyboard from the initial view controller to your new one. Then click on it and set the identifier to something. Place the first three lines of code in your initial controller's viewDidLoad changing 'MyVC' to your controller's class name. Then put the method in and put the name of the segue you created.
let gesture = UISwipeGestureRecognizer(target: self, action: #selector(MyVC.performChange))
gesture.direction = .right
self.view.addGestureRecognizer(gesture)
func performChange() {
self.performSegue(withIdentifier: "MySegue", sender: nil)
}
I think that this is a great question that could be answered best if divided into two different sections.
First thing is to understand how to use UIGestureRecognizer so that you can do different things when a user either taps, long presses, rotates, pinches, etc, a certain view. And then, if you want to make a transition between ViewControllers look different than the usual and add a cool effect to it, you will need to learn more about UIViewControllerAnimatedTransitioning, here is a great tutorial for that: APPCODA's Introduction to Custom View Controller Transitions and Animations.
Let's do an example and try to answer your question:
Let's say you have two ViewControllers, call them MainVC and SecondaryVC and you want to transition from one to the other after the user performs a certain gesture. As you previously mentioned, you can create a segue between them and set an identifier for it (so that you can perform the segue in code later on), you do this by clicking on the segue from your storyboard and going to the Attributes Inspector. For this example let's set the identifier to ToSecondaryVC and do something when the user swipes to the right anywhere in the view.
First thing, you need to initialize a UISwipeGesture object, so let's set the gesture recognizer by adding in the following to ViewDidLoad() of the MainVC class:
let swipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(MainVC.handleSwipeGesture(_:)))
// Adding detection for a right swipe
swipeGesture.direction = .Right
view.addGestureRecognizer(swipeGesture)
After this you want to create a function in which you will perform the segue (or do anything else really) after the swipeGesture has happened. Like so,
func handleSwipeGesture(gesture: UISwipeGestureRecognizer) {
performSegueWithIdentifier("ToSecondaryVC", sender: self)
}
I think that should do it for understanding how to use UIGestureRecognizer, and you can work on the other supported gestures by doing something very similar to what we did in this example. Hopefully that helps you a little bit! Best of luck!

Why Tap Gesture Recognizer to dismiss keyboard has Referencing Outlet Collection gestureRecognizers-View?

The purpose of my Tap Gesture Recognizer is to dismiss the keyboard if the user touches any part of the screen outside the keyboard.
The Tap Gesture Recognizer has a referencing outlet collection named gestureRecognizer connected to the root View of the View Controller. I don't know why this is necessary or how to use Referencing Outlet Collections in general. I'm very curious why this is necessary for my Tap Gesture Recognizer. I do not see any code related to the gestureRecognizer in my .h or .m files for the ViewController.
I found the code for this Tap Gesture Recognizer from a tutorial out there on the interwebz so I don't know exactly why they programmed it the way they did. Would really like to know.
Thank you!

Cannot segue from UILabel to ViewController

in the storyboard, I am trying to segue from a label to a view controller. For whatever reason, it's not going. Has anyone else experience this?
Its not possible. You can only segue from UIControls or subclasses of it, like UIButtons.
UILabels don't have target-action methods for activating the segue. An alternative is to use UITapGestureRecognizer on the label and run the segue yourself.
But simply using a button is simpler.
Style the UIButton to appear like a Label and then perform your navigation, just a quick hack.

GestureRecognizer and IBOutlet don't work together

I'm creating an iPhone app. In my main view, I have a subview. When I add a gesture recognizer to this subview, and I connect it to an action in my view controller, it works : the action method is called when the gesture is performed.
But when I connect my subview to an outlet in my view controller, suddenly, the gesture recognizer doesn't work anymore.
I have recreated this simple situation in a blank project, and here it works... Have you an idea about the origin of my problem please ?
So after reading your comment, I understand that the problem was indeed that you "messed" with the IBOutlet instance in your viewDidLoad as I presumed.

How do I add a segue to an UIGestureRecognizer

I am building an app for the first time using Storyboards. I have a scene which I would like to have open another scene when there is a long tap on a particular button. I am able to add the UILongPressGestureRecognizer with no problem, but I can't figure out how to have that gesture be the segue to the other scene. Doesn't seem to matter what I Ctrl-Drag, nothing works.
Am I missing something obvious?
Thanks,
Ken
You can control-drag from your first controller's window to your second controller to create the segue, and then you can call performSegueWithIdentifier in your GestureRecognizer method.
It's now possible to do it all in your Storyboard visually. Every gesture recognizer has Triggered Segues in Connections Inspector.

Resources