I need to use swipe to recognize swipe gesture down and then right. But on swift UISwipeGestureRecognizer has predeterminate Right direction.. And I don't know how make this for use other directions..
You need to have one UISwipeGestureRecognizer
for each direction. It's a little weird because the UISwipeGestureRecognizer.direction
property is an options-style bit mask, but each recognizer can only handle one direction. You can send them all to the same handler if you want, and sort it out there, or send them to different handlers. Here's one implementation:
override func viewDidLoad() {
super.viewDidLoad()
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture))
swipeRight.direction = .right
self.view.addGestureRecognizer(swipeRight)
let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture))
swipeDown.direction = .down
self.view.addGestureRecognizer(swipeDown)
}
@objc func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case .right:
print("Swiped right")
case .down:
print("Swiped down")
case .left:
print("Swiped left")
case .up:
print("Swiped up")
default:
break
}
}
}
Swift 3:
override func viewDidLoad() {
super.viewDidLoad()
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
swipeRight.direction = UISwipeGestureRecognizerDirection.right
self.view.addGestureRecognizer(swipeRight)
let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
swipeDown.direction = UISwipeGestureRecognizerDirection.down
self.view.addGestureRecognizer(swipeDown)
}
func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.right:
print("Swiped right")
case UISwipeGestureRecognizerDirection.down:
print("Swiped down")
case UISwipeGestureRecognizerDirection.left:
print("Swiped left")
case UISwipeGestureRecognizerDirection.up:
print("Swiped up")
default:
break
}
}
}
UISwipeGestureRecognizerDirection
in front of .Down
ect. Just using swipeDown.direction = .Down
if sufficient enough. Just a tip =) –
Wrapper swipe.direction = [.Right,.Down,.Up,.Left]
), the recognizer will not even get called, perhaps this is an issue with swift, but as of right now does not work. –
Monikamoniker swipeGesture.direction = UISwipeGestureRecognizerDirection.right self.view.addGestureRecognizer(swipeGesture) swipeGesture.direction = UISwipeGestureRecognizerDirection.left self.view.addGestureRecognizer(swipeGesture)
–
Unequivocal @objc
in front of func respondToSwipeGesture
for this to work in Swift 4 –
Protist @objc
is still needed in front of func respondToSwipeGesture
method. –
Nervy I just felt like contributing this, looks more elegant in the end:
func addSwipe() {
let directions: [UISwipeGestureRecognizerDirection] = [.Right, .Left, .Up, .Down]
for direction in directions {
let gesture = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipe:"))
gesture.direction = direction
self.addGestureRecognizer(gesture)
}
}
func handleSwipe(sender: UISwipeGestureRecognizer) {
print(sender.direction)
}
self.addGestureRecognizer(gesture)
caused an error for me. What fixed it was self.view.addGestureRecognizer(gesture);
. –
Norvan UIView
subclass, but you're absolutely right if you're in a UIViewController
! –
Backsaw From the storyboard:
- Add four swipe gesture recognizers to your view.
- Set each one with the target direction from the attribute inspector. You can select right, left, up or down
- One by one, select the swipe gesture recognizer, control + drag to your view controller. Insert the name (let us say leftGesture, rightGesture, upGesture and downGesture), change the connection to: Action and type to: UISwipeGestureRecognizer
From your viewController:
@IBAction func rightGesture(sender: UISwipeGestureRecognizer) {
print("Right")
}
@IBAction func leftGesture(sender: UISwipeGestureRecognizer) {
print("Left")
}
@IBAction func upGesture(sender: UISwipeGestureRecognizer) {
print("Up")
}
@IBAction func downGesture(sender: UISwipeGestureRecognizer) {
print("Down")
}
Looks like things have changed lately. In XCode 7.2 the following approach works:
override func viewDidLoad() {
super.viewDidLoad()
let swipeGesture = UISwipeGestureRecognizer(target: self, action: "handleSwipe:")
swipeGesture.direction = [.Down, .Up]
self.view.addGestureRecognizer(swipeGesture)
}
func handleSwipe(sender: UISwipeGestureRecognizer) {
print(sender.direction)
}
Tested in Simulator on iOS 8.4 and 9.2 and on actual device on 9.2.
Or, using mlcollard's handy extension here:
let swipeGesture = UISwipeGestureRecognizer() {
print("Gesture recognized !")
}
swipeGesture.direction = [.Down, .Up]
self.view.addGestureRecognizer(swipeGesture)
[UISwipeGestureRecognizerDirection.right, .left, .up, .down]
–
Handpick Apple Swift version 3.1 - Xcode Version 8.3 (8E162)
The handy way from Alexandre Cassagne's approach
let directions: [UISwipeGestureRecognizerDirection] = [.up, .down, .right, .left]
for direction in directions {
let gesture = UISwipeGestureRecognizer(target: self, action: #selector(YourClassName.handleSwipe(gesture:)))
gesture.direction = direction
self.view?.addGestureRecognizer(gesture)
}
func handleSwipe(gesture: UISwipeGestureRecognizer) {
print(gesture.direction)
switch gesture.direction {
case UISwipeGestureRecognizerDirection.down:
print("down swipe")
case UISwipeGestureRecognizerDirection.up:
print("up swipe")
case UISwipeGestureRecognizerDirection.left:
print("left swipe")
case UISwipeGestureRecognizerDirection.right:
print("right swipe")
default:
print("other swipe")
}
}
defaults write com.apple.dt.xcode IDEPlaygroundDisableSimulatorAlternateFramebuffer -bool YES
in Terminal to fix a bug on some hardware. It should be fixed in a new version of Xcode –
Staging In Swift 4.2 and Xcode 9.4.1
Add Animation delegate, CAAnimationDelegate to your class
//Swipe gesture for left and right
let swipeFromRight = UISwipeGestureRecognizer(target: self, action: #selector(didSwipeLeft))
swipeFromRight.direction = UISwipeGestureRecognizerDirection.left
menuTransparentView.addGestureRecognizer(swipeFromRight)
let swipeFromLeft = UISwipeGestureRecognizer(target: self, action: #selector(didSwipeRight))
swipeFromLeft.direction = UISwipeGestureRecognizerDirection.right
menuTransparentView.addGestureRecognizer(swipeFromLeft)
//Swipe gesture selector function
@objc func didSwipeLeft(gesture: UIGestureRecognizer) {
//We can add some animation also
DispatchQueue.main.async(execute: {
let animation = CATransition()
animation.type = kCATransitionReveal
animation.subtype = kCATransitionFromRight
animation.duration = 0.5
animation.delegate = self
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
//Add this animation to your view
self.transparentView.layer.add(animation, forKey: nil)
self.transparentView.removeFromSuperview()//Remove or hide your view if requirement.
})
}
//Swipe gesture selector function
@objc func didSwipeRight(gesture: UIGestureRecognizer) {
// Add animation here
DispatchQueue.main.async(execute: {
let animation = CATransition()
animation.type = kCATransitionReveal
animation.subtype = kCATransitionFromLeft
animation.duration = 0.5
animation.delegate = self
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
//Add this animation to your view
self.transparentView.layer.add(animation, forKey: nil)
self.transparentView.removeFromSuperview()//Remove or hide yourview if requirement.
})
}
If you want to remove gesture from view use this code
self.transparentView.removeGestureRecognizer(gesture)
Ex:
func willMoveFromView(view: UIView) {
if view.gestureRecognizers != nil {
for gesture in view.gestureRecognizers! {
//view.removeGestureRecognizer(gesture)//This will remove all gestures including tap etc...
if let recognizer = gesture as? UISwipeGestureRecognizer {
//view.removeGestureRecognizer(recognizer)//This will remove all swipe gestures
if recognizer.direction == .left {//Especially for left swipe
view.removeGestureRecognizer(recognizer)
}
}
}
}
}
Call this function like
//Remove swipe gesture
self.willMoveFromView(view: self.transparentView)
Like this you can write remaining directions and please careful whether if you have scroll view or not from bottom to top and vice versa
If you have scroll view, you will get conflict for Top to bottom and view versa gestures.
Swipe gesture to the view you want, or viewcontroller whole view in Swift 5 & XCode 11 based on @Alexandre Cassagne
override func viewDidLoad() {
super.viewDidLoad()
addSwipe()
}
func addSwipe() {
let directions: [UISwipeGestureRecognizer.Direction] = [.right, .left, .up, .down]
for direction in directions {
let gesture = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe))
gesture.direction = direction
self.myView.addGestureRecognizer(gesture)// self.view
}
}
@objc func handleSwipe(sender: UISwipeGestureRecognizer) {
let direction = sender.direction
switch direction {
case .right:
print("Gesture direction: Right")
case .left:
print("Gesture direction: Left")
case .up:
print("Gesture direction: Up")
case .down:
print("Gesture direction: Down")
default:
print("Unrecognized Gesture Direction")
}
}
Swift 5+
Add desired gestures to some UIView
:
[UISwipeGestureRecognizer.Direction.up, .down, .left, .right].forEach {
let gesture = UISwipeGestureRecognizer(target: self, action: #selector(swiped))
gesture.direction = $0
someView.addGestureRecognizer(gesture)
}
Handle swipe actions:
@objc func swiped(_ gesture: UISwipeGestureRecognizer) {
switch gesture.direction {
case .up: print("up")
case .down: print("down")
case .left: print("left")
case .right: print("right")
default: break
}
}
UISwipeGestureRecognizer
has a direction
property that has the following definition:
var direction: UISwipeGestureRecognizerDirection
The permitted direction of the swipe for this gesture recognizer.
The problem with Swift 3.0.1 (and below) is that even if UISwipeGestureRecognizerDirection
conforms to OptionSet
, the following snippet will compile but won't produce any positive expected result:
// This compiles but does not work
let gesture = UISwipeGestureRecognizer(target: self, action: #selector(gestureHandler))
gesture.direction = [.right, .left, .up, .down]
self.addGestureRecognizer(gesture)
As a workaround, you will have to create a UISwipeGestureRecognizer
for each desired direction
.
The following Playground code shows how to implement several UISwipeGestureRecognizer
for the same UIView
and the same selector
using Array's map
method:
import UIKit
import PlaygroundSupport
class SwipeableView: UIView {
convenience init() {
self.init(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
backgroundColor = .red
[UISwipeGestureRecognizerDirection.right, .left, .up, .down].map({
let gesture = UISwipeGestureRecognizer(target: self, action: #selector(gestureHandler))
gesture.direction = $0
self.addGestureRecognizer(gesture)
})
}
func gestureHandler(sender: UISwipeGestureRecognizer) {
switch sender.direction {
case [.left]: frame.origin.x -= 10
case [.right]: frame.origin.x += 10
case [.up]: frame.origin.y -= 10
case [.down]: frame.origin.y += 10
default: break
}
}
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
view.addSubview(SwipeableView())
}
}
let controller = ViewController()
PlaygroundPage.current.liveView = controller
UISwipeGestureRecognizerDirection(rawValue: 15)
–
Landgrave Swipe Gesture in Swift 5
override func viewDidLoad() {
super.viewDidLoad()
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture))
swipeLeft.direction = .left
self.view!.addGestureRecognizer(swipeLeft)
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture))
swipeRight.direction = .right
self.view!.addGestureRecognizer(swipeRight)
let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture))
swipeUp.direction = .up
self.view!.addGestureRecognizer(swipeUp)
let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture))
swipeDown.direction = .down
self.view!.addGestureRecognizer(swipeDown)
}
@objc func handleGesture(gesture: UISwipeGestureRecognizer) -> Void {
if gesture.direction == UISwipeGestureRecognizer.Direction.right {
print("Swipe Right")
}
else if gesture.direction == UISwipeGestureRecognizer.Direction.left {
print("Swipe Left")
}
else if gesture.direction == UISwipeGestureRecognizer.Direction.up {
print("Swipe Up")
}
else if gesture.direction == UISwipeGestureRecognizer.Direction.down {
print("Swipe Down")
}
}
EDİT: "swift5.3"
First create a baseViewController
and add viewDidLoad
this code :
class BaseViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(swiped))
swipeRight.direction = .right
self.view.addGestureRecognizer(swipeRight)
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(swiped))
swipeLeft.direction = .left
self.view.addGestureRecognizer(swipeLeft)
}
// Example Tabbar 5 pages
@objc func swiped(_ gesture: UISwipeGestureRecognizer) {
if gesture.direction == .left {
if (self.tabBarController?.selectedIndex)! < 5 {
self.tabBarController?.selectedIndex += 1
}
} else if gesture.direction == .right {
if (self.tabBarController?.selectedIndex)! > 0 {
self.tabBarController?.selectedIndex -= 1
}
}
}
}
And use this baseController
class:
class YourViewController: BaseViewController {
// its done. Swipe successful
//Now you can use all the Controller you have created without writing any code.
}
After digging around for a while:
The shortest way to add swipes for all 4 directions is:
override func viewDidLoad() {
super.viewDidLoad()
for direction in [UISwipeGestureRecognizer.Direction.down, .up, .left, .right]{
let swipeGest = UISwipeGestureRecognizer(target: self, action: #selector(swipeAction(_:)))
swipeGest.direction = direction
self.view.addGestureRecognizer(swipeGest)
}
}
@objc func swipeAction(_ gesture: UISwipeGestureRecognizer){
switch gesture.direction {
case UISwipeGestureRecognizer.Direction.right:
print("Swiped right")
case UISwipeGestureRecognizer.Direction.down:
print("Swiped down")
case UISwipeGestureRecognizer.Direction.left:
print("Swiped left")
case UISwipeGestureRecognizer.Direction.up:
print("Swiped up")
default: break
}
In Swift 5,
let swipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe))
swipeGesture.direction = [.left, .right, .up, .down]
view.addGestureRecognizer(swipeGesture)
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let leftside = UISwipeGestureRecognizer(target: self, action: #selector(swiped))
leftside.direction = .left
view.addGestureRecognizer(leftside)
let rightside = UISwipeGestureRecognizer(target: self, action: #selector(swiped))
rightside.direction = .right
view.addGestureRecognizer(rightside)
let upside = UISwipeGestureRecognizer(target: self, action: #selector(swiped))
upside.direction = .up
view.addGestureRecognizer(upside)
let downside = UISwipeGestureRecognizer(target: self, action: #selector(swiped))
downside.direction = .down
view.addGestureRecognizer(downside)
// Do any additional setup after loading the view, typically from a nib.
}
@objc func swiped(gesture: UIGestureRecognizer){
if let swipeGesture = gesture as? UISwipeGestureRecognizer{
switch swipeGesture.direction{
case UISwipeGestureRecognizer.Direction.left:
view.backgroundColor = UIColor.red
case UISwipeGestureRecognizer.Direction.right:
view.backgroundColor = UIColor.yellow
case UISwipeGestureRecognizer.Direction.up:
view.backgroundColor = UIColor.green
case UISwipeGestureRecognizer.Direction.down:
view.backgroundColor = UIColor.blue
default:
"ERROR"
}
}
}
}
For 2022
Here's how you'd probably write it in any real-life code.
You'd never clutter your VC code with all the boilerplate.
In your view controller ...
override func viewDidLoad() {
super.viewDidLoad()
pentaSceneView.buildFresh()
addSwipesLRUD(#selector(swipeLeft), #selector(swipeRight),
#selector(swipeUp), #selector(swipeDown))
}
and ...
@objc func swipeLeft() {
blah ...
}
@objc func swipeRight() {
blah ...
}
@objc func swipeUp() {
blah ...
}
@objc func swipeDown() {
blah ...
}
Should you use a switch statement? Answer: NO, don't.
Apple's code for .direction
is not so much broken, but just "incredibly stupid", and they keep changing it. A switch statement is neither more elegant nor shorter than having four functions, and it will very likely break when Apple (again) slightly change or rationalize the way .direction
works.
Here's the simple extension needed.
Put this in any file in your project, say, "Utils.swift". Every real-world project has an "extensions in here" type file, where, you keep very common extensions you use in all projects.
extension UIViewController {
///Add a UISwipeGestureRecognizer.
func addSwipe(_ d: UISwipeGestureRecognizer.Direction, _ s: Selector) {
let g = UISwipeGestureRecognizer(target: self, action: s)
g.direction = d
view.addGestureRecognizer(g)
}
///Add four swipes for left right up down.
func addSwipesLRUD(_ l: Selector, _ r: Selector, _ u: Selector, _ d: Selector) {
addSwipe(.left, l)
addSwipe(.right, r)
addSwipe(.up, u)
addSwipe(.down, d)
}
}
I like @Fattie's approach (extension), which is what I've been using for tap gestures for a long time, except as an extension of UIView, instead of UIViewController. It is a great time saving convenience. Since you may not want to enable all four cartesian direction,s I default to nil for each selector to check for presence of parameters to make activation of gesture for any given direction optional.
I made the parameter names a little bit longer to speed up reverse engineering when skimming code and overloaded the function name, because why not?
extension UIView {
func addSwipeRecognizer(direction: UISwipeGestureRecognizer.Direction, target: Any, action: Selector) {
let recognizer = UISwipeGestureRecognizer(target: target, action: action)
recognizer.direction = direction
addGestureRecognizer(recognizer)
}
func addSwipeRecognizer(target: Any, left: Selector? = nil, right: Selector? = nil, up: Selector? = nil, down: Selector? = nil) {
if let left { addSwipeRecognizer(direction: .left, target: target, action: left) }
if let right { addSwipeRecognizer(direction: .right, target: target, action: right) }
if let up { addSwipeRecognizer(direction: .up, target: target, action: up) }
if let down { addSwipeRecognizer(direction: .down, target: target, action: down) }
}
func addTapRecognizer(tapNumber: Int, target: Any, action: Selector) {
let tap = UITapGestureRecognizer(target: target, action: action)
tap.numberOfTapsRequired = tapNumber
addGestureRecognizer(tap)
isUserInteractionEnabled = true
}
}
Usage:
@objc func viewSwipedLeft() { }
@objc func viewSwipedRight() { }
@objc func viewSwipedUp() { }
@objc func viewSwipedDown() { }
view.addSwipeRecognizer(target: self,
left: #selector(viewSwipedLeft),
right: #selector(viewSwipedRight),
up: #selector(viewSwipedUp),
down: #selector(viewSwipedDown))
Just a cooler swift syntax for Nate's answer:
[UISwipeGestureRecognizerDirection.right,
UISwipeGestureRecognizerDirection.left,
UISwipeGestureRecognizerDirection.up,
UISwipeGestureRecognizerDirection.down].forEach({ direction in
let swipe = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
swipe.direction = direction
self.view.addGestureRecognizer(swipe)
})
Easy. Just follow the code below and enjoy.
//SwipeGestureMethodUsing
func SwipeGestureMethodUsing ()
{
//AddSwipeGesture
[UISwipeGestureRecognizerDirection.right,
UISwipeGestureRecognizerDirection.left,
UISwipeGestureRecognizerDirection.up,
UISwipeGestureRecognizerDirection.down].forEach({ direction in
let swipe = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
swipe.direction = direction
window?.addGestureRecognizer(swipe)
})
}
//respondToSwipeGesture
func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer
{
switch swipeGesture.direction
{
case UISwipeGestureRecognizerDirection.right:
print("Swiped right")
case UISwipeGestureRecognizerDirection.down:
print("Swiped down")
case UISwipeGestureRecognizerDirection.left:
print("Swiped left")
case UISwipeGestureRecognizerDirection.up:
print("Swiped up")
default:
break
}
}
}
For Swift 5 it's updated
//Add in ViewDidLoad
let gesture = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.handleSwipe))
gesture.direction = .right
self.view.addGestureRecognizer(gesture)
//Add New Method
@objc func handleSwipe(sender: UISwipeGestureRecognizer) {
print("swipe direction is",sender.direction)
}
It can be done by simply declaring one function which will handle all your swipe UISwipeGestureRecognizer directions. Here is my code:
let swipeGestureRight = UISwipeGestureRecognizer(target: self, action:#selector(ViewController.respondToSwipeGesture(_:)) )
swipeGestureRight.direction = UISwipeGestureRecognizerDirection.right
self.view .addGestureRecognizer(swipeGestureRight)
let swipeGestureLeft = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGesture(_:)))
swipeGestureLeft.direction = UISwipeGestureRecognizerDirection.left
self.view.addGestureRecognizer(swipeGestureLeft)
let swipeGestureUp = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGesture(_:)))
swipeGestureUp.direction = UISwipeGestureRecognizerDirection.up
self.view.addGestureRecognizer(swipeGestureUp)
let swipeGestureDown = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGesture(_:)))
swipeGestureDown.direction = UISwipeGestureRecognizerDirection.down
self.view.addGestureRecognizer(swipeGestureDown)
Here is the function which will hande the swipedirection functionality:
func respondToSwipeGesture(_ sender: UIGestureRecognizer) {
if let swipeGesture = sender as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.right:
print("right swipe")
case UISwipeGestureRecognizerDirection.left:
print("leftSwipe")
case UISwipeGestureRecognizerDirection.up:
print("upSwipe")
case UISwipeGestureRecognizerDirection.down:
print("downSwipe")
default:
break
}
}
}
© 2022 - 2024 — McMap. All rights reserved.