In my container controller, sometimes I have to add a gesture recognizer to a view to handle things. Is it possible to fail all the other gestures on said view and only execute the added gesture?
Option 1:
Right now, all the individual gestures are set to fail when the new gesture has been detected, but it doesn't seem very efficient to do it this way.
Option 2:
Another way is get the array of gestures of said view and set enabled to false for all the gestures inside the array.
What I'd like to do is set the delegate of the new gesture to the container controller and use one of the delegate methods to fail all other gestures when the new gesture has been detected.
If you want to remove other gesture recogniser and want keep one gesture which user used then you can do by detecting the direction of that gesture and then you can remove all other gestures and you can keep used gesture.
Here is complete working code:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let swipeRight = UISwipeGestureRecognizer(target: self, action: Selector("rightSwiped:"))
swipeRight.direction = UISwipeGestureRecognizerDirection.Right
self.view.addGestureRecognizer(swipeRight)
let swipeLeft = UISwipeGestureRecognizer(target: self, action: Selector("leftSwiped:"))
swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
self.view.addGestureRecognizer(swipeLeft)
let swipeDown = UISwipeGestureRecognizer(target: self, action: Selector("downSwiped:"))
swipeDown.direction = UISwipeGestureRecognizerDirection.Down
self.view.addGestureRecognizer(swipeDown)
let swipeUp = UISwipeGestureRecognizer(target: self, action: Selector("upSwiped:"))
swipeUp.direction = UISwipeGestureRecognizerDirection.Up
self.view.addGestureRecognizer(swipeUp)
}
func rightSwiped(recognizer: UISwipeGestureRecognizer)
{
println("right swiped ")
manageGesture(recognizer)
}
func manageGesture(recognizer: UISwipeGestureRecognizer) {
//First detect which gesture is used
if recognizer.direction == UISwipeGestureRecognizerDirection.Right {
println("Right")
if let recognizers = view.gestureRecognizers {
for recognizer in recognizers {
if recognizer.direction == UISwipeGestureRecognizerDirection.Right {
println("Called")
}else {
view.removeGestureRecognizer(recognizer as! UIGestureRecognizer)
}
}
}
}else if recognizer.direction == UISwipeGestureRecognizerDirection.Left {
println("Left")
if let recognizers = view.gestureRecognizers {
for recognizer in recognizers {
if recognizer.direction == UISwipeGestureRecognizerDirection.Left {
println("Called")
}else {
view.removeGestureRecognizer(recognizer as! UIGestureRecognizer)
}
}
}
}else if recognizer.direction == UISwipeGestureRecognizerDirection.Up {
println("Up")
if let recognizers = view.gestureRecognizers {
for recognizer in recognizers {
if recognizer.direction == UISwipeGestureRecognizerDirection.Up {
println("Called")
}else {
view.removeGestureRecognizer(recognizer as! UIGestureRecognizer)
}
}
}
}else if recognizer.direction == UISwipeGestureRecognizerDirection.Down {
println("Down")
if let recognizers = view.gestureRecognizers {
for recognizer in recognizers {
if recognizer.direction == UISwipeGestureRecognizerDirection.Down {
println("Called")
}else {
view.removeGestureRecognizer(recognizer as! UIGestureRecognizer)
}
}
}
}
}
func leftSwiped(recognizer: UISwipeGestureRecognizer)
{
println("left swiped ")
manageGesture(recognizer)
}
func downSwiped(recognizer: UISwipeGestureRecognizer)
{
println("down swiped ")
manageGesture(recognizer)
}
func upSwiped(recognizer: UISwipeGestureRecognizer)
{
println("Up swiped ")
manageGesture(recognizer)
}
}
Hope this is what you need.
Related
I'm having a little trouble with the gestures.
I'm trying to use both tap and long press on the same button, so I've used
#IBAction func xxx (sender: UITapGestureRecognizer)
and
#IBAction func xxx (sender: UILongPressGestureRecognizer)
but my button seems to react to both functions when I tap. What might be wrong?
func long(longpress: UIGestureRecognizer){
if(longpress.state == UIGestureRecognizerState.Ended){
homeScoreBool = !homeScoreBool
}else if(longpress.state == UIGestureRecognizerState.Began){
print("began")
}
}
Hard to say what´s not working with your code, with the only two rows that you have provided, but I would recommend you to do it in this way instead:
Create an outlet to your button instead
#IBOutlet weak var myBtn: UIButton!
And in your viewDidLoad() add the gestures to the buttons
let tapGesture = UITapGestureRecognizer(target: self, action: "normalTap")
let longGesture = UILongPressGestureRecognizer(target: self, action: "longTap:")
tapGesture.numberOfTapsRequired = 1
myBtn.addGestureRecognizer(tapGesture)
myBtn.addGestureRecognizer(longGesture)
And then create the actions to handle the taps
func normalTap(){
print("Normal tap")
}
func longTap(sender : UIGestureRecognizer){
print("Long tap")
if sender.state == .Ended {
print("UIGestureRecognizerStateEnded")
//Do Whatever You want on End of Gesture
}
else if sender.state == .Began {
print("UIGestureRecognizerStateBegan.")
//Do Whatever You want on Began of Gesture
}
}
Swift 3.0 version:
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.normalTap))
let longGesture = UILongPressGestureRecognizer(target: self, action: Selector(("longTap:")))
tapGesture.numberOfTapsRequired = 1
myBtn.addGestureRecognizer(tapGesture)
myBtn.addGestureRecognizer(longGesture)
func normalTap(){
print("Normal tap")
}
func longTap(sender : UIGestureRecognizer){
print("Long tap")
if sender.state == .ended {
print("UIGestureRecognizerStateEnded")
//Do Whatever You want on End of Gesture
}
else if sender.state == .began {
print("UIGestureRecognizerStateBegan.")
//Do Whatever You want on Began of Gesture
}
}
Updated syntax for Swift 5.x:
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(normalTap))
button.addGestureRecognizer(tapGesture)
let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(longTap))
button.addGestureRecognizer(longGesture)
#objc func normalTap(_ sender: UIGestureRecognizer){
print("Normal tap")
}
#objc func longTap(_ sender: UIGestureRecognizer){
print("Long tap")
if sender.state == .ended {
print("UIGestureRecognizerStateEnded")
//Do Whatever You want on End of Gesture
}
else if sender.state == .began {
print("UIGestureRecognizerStateBegan.")
//Do Whatever You want on Began of Gesture
}
}
I have a view that contains a counter.
I have implemented two different gesture recognizers,
a UISwipeGesture to increase the count by one,
and a UIPanGesture in order to increase the count by multiple numbers every
time the user swipes up.
Both gestures work, but my problem is they don't work at the same time.
I want them to work alternatively, for example if I do small swipes the
counter should increase by one, and if I keep dragging upwards the counter
should increase by multiple numbers.
Here's part of my code:
```
override func viewDidLoad() {
super.viewDidLoad()
setupSwipeGestures()
setupPanGestures()
}
private func setupSwipeGestures() {
let swipeUp = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
let swipeDown = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
swipeUp.direction = .Up
swipeDown.direction = .Down
circleView.addGestureRecognizer(swipeUp)
circleView.addGestureRecognizer(swipeDown)
}
private func setupPanGestures() {
let panGesture = UIPanGestureRecognizer(target: self, action: Selector("handleThePan:"))
circleView.addGestureRecognizer(panGesture)
}
extension UIPanGestureRecognizer {
func isDown(circleView: UIView) -> Bool {
let velocity : CGPoint = velocityInView(circleView)
if velocity.y < 0 {
print("ex Gesture went up")
return false
} else {
print("ex Gesture went down")
return true
}
}
}
```
P.S: The methods "handleSwipe" and "handlePan" contain the logic of the counter.
A swipe and a pan gesture can not be recognized at the same time because a swipe gesture is just a special type of pan that has to be faster, short lived, and in one direction.
What you should do is setup the pan gestures to require the swipe gesture to fail before they can start. You will need to set each of the UISwipeGestureRecognizer to be a requireGestureRecognizerToFail on the UIPanGestureRecognizer
override func viewDidLoad() {
super.viewDidLoad()
let swipeGestures = setupSwipeGestures()
setupPanGestures(swipeGestures: swipeGestures)
}
private func setupSwipeGestures() -> [UISwipeGestureRecognizer] {
let swipeUp = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
let swipeDown = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
swipeUp.direction = .Up
swipeDown.direction = .Down
circleView.addGestureRecognizer(swipeUp)
circleView.addGestureRecognizer(swipeDown)
return [swipeUp, swipeDown]
}
private func setupPanGestures(swipeGestures: [UISwipeGestureRecognizer]) {
let panGesture = UIPanGestureRecognizer(target: self, action: Selector("handleThePan:"))
for swipeGeature in swipeGestures {
panGesture.requireGestureRecognizerToFail(swipeGesture)
}
circleView.addGestureRecognizer(panGesture)
}
Swift 3
override func viewDidLoad() {
super.viewDidLoad()
let swipeGestures = setupSwipeGestures()
setupPanGestures(swipeGestures: swipeGestures)
}
private func setupSwipeGestures() -> [UISwipeGestureRecognizer] {
let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(self.handleSwipes))
let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(self.handleSwipes))
swipeUp.direction = .up
swipeDown.direction = .down
circleView.addGestureRecognizer(swipeUp)
circleView.addGestureRecognizer(swipeDown)
return [swipeUp, swipeDown]
}
private func setupPanGestures(swipeGestures: [UISwipeGestureRecognizer]) {
let panGesture = UIPanGestureRecognizer.init(target: self, action:#selector(self.handleThePanUp))
for swipeGesture in swipeGestures {
panGesture.require(toFail: swipeGesture)
}
circleView.addGestureRecognizer(panGesture)
}
Is it possible to capture touchdown and longpress with UIGestureRecognizer?
func gestureSetup() {
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tapButton:")
tapTempoButton.addGestureRecognizer(tapGestureRecognizer)
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: "longPressed:")
longPressRecognizer.minimumPressDuration = 1.0
tapTempoButton.addGestureRecognizer(longPressRecognizer)
}
func tapButton(sender: UITapGestureRecognizer) {
// On touch down update UI and play sound
}
example: Gestures on UITextLabel
//Called on Label
let rotationGesture = UIRotationGestureRecognizer(target: self, action: "handleRotation:")
rotationGesture.delegate = self
let tapToDeleteTextGesture = UITapGestureRecognizer(target: self, action: "tapToDeleteText:")
tapToDeleteTextGesture.numberOfTapsRequired = 2
textLabel?.addGestureRecognizer(rotationGesture)
textLabel?.addGestureRecognizer(tapToDeleteTextGesture)
func handleRotation(recognizer: UIRotationGestureRecognizer){
let state = recognizer.state
if (state == UIGestureRecognizerState.Began || state == UIGestureRecognizerState.Changed){
if let view = recognizer.view {
view.transform = CGAffineTransformRotate(view.transform, recognizer.rotation)
recognizer.rotation = 0
}
}
}
func tapToDeleteText(recognizer: UITapGestureRecognizer){
if let view = recognizer.view {
view.removeFromSuperview()
}
}
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
Add add the longPressGesture and you'll be set.
How can I implement a swipe gesture to change view to and fro?
The best example I've seen so far is the Soundcloud application but I couldn't figure out how to make it work.
Compatible with Swift 5
override func viewDidLoad()
{
super.viewDidLoad()
let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:)))
let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:)))
leftSwipe.direction = .left
rightSwipe.direction = .right
view.addGestureRecognizer(leftSwipe)
view.addGestureRecognizer(rightSwipe)
}
#objc func handleSwipes(_ sender: UISwipeGestureRecognizer)
{
if sender.direction == .left
{
print("Swipe left")
// show the view from the right side
}
if sender.direction == .right
{
print("Swipe right")
// show the view from the left side
}
}
Use this code...
override func viewDidLoad() {
super.viewDidLoad()
var swipeRight = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
swipeRight.direction = UISwipeGestureRecognizerDirection.Right
self.view.addGestureRecognizer(swipeRight)
}
func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.Right:
println("Swiped right")
//change view controllers
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let resultViewController = storyBoard.instantiateViewControllerWithIdentifier("StoryboardID") as ViewControllerName
self.presentViewController(resultViewController, animated:true, completion:nil)
default:
break
}
}
}
You can a UISwipeGestureRecognizer to your UIView and add to this gesture a target and an action to perform when the gesture occurs
var swipeGesture = UISwipeGestureRecognizer(target: self, action: "doSomething")
myView.addGestureRecognizer(swipeGesture)
func doSomething() {
// change your view's frame here if you want
}
This tutorial might be helpful to you: http://www.avocarrot.com/blog/implement-gesture-recognizers-swift/
Basically, you'll need to add a gesture recognizer to your view that listens for swipe gestures. Then when it detects a swipe, push to the next view.
So my game uses swipe gestures, in my didMoveToView() function I have these gestures initialized:
let swipeRight = UISwipeGestureRecognizer()
swipeRight.direction = UISwipeGestureRecognizerDirection.Right
self.view?.addGestureRecognizer(swipeRight)
let swipeLeft = UISwipeGestureRecognizer()
swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
self.view?.addGestureRecognizer(swipeLeft)
let swipeUp = UISwipeGestureRecognizer()
swipeUp.direction = UISwipeGestureRecognizerDirection.Up
self.view?.addGestureRecognizer(swipeUp)
let swipeDown = UISwipeGestureRecognizer()
swipeDown.direction = UISwipeGestureRecognizerDirection.Down
self.view?.addGestureRecognizer(swipeDown)
Problem is when I move to my GameOver scene, and I swipe, it crashes my app. I noticed someone had posted something similar and got this as an answer
override func willMoveFromView(view: SKView) {
for recognizer in self.view.gestureRecognizers! {
self.view.removeGestureRecognizer(recognizer)
}
}
Still not quite sure how to implement this and/or remove the gestures from the scene before switching to game over. Can anyone help?
The following removes all swipe gesture recognizers from the view:
override func willMoveFromView(view: SKView) {
if let gestures = view.gestureRecognizers {
for gesture in gestures {
if let recognizer = gesture as? UISwipeGestureRecognizer {
view.removeGestureRecognizer(recognizer)
}
}
}
}
You are removing all kind of gesture, try this:
if([recognizer isKindOfClass:[UISwipeGestureRecognizer class]]) {
[self removeGestureRecognizer:recognizer];
}
Hope this helps.. :)
Replace UIGestureRecognizer with UISwipeGestureRecognizer if that's what you want to remove.
extension SKView {
func removeAllGestureRecognizers() {
if let objects = gestureRecognizers {
for object in objects {
if let gestureRecognizer = object as? UIGestureRecognizer {
removeGestureRecognizer(gestureRecognizer)
}
}
}
}
}