How to Do a Long Press in Swift

Swift: Long Press Gesture Recognizer - Detect taps and Long Press

Define two IBActions and set one Gesture Recognizer to each of them. This way you can perform two different actions for each gesture.

You can set each Gesture Recognizer to different IBActions in the interface builder.

@IBAction func tapped(sender: UITapGestureRecognizer)
{
println("tapped")
//Your animation code.
}

@IBAction func longPressed(sender: UILongPressGestureRecognizer)
{
println("longpressed")
//Different code
}

Through code without interface builder

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tapped:")
self.view.addGestureRecognizer(tapGestureRecognizer)

let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: "longPressed:")
self.view.addGestureRecognizer(longPressRecognizer)

func tapped(sender: UITapGestureRecognizer)
{
println("tapped")
}

func longPressed(sender: UILongPressGestureRecognizer)
{
println("longpressed")
}

Swift 5

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapped))
self.view.addGestureRecognizer(tapGestureRecognizer)

let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPressed))
self.view.addGestureRecognizer(longPressRecognizer)

@objc func tapped(sender: UITapGestureRecognizer){
print("tapped")
}

@objc func longPressed(sender: UILongPressGestureRecognizer) {
print("longpressed")
}

Customize long press gesture recognizer

The answer from @nathan is essentially fine but a detail is missing you need to implement the UIGestureRecognizerDelegate to allow both gestures works simultaneously, so this is my code

class ViewController: UIViewController, UIGestureRecognizerDelegate{   

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.

//this for .5 time
let firstGesture = UILongPressGestureRecognizer(target: self, action: #selector(firstMethod))
//this for 1.5
let secondGesture = UILongPressGestureRecognizer(target: self, action: #selector(secondMethod))
secondGesture.minimumPressDuration = 1.5
firstGesture.delegate = self
secondGesture.delegate = self
self.view.addGestureRecognizer(firstGesture)
self.view.addGestureRecognizer(secondGesture)

}

func firstMethod() {
debugPrint("short")
}

func secondMethod() {
debugPrint("long")
}

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool{
return true
}
}

Hope this help

swift: long press gesture reconizer doesn work

You have to add the long press gesture recognizer to the table view:

UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] 
initWithTarget:self action:@selector(handleLongPress:)];
lpgr.minimumPressDuration = 2.0; //seconds
lpgr.delegate = self;
[self.myTableView addGestureRecognizer:lpgr];
[lpgr release];

Then in the gesture handler: get the cell index:-

-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
CGPoint p = [gestureRecognizer locationInView:self.myTableView];

NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint:p];
if (indexPath == nil) {
NSLog(@"long press on table view but not on a row");
} else if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
NSLog(@"long press on table view at row %ld", indexPath.row);
} else {
NSLog(@"gestureRecognizer.state = %ld", gestureRecognizer.state);
}
}

Create long press gesture recognizer with annotation pin

First of all in Swift 3 signature of CLLocationManagerDelegate method's locationManager(_:didUpdateLocations:) is changed, so you need to change that method as follow.

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
//your code and don't forgot to remove private
}

You can use longGesture with mapView like this, first addGestureRecognizer in your mapView in the viewDidLoad.

let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(addAnnotationOnLongPress(gesture:)))
longPressGesture.minimumPressDuration = 1.0
self.mapView.addGestureRecognizer(longPressGesture)

Now add action for that UILongPressGestureRecognizer.

@objc func addAnnotationOnLongPress(gesture: UILongPressGestureRecognizer) {

if gesture.state == .ended {
let point = gesture.location(in: self.mapView)
let coordinate = self.mapView.convert(point, toCoordinateFrom: self.mapView)
print(coordinate)
//Now use this coordinate to add annotation on map.
var annotation = MKPointAnnotation()
annotation.coordinate = coordinate
//Set title and subtitle if you want
annotation.title = "Title"
annotation.subtitle = "subtitle"
self.mapView.addAnnotation(annotation)
}
}

How to create a long press button in TableView Cell to segue

I found an easy yet unorthodox solution but works like a charm:

Place a second button into your TableView Cell, connect your segue, and set it to hidden. Then create your IBOutlet for it, which I named SegueButton:

@IBOutlet weak var LongPressButton: UIButton!
@IBOutlet weak var SegueButton: UIButton!

Now add to your awakeFromNib:

override func awakeFromNib() {
addLongPressGesture()
}

Lastly, add the long press button code:

@objc func longPress(gesture: UILongPressGestureRecognizer) {
if gesture.state == UIGestureRecognizer.State.began {
print("Segue was Successful")
SegueButton.sendActions(for: .touchUpInside)
}
}

func addLongPressGesture(){
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longPress(gesture:)))
longPress.minimumPressDuration = 0.75
self.LongPressButton.addGestureRecognizer(longPress)
}
}


Related Topics



Leave a reply



Submit