0

I would like to make long press tableview Cells, But i'm getting error:

UIGestureRecognizer.Type' does not have a member named 'state'

Here's the code

 override func viewDidLoad() {
        super.viewDidLoad()

        var gesture: UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: "longPressed:")
        gesture.minimumPressDuration = 2.0
        self.view.addGestureRecognizer(gesture)

        }

func longpressed() {

    if(UIGestureRecognizer.state == UIGestureRecognizerState.Ended){

        print("ended")
    } else if (UIGestureRecognizer.state == UIGestureRecognizerState.Began){

        print("began")


    }
}

And Yes I've created Bridging-Header.h and imported this file:

#import <UIKit/UIGestureRecognizerSubclass.h>

I want swift tutorial not objective-c!

Salah
  • 933
  • 3
  • 13
  • 32
  • Have you tried in adding the gesture to each cell object? No need of importing this class. – Amit89 Aug 23 '15 at 15:51
  • @Amit89 thanks for your reply, This code was in Tableview , And how can i add the gesture in each cell ? – Salah Aug 23 '15 at 15:55
  • 1
    possible duplicate of [UIGestureRecognizer and UITableViewCell issue](http://stackoverflow.com/questions/4604296/uigesturerecognizer-and-uitableviewcell-issue) – Shamas S Aug 23 '15 at 16:01
  • @iosDev82 Thanks! but this Objective-c and I don't know how to read objective-c codes :) – Salah Aug 23 '15 at 16:03

2 Answers2

1

Add a gesture recogniser to your UITableView like

    var gestureRec = UILongPressGestureRecognizer(target: self, action: "didTap:")
    self.tableView.addGestureRecognizer(gestureRec)

Implement a didTap function, which would look something like this.

func didTap(sender : UIGestureRecognizer)
{
    if sender.state == .Began {
        var location = sender.locationInView(self.tableView)
        var indexPath = self.tableView.indexPathForRowAtPoint(location)
        var cell = self.tableView.cellForRowAtIndexPath(indexPath!)
    }
}

This should work.

Shamas S
  • 7,507
  • 10
  • 46
  • 58
0

Try this in your longPress method:

func longpressed(sender: UILongPressGestureRecognizer) {

var state = sender.state

if(state.state == UIGestureRecognizerState.Ended){
    print("ended")

} else if (state.state == UIGestureRecognizerState.Began){
    print("began")

}
}
whereisleo
  • 818
  • 5
  • 12