2015-08-23 94 views
0

我想打長按的tableview細胞,但我得到的錯誤:迅速GestureRecognizer

UIGestureRecognizer.Type' does not have a member named '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") 


    } 
} 

是的,我已經創建了橋接報頭.h並導入此文件:

#import <UIKit/UIGestureRecognizerSubclass.h> 

我想要swift教程不是objective-c!

+0

您是否嘗試將手勢添加到每個單元格對象?無需導入此類。 – Amit89

+0

@Amit89感謝您的回覆,此代碼在Tableview中,我如何在每個單元中添加手勢? – Salah

+1

可能重複的[UIGestureRecognizer和UITableViewCell問題](http://stackoverflow.com/questions/4604296/uigesturerecognizer-and-uitableviewcell-issue) –

回答

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

這應該有效。

0

試試這個在您長按法:

func longpressed(sender: UILongPressGestureRecognizer) { 

var state = sender.state 

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

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

} 
}