While developing the app, I used swipe gestures. So this time, I will summarize the swipe gestures.
You can watch videos of various gestures from Apple's Human Interface Guidelines. Human Interface Guidelines
Looking at the guidelines, what's the difference between flicking and swiping? I thought, so I looked it up.
Flick: The image of lightly flicking the screen with your finger. Used for character input. Swipe: An image of sliding while touching the screen. Used for screen transitions, etc.
Reference site: Differences between "swipe" and "flick" and how to use them: basic smartphone operations that you can't hear anymore
It's certainly called flick input. (Maybe it was hard to imagine because I'm used to tap input for character input)
Whole code
import UIKit
//Add UIGestureRecognizerDelegate
class ViewController: UIViewController, UIGestureRecognizerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
//Create an instance for swiping up
let upSwipe = UISwipeGestureRecognizer(
target: self,
action: #selector(ViewController.didSwipe(_:))
)
upSwipe.direction = .up
self.view.addGestureRecognizer(upSwipe)
//Create an instance for a right swipe
let rightSwipe = UISwipeGestureRecognizer(
target: self,
action: #selector(ViewController.didSwipe(_:))
)
rightSwipe.direction = .right
self.view.addGestureRecognizer(rightSwipe)
//Create an instance for the down swipe
let downSwipe = UISwipeGestureRecognizer(
target: self,
action: #selector(ViewController.didSwipe(_:))
)
downSwipe.direction = .down
self.view.addGestureRecognizer(downSwipe)
//Create an instance for a left swipe
let leftSwipe = UISwipeGestureRecognizer(
target: self,
action: #selector(ViewController.didSwipe(_:))
)
leftSwipe.direction = .left
self.view.addGestureRecognizer(leftSwipe)
}
//Method executed when swiping
@objc func didSwipe(_ sender: UISwipeGestureRecognizer) {
//Specify the execution process in the swipe direction in the case statement
switch sender.direction {
case .up:
//What you want to do when swiping up
case .right:
//What you want to do when you swipe right
case .down:
//The process you want to execute when swiping down
case .left:
//The process you want to execute when swiping left
default:
break
}
}
}
Reference: [Swift/UIKit] Tap/long press/swipe to recognize
The swipe process is the same except for the specified direction, so here we will briefly explain using the code for the above swipe process.
//Create an instance for swiping up
let upSwipe = UISwipeGestureRecognizer(
target: self,
action: #selector(ViewController.didSwipe(_:))
)
upSwipe.direction = .up
self.view.addGestureRecognizer(upSwipe)
As with tap and long press, first create an instance.
At that time, specify the method you want to execute during the swipe gesture.
The reason why # selector
is used when specifying the method and @ objc
when defining the method are as follows.
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 with #selector. Reference: [Swift] Tap, implement long press processing!
upSwipe.direction = .up
Specify the swipe direction from direction.
There are four swipe directions in all. right
, left
, up
, down
Official Document UISwipe Gesture Recognizer .Direction
self.view.addGestureRecognizer(upSwipe)
I'm adding a swipe event to the view with addGestureRecognizer ()
.
//Method executed when swiping
@objc func didSwipe(_ sender: UISwipeGestureRecognizer) {
//Specify the execution process in the swipe direction in the case statement
switch sender.direction {
case .up:
//What you want to do when swiping up
case .right:
//What you want to do when you swipe right
case .down:
//The process you want to execute when swiping down
case .left:
//The process you want to execute when swiping left
default:
break
}
}
Here is the process to be executed at each swipe. The switch statement is used to set the process to be executed depending on the swipe direction.
When I was first investigating how to disable and enable gestures, I found an article that the following code can be used.
UIApplication.shared.beginIgnoringInteractionEvents() //Invalidation
UIApplication.shared.endIgnoringInteractionEvents() //activation
However, when I implemented it, the following warning appeared.
beginIgnoringInteractionEvents()' was deprecated in iOS 13.0: Use UIView's userInteractionEnabled property instead
Apparently, the above code has been deprecated since iOS 13. You said that you should use userInteractionEnabled
.
Click here to disable and enable using userInteractionEnabled
.
self.view.isUserInteractionEnabled = false //Gesture disabled
self.view.isUserInteractionEnabled = true //Gesture enabled (default value)
Reference: Official document isUserInteractionEnabled
This time I have summarized what I learned about swipe processing. I learned that the swipe process that I usually use casually is implemented like this.
that's all.
Recommended Posts