Search code examples
xcodeanimationswiftios8gesture

Add gestures and animation in Swift


I am making an application which is inspired from iOS8 voice messages and trying to add 'gestures and animations(like slide left and cancel the record,slide rightto Upload the voice record.)' but it doesn't work at all.Here is the code snippet below.

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

Whole code(before adding UISwipeGestureRecognizer) is in here ー> https://github.com/chansuke/GoForIt

Anyone give some advice?


Solution

  • Your action selectors should be named swipeLeftCancel: and swipeRightUpload::

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

    The colon at the end is necessary because your functions accept an argument sender. This is because in Objective-C, your method would be declared as - (void)swipeLeftCancel:(id)sender, and its selector would be swipeLeftCancel:. In Swift this makes a lot less sense, but it's just something you have to remember when you use selectors.