单元格中的swift GestureRecognizer

时间:2015-08-23 15:43:11

标签: swift uigesturerecognizer

我想长按tableview单元格,但我收到错误:

  

UIGestureRecognizer.Type'没有名为'state'的成员

这是代码

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


    }
}

是的我已经创建了Bridging-Header.h并导入了这个文件:

#import <UIKit/UIGestureRecognizerSubclass.h>

我想要快速的教程而不是客观的c!

2 个答案:

答案 0 :(得分:1)

为您的UITableView添加手势识别器

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

实现didTap函数,看起来像这样。

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!)
    }
}

这应该有用。

答案 1 :(得分:0)

在你的longPress方法中试试这个:

func longpressed(sender: UILongPressGestureRecognizer) {

var state = sender.state

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

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

}
}