首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >滑动手势-斯威夫特3

滑动手势-斯威夫特3
EN

Stack Overflow用户
提问于 2016-12-06 20:31:40
回答 1查看 4.6K关注 0票数 1

我需要我的应用程序来响应一个滑动手势,我得到了这个代码到目前为止。但由于某种原因,当我滑动时,应用程序就崩溃了。有什么问题吗?我在xcode中使用了swift 3。谢谢

代码语言:javascript
复制
    override func viewDidLoad() {
    super.viewDidLoad()

    var swipeRight = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
    swipeRight.direction = UISwipeGestureRecognizerDirection.Right
    self.view.addGestureRecognizer(swipeRight)

    var swipeDown = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
    swipeDown.direction = UISwipeGestureRecognizerDirection.Down
    self.view.addGestureRecognizer(swipeDown)
}

func respondToSwipeGesture(gesture: UIGestureRecognizer) {

    if let swipeGesture = gesture as? UISwipeGestureRecognizer {


        switch swipeGesture.direction {
            case UISwipeGestureRecognizerDirection.Right:
                print("Swiped right")
            case UISwipeGestureRecognizerDirection.Down:
                print("Swiped down")
            case UISwipeGestureRecognizerDirection.Left:
                print("Swiped left")
            case UISwipeGestureRecognizerDirection.Up:
                print("Swiped up")
            default:
                break
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-12-06 20:55:45

这是由于错误:-没有用Objective选择器'respondToSwipeGesture:‘声明的方法

这意味着编译器没有找到任何方法"respondToSwipeGesture“,当您试图滑动程序时,尝试查找该方法并崩溃,因为它无法找到它。

请提供这样的行动:

代码语言:javascript
复制
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))

let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))

这将解决你的问题。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41004313

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档