在Swift中添加手势和动画

时间:2014-10-05 09:28:45

标签: xcode animation swift ios8 gesture

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

    // 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

有人给出一些建议吗?

2 个答案:

答案 0 :(得分:1)

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

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

最后的冒号是必要的,因为你的函数接受参数sender。这是因为在Objective-C中,您的方法将声明为- (void)swipeLeftCancel:(id)sender,其选择器将为swipeLeftCancel:。在Swift中,这意义不大,但它只是你在使用选择器时必须记住的东西。

答案 1 :(得分:0)

//添加手势:

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

swipeLeft是您在Gesture动作中触发的选择器。