【问题标题】:Add gestures and animation in Swift在 Swift 中添加手势和动画
【发布时间】:2014-10-05 09:28:45
【问题描述】:

我正在制作一个受 iOS8 语音消息启发的应用程序,并尝试添加“手势和动画(例如向左滑动并取消记录,向右滑动以上传语音记录。)”,但它根本不起作用。下面是sn-p的代码。

    // Swipe left and cancel
    let swipeLeftGesture = UISwipeGestureRecognizer(target: self, action: "swipeLeftCancel")
    swipeLeftGesture.direction = UISwipeGestureRecognizerDirection.Left
    self.view.addGestureRecognizer(swipeLeftGesture)

    // Swipe right and upload
    let swipeRightGesture = UISwipeGestureRecognizer(target: self, action: "swipeRightUpload")
    swipeRightGesture.direction = UISwipeGestureRecognizerDirection.Right
    self.view.addGestureRecognizer(swipeRightGesture)
    session.requestRecordPermission({(granted: Bool)-> Void in
        if granted {
            self.setupRecorder()
        } else {
            println("Permission to record not granted")
        }
    })

   func swipeLeftCancel(sender: UISwipeGestureRecognizer) {
    // slide left and cancel
   }

   func swipeRightUpload(sender: UISwipeGestureRecognizer) {
    // slide right and upload
    )

整个代码(在添加 UISwipeGestureRecognizer 之前)在这里 ー> https://github.com/chansuke/GoForIt

有人给点建议吗?

【问题讨论】:

  • “根本不起作用”我猜你的意思是当你执行滑动手势时应用程序崩溃?

标签: xcode animation swift ios8 gesture


【解决方案1】:

您的操作选择器应命名为swipeLeftCancel:swipeRightUpload:

let swipeLeftGesture = UISwipeGestureRecognizer(target: self, action: "swipeLeftCancel:")

末尾的冒号是必要的,因为您的函数接受参数sender。这是因为在 Objective-C 中,您的方法将被声明为 - (void)swipeLeftCancel:(id)sender,而它的选择器将是 swipeLeftCancel:。在 Swift 中,这没有什么意义,但这只是你在使用选择器时必须记住的事情。

【讨论】:

  • 为了简洁起见并保持所有人的理智,我将省略对函数接受多个参数、混合命名参数和未命名参数时会发生什么的广泛解释,以及关于 令人惊叹的语言设计导致了这一点。
【解决方案2】:

//添加手势:

    let swipeLeftGesture = UISwipeGestureRecognizer(target: self, action:"swipeLeft")
    swipeLeftGesture.direction = UISwipeGestureRecognizerDirection.Left
    self.view.addGestureRecognizer(swipeLeftGesture)

swipeLeft 是在手势操作时触发的选择器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多