resumen pelicula libro latino fahrenheit español bradbury analisis swift swipe direction uiswipegesturerecognizer

swift - pelicula - Cómo reconocer el deslizamiento en las 4 direcciones



fahrenheit 451 ray bradbury (10)

Necesito usar deslizar para reconocer el gesto deslizar hacia abajo y luego hacia la derecha. Pero en UISwipeGestureRecognizer rápido tiene una dirección correcta predeterminada. Y no sé cómo hacer esto para usar otras direcciones ...


Del guión gráfico:

  1. Agregue cuatro reconocedores de gestos de barrido a su vista.
  2. Configure cada uno con la dirección del objetivo desde el inspector de atributos. Puede seleccionar derecha, izquierda, arriba o abajo
  3. Uno por uno, seleccione el reconocedor de gestos de barrido, controle + arrastre a su controlador de vista. Inserte el nombre (digamos leftGesture, rightGesture, upGesture y downGesture), cambie la conexión a: Acción y escriba a: UISwipeGestureRecognizer

Desde su 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") }


En Swift 4.0 y Xcode 9.3

Añadir delegado de animación, CAAnimationDelegate

//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) }) } //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) }) }


Fácil. Solo sigue el siguiente código y disfruta.

//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 } } }


Parece que las cosas han cambiado últimamente. En XCode 7.2, el siguiente enfoque funciona:

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) }

Probado en Simulator en iOS 8.4 y 9.2 y en el dispositivo real en 9.2.

O bien, usando la práctica extensión de mlcollard here :

let swipeGesture = UISwipeGestureRecognizer() { print("Gesture recognized !") } swipeGesture.direction = [.Down, .Up] self.view.addGestureRecognizer(swipeGesture)


Se puede hacer simplemente declarando una función que manejará todas las direcciones UISwipeGestureRecognizer de deslizamiento. Aquí está mi código:

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)

Aquí está la función que manejará la funcionalidad de la dirección swiped:

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 } } }


Solo quería contribuir con esto, se ve más elegante al final:

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) }


Solo una sintaxis rápida más fresca para la respuesta de Nate:

[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) })


UISwipeGestureRecognizer tiene una propiedad de direction que tiene la siguiente definición:

var direction: UISwipeGestureRecognizerDirection

La dirección permitida del deslizamiento para este reconocedor de gestos.

El problema con Swift 3.0.1 (y debajo) es que incluso si UISwipeGestureRecognizerDirection ajusta a OptionSet , el siguiente fragmento se compilará pero no generará ningún resultado esperado positivo:

// This compiles but does not work let gesture = UISwipeGestureRecognizer(target: self, action: #selector(gestureHandler)) gesture.direction = [.right, .left, .up, .down] self.addGestureRecognizer(gesture)

Como solución alternativa, deberá crear un UISwipeGestureRecognizer para cada direction deseada.

El siguiente código de Playground muestra cómo implementar varios UISwipeGestureRecognizer para la misma UIView y el mismo selector utilizando el método de map de Array:

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


Necesita tener un UISwipeGestureRecognizer para cada dirección. Es un poco extraño porque la propiedad UISwipeGestureRecognizer.direction es una máscara de bits de estilo de opciones, pero cada reconocedor solo puede manejar una dirección. Puede enviarlos todos al mismo controlador si lo desea, y ordenarlos allí, o enviarlos a diferentes controladores. Aquí hay una implementación:

override func viewDidLoad() { super.viewDidLoad() var swipeRight = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:") swipeRight.direction = UISwipeGestureRecognizerDirection.Right self.view.addGestureRecognizer(swipeRight) var swipeDown = UISwipeGestureRecognizer(target: self, action: "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 } } }

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 } } }


Apple Swift versión 3.1 - Xcode versión 8.3 (8E162)

La manera práctica del enfoque de Alexandre Cassagne

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") } }