1

I am trying to add a long press gesture recognizer to a button inside a tableview cell. But i could not figure out what i am doing wrong. I want my button to play a different audio file when user taps and holds the button.

MY CODE

import UIKit
import AVFoundation

class ViewController6: UIViewController, UITableViewDataSource, UITableViewDelegate
{

    var player = AVQueuePlayer()


    @IBOutlet var tableView: UITableView!

    override func viewDidLoad()
    {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
    }


//Functions for tableView

    //Cell - For Rifles

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return (arrayOfRifles.count)
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell6 = tableView.dequeueReusableCell(withIdentifier: "cell6", for: indexPath) as! ViewControllerTableViewCell6

        cell6.myImage.image = UIImage(named: arrayOfRifles[indexPath.row] + ".jpg")
        cell6.myButton.setTitle(buttonDataRifles[indexPath.row], for: UIControlState.normal)
        cell6.myButton.tag = indexPath.row
        cell6.myButton.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
        cell6.myButton.addGestureRecognizer(longPressGesture)
        return cell6

    }

    func longPressGesture() {
        let lpg = UILongPressGestureRecognizer(target: self, action: "longPress")
        lpg.minimumPressDuration = 0.5
    }

    func longPress(_ sender: UIButton) {
        if let url = Bundle.main.url(forResource: soundArrayRiflesUzun[(sender as AnyObject).tag], withExtension: "mp3")
        {
            player.removeAllItems()
            player.insert(AVPlayerItem(url: url), after: nil)
            player.play()

        }

    }

 //Action for Sounds


    @IBAction func buttonAction(_ sender: UIButton)
    {

        if let url = Bundle.main.url(forResource: soundArrayRifles[sender.tag], withExtension: "mp3")
        {
            player.removeAllItems()
            player.insert(AVPlayerItem(url: url), after: nil)
            player.play()

        }

    }

}

I am getting an error in this line

cell6.myButton.addGestureRecognizer(longPressGesture)
Volkan Elçi
  • 163
  • 2
  • 13

2 Answers2

4

Do like this,

cell6.myButton.addTarget(self, action: #selector(ViewController6.buttonAction(_:)), for: .touchUpInside)
cell6.myButton.addGestureRecognizer(self.longPressGesture())

func longPressGesture() -> UILongPressGestureRecognizer {
    let lpg = UILongPressGestureRecognizer(target: self, action: #selector(ViewController6.longPress))
    lpg.minimumPressDuration = 0.5
    return lpg
}

func longPress(_ sender: UILongPressGestureRecognizer) {
    if let url = Bundle.main.url(forResource: soundArrayRiflesUzun[sender.view.tag], withExtension: "mp3")
    {
        player.removeAllItems()
        player.insert(AVPlayerItem(url: url), after: nil)
        player.play()

    }

}
Venk
  • 5,949
  • 9
  • 41
  • 52
  • 1
    Thank you, that worked. But when i release the button, the audio file starts to play again. Do you have any idea how to fix that? – Volkan Elçi Nov 24 '16 at 13:04
  • 1
    Fixed that too. I have added 'if sender.state == UIGestureRecognizerState.began' and 'if sender.state == UIGestureRecognizerState.ended' Again thank you very much for your help – Volkan Elçi Nov 24 '16 at 13:47
0

Make your method return the lpg:

func longPressGesture() -> UILongPressGestureRecognizer {
    let lpg = UILongPressGestureRecognizer(target: self, action: "longPress")
    lpg.minimumPressDuration = 0.5
    return lpg
}

and call it like this:

...
cell6.myButton.addGestureRecognizer(longPressGesture())
    return cell6

}
shallowThought
  • 19,212
  • 9
  • 65
  • 112