tapgesture recognizer swift
Swipe gesto en Swift 3 (7)
En, Swift 3 puedes probar esto.
let swipeRightOrange = UISwipeGestureRecognizer(target: self, action:#selector(slideToRightWithGestureRecognizer))
swipeRightOrange.direction = UISwipeGestureRecognizerDirection.Right;
let swipeLeftOrange:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(slideToLeftWithGestureRecognizer))
swipeLeftOrange.direction = UISwipeGestureRecognizerDirection.Left;
@IBAction func slideToLeftWithGestureRecognizer(gestureRecognizer:UISwipeGestureRecognizer)
{
viewOrange.backgroundColor = UIColor.blueColor()
}
@IBAction func slideToRightWithGestureRecognizer
(gestureRecognizer:UISwipeGestureRecognizer)
{
viewOrange.backgroundColor = UIColor.lightGrayColor()
}
Estoy tratando de hacer que un UISwipeGestureRecognizer funcione en Swift 3, el deslizamiento por defecto a la derecha funciona correctamente aunque no hacia arriba o hacia la izquierda.
Lo he probado por control arrastrando una acción.
@IBAction func hole(_ recognizer: UISwipeGestureRecognizer) {
if (recognizer.direction == UISwipeGestureRecognizerDirection.left)
{
print("left")
}else if recognizer.direction == .right {
print("right")
}else {
print("other")
}
}
Y, en ViewDidLoad
//gesture recognisers
let swipeRight = UISwipeGestureRecognizer(target: self, action: "holeSwiped:")
swipeRight.direction = UISwipeGestureRecognizerDirection.right
self.view.addGestureRecognizer(swipeRight)
let swipeLeft = UISwipeGestureRecognizer(target: self, action: "holeSwiped:")
swipeLeft.direction = UISwipeGestureRecognizerDirection.left
self.view.addGestureRecognizer(swipeLeft)
a mi metodo
func holeSwiped(gesture: UISwipeGestureRecognizer)
{
if let swipeGesture = gesture as? UISwipeGestureRecognizer{
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.right:
print("right swipe")
case UISwipeGestureRecognizerDirection.left:
print("left swipe")
default:
print("other swipe")
}
}
}
Ahora, ninguno de los swipes está funcionando, excepto el derecho predeterminado. ¿Algunas ideas?
Estaba teniendo el mismo problema. En realidad, cualquier golpe simplemente estrelló mi código (de Rob Percival, ¿verdad?). Así que descargué su código terminado, lo abrí en XCode 8 y lo dejé convertir a Swift 3. Se cambiaron dos puntos.
In ViewDidLoad:
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.swiped(_:)))
Y dentro de la función:
func swiped(_ gesture: UIGestureRecognizer)
Me imagino que lo que está sucediendo es porque primero está agregando su reconocedor de gesto de golpe derecho, es decir, está tomando todos los gestos y no pasa ninguno al reconocedor de gesto de golpe de izquierda.
Mi sugerencia sería mirar require(toFail:)
y sus métodos similares .
Esto le permitiría definir qué gestores de gestos deben fallar antes de que otros reciban gestos. Por ejemplo, podría decirle a su reconocedor de gesto de deslizamiento izquierdo que requiere que el reconocedor de gesto de deslizamiento derecho falle. Luego, si el reconocedor de gesto de deslizamiento derecho recibe un gesto pero no es un barrido correcto, pasará el gesto al reconocedor de gesto de deslizamiento a la izquierda.
Paso 1: agregue gestos de deslizamiento en el método viewDidLoad () .
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)
}
Paso 2: Verifique la detección de gestos en el método handleGesture ()
func handleGesture(gesture: UISwipeGestureRecognizer) -> Void {
if gesture.direction == UISwipeGestureRecognizerDirection.right {
print("Swipe Right")
}
else if gesture.direction == UISwipeGestureRecognizerDirection.left {
print("Swipe Left")
}
else if gesture.direction == UISwipeGestureRecognizerDirection.up {
print("Swipe Up")
}
else if gesture.direction == UISwipeGestureRecognizerDirection.down {
print("Swipe Down")
}
}
Espero que esto ayude a alguien.
Swift 4, Xcode 9.2 Este código le permitirá reconocer la dirección de deslizamiento del usuario en todo el ViewController (toda la pantalla del iPhone, no específica de un botón) Gracias @ Ram-madhavan
import UIKit
class ViewController: UIViewController {
//Label to show test and see Gesture direction.
@IBOutlet weak var swipeDirectionLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
//Defining the Various Swipe directions (left, right, up, down)
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(self.handleGesture(gesture:)))
swipeLeft.direction = .left
self.view.addGestureRecognizer(swipeLeft)
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.handleGesture(gesture:)))
swipeRight.direction = .right
self.view.addGestureRecognizer(swipeRight)
let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(self.handleGesture(gesture:)))
swipeUp.direction = .up
self.view.addGestureRecognizer(swipeUp)
let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(self.handleGesture(gesture:)))
swipeDown.direction = .down
self.view.addGestureRecognizer(swipeDown)
}
//Function to Print to console Swipe Direction, and Change the label to show the directions. The @objc before func is a must, since we are using #selector (above). You can add to the function, in my case, I''ll add a sound, so when someone flips the page, it plays a page sound.
@objc func handleGesture(gesture: UISwipeGestureRecognizer) -> Void {
if gesture.direction == UISwipeGestureRecognizerDirection.right {
print("Swipe Right")
swipeDirectionLabel.text = "Swiped Right"
}
else if gesture.direction == UISwipeGestureRecognizerDirection.left {
print("Swipe Left")
swipeDirectionLabel.text = "Swiped Left"
}
else if gesture.direction == UISwipeGestureRecognizerDirection.up {
print("Swipe Up")
swipeDirectionLabel.text = "Swiped Up"
}
else if gesture.direction == UISwipeGestureRecognizerDirection.down {
print("Swipe Down")
swipeDirectionLabel.text = "Swiped Down"
}
}
}
func holeSwiped(gesture: UISwipeGestureRecognizer)
{
if let swipeGesture = gesture as? UISwipeGestureRecognizer{
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.right:
NSLog("right swipe")
case UISwipeGestureRecognizerDirection.left:
NSLog("left swipe")
default:
NSLog("other swipe")
}
}
}
var swipeGesture = UISwipeGestureRecognizer()
Mira y configura IBOutlet:
@IBOutlet weak var viewSwipe: UIView!
Escribe este código bonito en viewDidLoad ()
let direction: [UISwipeGestureRecognizerDirection] = [.up, .down, .left, .right]
for dir in direction{
swipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(self.swipeView(_:)))
viewSwipe.addGestureRecognizer(swipeGesture)
swipeGesture.direction = dir
viewSwipe.isUserInteractionEnabled = true
viewSwipe.isMultipleTouchEnabled = true
}
Ahora, este es el método que está llamando cuando se reconoce el gesto de deslizar.
@objc func swipeView(_ sender:UISwipeGestureRecognizer){
UIView.animate(withDuration: 1.0) {
if sender.direction == .right{
self.viewSwipe.frame = CGRect(x: self.view.frame.size.width - self.viewSwipe.frame.size.width, y: self.viewSwipe.frame.origin.y, width: self.viewSwipe.frame.size.width, height: self.viewSwipe.frame.size.height)
}else if sender.direction == .left{
self.viewSwipe.frame = CGRect(x: 0, y: self.viewSwipe.frame.origin.y, width: self.viewSwipe.frame.size.width, height: self.viewSwipe.frame.size.height)
}else if sender.direction == .up{
self.viewSwipe.frame = CGRect(x: self.view.frame.size.width - self.viewSwipe.frame.size.width, y: 0, width: self.viewSwipe.frame.size.width, height: self.viewSwipe.frame.size.height)
}else if sender.direction == .down{
self.viewSwipe.frame = CGRect(x: self.view.frame.size.width - self.viewSwipe.frame.size.width, y: self.view.frame.size.height - self.viewSwipe.frame.size.height, width: self.viewSwipe.frame.size.width, height: self.viewSwipe.frame.size.height)
}
}
}
100% trabajando en mi proyecto y probado.