简体   繁体   English

当使用滑动手势识别器转到Swift中的另一个视图时如何执行操作

[英]How to perform an action when Swipe Gesture Recogniser is used to go to another View in Swift

Here is my problem (on Xcode 6.0.1, coding in Swift, using iOS 7.1 and 8.0) : I am going from one view to another using a Swipe Gesture Recogniser (with the Action Segue " present modally "). 这是我的问题(在Xcode 6.0.1上,使用iOS 7.1和8.0在Swift中进行编码) :我正在使用“手势手势识别器”(Action Segue“ present modally ”)从一个视图转到另一个视图。

Before this I was using a button " next " to go from one view to another. 在此之前,我使用按钮“ next ”从一个视图转到另一个视图。 I created an @IBAction for this button next that was performing, let say, a simple println("test") . 接下来,我为该按钮创建了一个@IBAction ,它正在执行一个简单的println("test")

@IBAction func nextPressed(sender: AnyObject) {
    println("test")
}    

If I run my app, when I press the button Next, I go to the next view and " test " appears in the consol. 如果我运行我的应用程序,则当我按下“下一步”按钮时,我会转到下一个视图,并且控制台中会显示“ test ”。

Now I do the same for a Swipe Gesture Recogniser, let say I create an @IBAction for the Swipe Gesture Recogniser (so I ctrl-click on the gesture icon next to the view controller icon at the top of the view, then drag into the code). 现在,我对“滑动手势识别器”执行相同的操作,假设我为“滑动手势识别器”创建了一个@IBAction (因此,我按住Ctrl键单击视图顶部视图控制器图标旁边的手势图标,然后将其拖入码)。

@IBAction func swipeGestureDone(sender: AnyObject) {
    println("test")
}    

If I run my app, when I swipe on the screen, I go to the next view but the " test " does not appear in the consol. 如果我运行我的应用程序,则在屏幕上滑动时,我会转到下一个视图,但控制台中不会显示“ test ”。 I cannot figure out how to execute a function when I use a Swipe to go from one view to another. 当我使用“滑动”从一个视图转到另一个视图时,我不知道如何执行功能。 It is apparently because there is no " Sent Events " for a Swipe Gesture Recogniser as for a Button. 显然是因为没有针对滑动手势识别器的“已发送事件 ”。 I don't know how to do it. 我不知道该怎么做。 Everything seems to be well connected in the Connection inspector though. 不过,在连接检查器中,一切似乎都连接良好。

Set cancelsTouchesInView of the recognizer to true . 将识别器的cancelsTouchesInView设置为true It does what it says. 它按照它说的去做。

I have a similar problem with a UITapGestureRecognizer. UITapGestureRecognizer也有类似的问题。 Here are a couple of things I found as solutions. 这是我找到的一些解决方案。 They have not worked in my case, but maybe they will fix your issue: 他们在我的案例中没有用,但是也许他们可以解决您的问题:

Try selecting the type of object a UISwipeGestureRecognizer instead of AnyObject. 尝试使用UISwipeGestureRecognizer而不是AnyObject选择对象的类型。

@IBAction func swipeGestureDone(sender: UISwipeGestureRecognizer) {
    println("test")  
}   

Also make sure that your view recognises gestures by adding 另外,通过添加以下内容确保您的视图可以识别手势

yourView.userInteractionEnabled = true 

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

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