简体   繁体   English

在两个视图控制器之间将手势识别器实现为Segue

[英]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. 它是Apple的“学习编码” iBook的一部分。 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. 我在Main.storyboard中添加了第二个View Controller,以及一个UIImageView。 However, I am not sure how to connect the two View Controllers together. 但是,我不确定如何将两个View Controller连接在一起。 I know that I have to add a segue, but I want to use a swipe gesture (like a photo gallery). 我知道我必须添加一个segue,但是我想使用滑动手势(例如照片库)。 It was recommended that I use UIPanGestureRecognizer as an alternative to UISwipeGestureRecognizer. 建议我使用UIPanGestureRecognizer替代UISwipeGestureRecognizer。

My confusion is that I am not sure how to implement either UIGestureRecognizer. 我的困惑是我不确定如何实现UIGestureRecognizer。 Do I have to implement it as the segue between the two View Controllers? 我是否必须将其实现两个View Controller之间的安全对象? That would make sense. 那是有道理的。

tl;dr: How do I implement UIGestureRecognizer as a segue between two View Controllers? tl; dr: 如何将UIGestureRecognizer作为两个View Controller之间的序列来实现?

EDIT: When I make some changes, I get the errors seen in the screenshot. 编辑:当我进行一些更改时,我得到了屏幕截图中看到的错误。 {1}: https://i.stack.imgur.com/JpViy.png {1} {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; 虽然我不十分了解Swift,但是这就是我要这样做的方式。

  1. Add a gesture recogniser object onto your view using Interface Builder. 使用Interface Builder将手势识别器对象添加到视图中。 You'll find it in the list of objects to add (Label, TextField, etc). 您可以在要添加的对象(标签,文本字段等)列表中找到它。

  2. Add an IBAction method in your ViewController.m and declare it in your .h also like: 在您的ViewController.m中添加一个IBAction方法,并在您的.h中对其进行声明,如下所示:

- (IBAction)handleSwipe:(UISwipeGestureRecognizer *)recognizer

  1. Connect the gesture recogniser in Interface Builder to the IBAction as a 'Sent Action' 将接口生成器中的手势识别器作为“已发送动作”连接到IBAction

  2. Make a new Segue by Control-clicking on the first view controller and dragging the blue line to the second view. 按住Control键并单击第一个视图控制器,然后将蓝线拖动到第二个视图,以创建新的Segue。 Give this segue a unique identifier and set the style of segue. 给此segue一个唯一的标识符并设置segue的样式。

  3. Handle the gesture in the IBAction based on direction of swipe and call the segue from here; 根据滑动方向在IBAction中处理手势并从此处调用segue;

    [self performSegueWithIdentifer:@"theSegue" sender:self]; [self performSegueWithIdentifer:@“ theSegue”发送者: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. 可以使用UIPanGestureRecognizerUISwipeGestureRecognizer触发视图控制器之间的UISwipeGestureRecognizer ,但这绝不是自动的。

I suggest using a UIPageViewController . 我建议使用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 . 这是一种父视图控制器,它管理一组子视图控制器,并允许您在它们之间进行分页,例如相册或iBooks

There is a demo application built into Xcode called PhotoScroller that will give you the idea. Xcode中内置了一个名为PhotoScroller的演示应用程序,它将为您提供想法。 Search on PhotoScroller in the Xcode help system to find it. 在Xcode帮助系统中的PhotoScroller上搜索以找到它。 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. 我记得它是用Objective-C编写的,但它至少会让您了解页面视图控制器的工作方式。

tl;dr: How do I implement UIGestureRecognizer as a segue between two View Controllers? tl; dr:如何将UIGestureRecognizer作为两个View Controller之间的序列来实现?

tl;dr: You would write an interactive custom view controller transition animation . tl; dr:您将编写一个交互式自定义视图控制器过渡动画

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. 第一件事是了解如何使用UIGestureRecognizer,以便当用户在某个视图上轻击,长按,旋转,挤压等时,您可以执行不同的操作。 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. 然后,如果要使ViewController之间的过渡看起来与平常不同并为其添加酷效果,则需要了解有关UIViewControllerAnimatedTransitioning更多信息,这是一个很棒的教程: APPCODA的自定义View Controller过渡介绍和动画。

  • Let's do an example and try to answer your question: 让我们做一个例子,尝试回答您的问题:

    1. 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. 假设您有两个ViewController,分别称为MainVC和SecondaryVC,并且您想在用户执行特定手势后从一个过渡到另一个。 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. 如前所述,您可以在它们之间创建序列并为其设置标识符(以便稍后可以在代码中执行序列),方法是从情节提要中单击序列并转到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. 对于此示例,让我们将标识符设置为ToSecondaryVC ,并在用户向视图中的任意位置向右滑动时执行某些操作。

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: 首先,您需要初始化UISwipeGesture对象,因此,通过将以下内容添加到MainVC类的ViewDidLoad()中来设置手势识别器:

    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. 此后,您要创建一个函数,在其中将执行swipeGesture之后的segue(或执行其他任何其他操作)。 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. 我认为这样做应该是为了了解如何使用UIGestureRecognizer ,并且您可以通过执行与本示例中非常相似的操作来处理其他受支持的手势。 Hopefully that helps you a little bit! 希望对您有所帮助! Best of luck! 祝你好运!

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. 将代码的前三行放在初始控制器的viewDidLoad中,将“ MyVC”更改为控制器的类名。 Then put the method in and put the name of the segue you created. 然后放入方法并放入您创建的segue的名称。

let gesture = UISwipeGestureRecognizer(target: self, action: #selector(MyVC.performChange))
gesture.direction = .right
self.view.addGestureRecognizer(gesture)

func performChange() {
    self.performSegue(withIdentifier: "MySegue", sender: nil)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM