選択したテーブル行に応じてコードを実行する長押しジェスチャー認識機能を持つテーブルがあります。
私が抱えている問題は、現在必要な行をタップしてから長押しする必要があることです。
最初に選択するためにタップする必要なく、長押ししている行をテーブルに選択させるにはどうすればよいですか?
次のコードは私のためにうまく機能します:
ViewDidLoadに長押しジェスチャー認識機能を追加します。
// tapRecognizer, placed in viewDidLoad
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: "longPress:")
self.view.addGestureRecognizer(longPressRecognizer)
次に、長押しで呼び出すメソッドは次のようになります。
//Called, when long press occurred
func longPress(longPressGestureRecognizer: UILongPressGestureRecognizer) {
if longPressGestureRecognizer.state == UIGestureRecognizerState.Began {
let touchPoint = longPressGestureRecognizer.locationInView(self.view)
if let indexPath = tableView.indexPathForRowAtPoint(touchPoint) {
// your code here, get the row for the indexPath or do whatever you want
}
}
Swift 4
override func viewDidLoad() {
super.viewDidLoad()
setupLongPressGesture()
}
func setupLongPressGesture() {
let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(self.handleLongPress))
longPressGesture.minimumPressDuration = 1.0 // 1 second press
longPressGesture.delegate = self
self.tblMessage.addGestureRecognizer(longPressGesture)
}
@objc func handleLongPress(_ gestureRecognizer: UILongPressGestureRecognizer){
if gestureRecognizer.state == .ended {
let touchPoint = gestureRecognizer.location(in: self.tblMessage)
if let indexPath = tblMessage.indexPathForRow(at: touchPoint) {
}
}
}
Swift
override func viewDidLoad() {
super.viewDidLoad()
setupLongPressGesture()
}
func setupLongPressGesture() {
let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(YourViewController.handleLongPress(_:)))
longPressGesture.minimumPressDuration = 1.0 // 1 second press
longPressGesture.delegate = self
self.tblMessage.addGestureRecognizer(longPressGesture)
}
func handleLongPress(_ longPressGestureRecognizer: UILongPressGestureRecognizer) {
if longPressGestureRecognizer.state == UIGestureRecognizerState.Began {
let touchPoint = longPressGestureRecognizer.locationInView(self.view)
if let indexPath = tableView.indexPathForRowAtPoint(touchPoint) {
// your code here, get the row for the indexPath or do whatever you want
}
}
}
目的-C
UILongPressGestureRecognizer* longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(onLongPress:)];
[self.tableView addGestureRecognizer:longPressRecognizer];
-(void)onLongPress:(UILongPressGestureRecognizer*)pGesture
{
if (pGesture.state == UIGestureRecognizerStateRecognized)
{
//Do something to tell the user!
}
if (pGesture.state == UIGestureRecognizerStateEnded)
{
UITableView* tableView = (UITableView*)self.view;
CGPoint touchPoint = [pGesture locationInView:self.view];
NSIndexPath* row = [tableView indexPathForRowAtPoint:touchPoint];
if (row != nil) {
//Handle the long press on row
}
}
}
Swift 3機能:
func handleLongPress(_ longPressGestureRecognizer: UILongPressGestureRecognizer) {
if longPressGestureRecognizer.state == UIGestureRecognizerState.Began {
let touchPoint = longPressGestureRecognizer.locationInView(self.view)
if let indexPath = tableView.indexPathForRowAtPoint(touchPoint) {
// your code here, get the row for the indexPath or do whatever you want
}
}
viewDidLoad:
let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(YourViewController.handleLongPress(_:)))
longPressGesture.minimumPressDuration = 1.0 // 1 second press
longPressGesture.delegate = self
self.tableView.addGestureRecognizer(longPressGesture)
Swift 4
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPressed(sender:)))
self.view.addGestureRecognizer(longPressRecognizer)
// MARK:アクション
@objc func longPressed(sender: UILongPressGestureRecognizer) {
if sender.state == UIGestureRecognizerState.began {
let touchPoint = sender.location(in: self.tableView)
if let indexPath = tableView.indexPathForRow(at: touchPoint) {
print("Long pressed row: \(indexPath.row)")
}
}
}
SwiftのVerision 3の場合
func longPress(_ longPressGestureRecognizer: UILongPressGestureRecognizer) {
if longPressGestureRecognizer.state == UIGestureRecognizerState.began {
let touchPoint = longPressGestureRecognizer.location(in: self.view)
if let indexPath = notificationTabelView.indexPathForRow(at: touchPoint) {
print("indexPath=\(indexPath)")
// your code here, get the row for the indexPath or do whatever you want
}
}
}
viewDidLoad
関数で
let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(EmployeeNotificationViewController.longPress(_:)))
longPressGesture.minimumPressDuration = 1.0 // 1 second press
longPressGesture.delegate = self as? UIGestureRecognizerDelegate
self.notificationTabelView.addGestureRecognizer(longPressGesture)
これを回避するには、UILongPressGestureRecognizer
の代わりにcellForRowAtIndexPath
内にdidSelectRowAtIndexPath
を追加します。
IBActionを使用すると、次のことができます(CollectionViewの場合):
@IBAction func handleLongPress(sender: AnyObject) {
if sender.state == UIGestureRecognizerState.Began
{
let position = sender.locationInView(sender.view)
if let indexPath : NSIndexPath = ((sender.view as! UICollectionView).indexPathForItemAtPoint(position))!{
print("You holding cell #\(indexPath.item)!")
}
}
}
Long Press Gesture Recognizerとリンクすることを忘れないでください。
let longPressGesture = UILongPressGestureRecognizer(target: self, action: (#selector(YourCustomeTableCell.longTap)))
self.addGestureRecognizer(longPressGesture)
func longTap(){
print("Long tap")
}