[Swift] Implement tap and long press processing!

Introduction

During the development of the app, I used gestures such as tap and long press. So, this time, I will summarize how to execute the process at the time of tap / long press gesture.

Development environment

How to use smartphone gestures?

In Swift, you can implement processing using various gestures by using UIGestureRecognizer. There are various types of gestures as shown below.

Apple Official Document: UIGestureRecognizer

You can watch videos of various gestures from Apple's Human Interface Guidelines. Human Interface Guidelines

Processing using taps

Use UITapGestureRecognizer to recognize taps.

Click here for the entire code

import UIKit

//Add UIGestureRecognizerDelegate
class ViewController: UIViewController, UIGestureRecognizerDelegate {
    
    override func viewDidLoad() {
        super.viewDidLoad()

        //Create an instance for a single tap
        let tapGesture: UITapGestureRecognizer = UITapGestureRecognizer(
            target: self,
            action: #selector(ViewController.singleTap(_:))
        )
        
        //Set delegate
        tapGesture.delegate = self
        
        //Add tap gesture to view
        self.view.addGestureRecognizer(tapGesture)
        
    }

    //Method executed on single tap
    @objc func singleTap(_ sender: UITapGestureRecognizer) {
        if sender.state == .ended {
            //Here, describe the process you want to execute at the end of tapping.
        }
    }
}

Commentary

//Create an instance for a single tap
let tapGesture: UITapGestureRecognizer = UITapGestureRecognizer(
    target: self,
    action: #selector(ViewController.singleTap(_:))
)

I am creating a UITapGestureRecognizer instance for a single tap. At the time of generation, the target and the method to be executed at the time of tap are specified.

Apple official document init (target: action :)


//Add tap gesture to view
self.view.addGestureRecognizer(tapGesture)

As you can see in the comments, we are adding a tap gesture to the view. As a result, the specified process is executed when the view is tapped.


//Method executed on single tap
@objc func singleTap(_ sender: UITapGestureRecognizer) {
    if sender.state == .ended {
        //Here, describe the process you want to execute when you tap and release it.
    }
}

UITapGesture implements various properties, including the state property. Here we will only introduce what state properties are available. Please check the official document for details.

Apple Official Document UIGestureRecognizer.State

This time, by setting sender.state == .ended, the specified process is executed at the end of the tap gesture.

What are @ objc and # selector?

By adding @ objc when defining a method, it will be recognized as an Objective-C method. So why should it be recognized as an Objective-C method?

target-action is a mechanism implemented in Objective-C, and it is necessary to specify the method in Objective-C in target-action. Therefore, @objc is added when defining the method.

Also, in Swift, you can specify Objective-C methods by # selector.

Reference site: #selector, @objc and target-action

Processing using a long press

The process using the tap is basically the same.

override func viewDidLoad() {
    super.viewDidLoad()

    //Create an instance for long press
    let longPressGesture = UILongPressGestureRecognizer(
        target: self,
        action: #selector(ViewController.longPress(_:))
    )
        
    //Set delegate
    longPressGesture.delegate = self
        
    //Added long press gesture to view
    self.view.addGestureRecognizer(longPressGesture)
}
    
//Method executed during long press
@objc func longPress(_ sender: UILongPressGestureRecognizer) {

    if sender.state == .ended {
        //Describe the process you want to execute at the end of the long press
    }
}

The explanation is the same as the single tap, so I will omit it.

Summary

This time, I summarized what I learned when implementing tap and long press processing. I was surprised that it was easier to implement than I had imagined. Since my understanding is still shallow, I would like to read the documents again and deepen my understanding.

that's all.

Recommended Posts

[Swift] Implement tap and long press processing!
[Swift] Implement swipe processing
[Swift] Asynchronous processing "GCD"
[Swift] Constants and variables
Implement writing and reading to Property List (.plist) in Swift