I am creating swift application and i am using UISwipegesture i am swiping up and down direction and its work perfectly but when usewr swipe up or down i will hide show view and it hide and show as expected but when swiping stopped i want that view show automatically
let me Show my code for better understanding
Code
videDidLoad()
let swipe = UISwipeGestureRecognizer(target: self, action:
#selector(respondToSwipeGesture(gesture:)))
swipe.direction = UISwipeGestureRecognizer.Direction.up
swipe.delegate = self
self.view.addGestureRecognizer(swipe)
let swipe1 = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture(gesture:)))
swipe1.direction = UISwipeGestureRecognizer.Direction.down
swipe1.delegate = self
self.view.addGestureRecognizer(swipe1)
#objc func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizer.Direction.up:
print("Swiped up")
viewFilter.isHidden = true
case UISwipeGestureRecognizer.Direction.down:
print("Swiped down")
viewFilter.isHidden = true
default:
break
}
}
}
here you can able to see that on up down direction i hide view but when swipe stop i want that again show that view so i cant under stand how to do that can any please help me
Use the UIGestureRecognizer.State appledoc
In your selector do something like this
#objc func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizer.Direction.up:
print("Swiped up")
viewFilter.isHidden = true
case UISwipeGestureRecognizer.Direction.down:
print("Swiped down")
viewFilter.isHidden = true
default:
break
}
// code for looking up which state the gesture currently is in.
switch swipeGesture.state {
case .ended, .failed:
viewFilter.isHidden = false
// list up other cases here
}
}
}
You can use state for gesture recognizer:
#objc func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizer.Direction.up:
print("Swiped up")
viewFilter.isHidden = true
case UISwipeGestureRecognizer.Direction.down:
print("Swiped down")
viewFilter.isHidden = true
default:
break
}
if swipeGesture.state == .ended {
viewFilter.isHidden = false
}
}
}
Get the ended state of UIGestureRecognizer and then show the view. See this doc
#objc func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
if swipeGesture.state == .ended {
viewFilter.isHidden = false
}
}
}
Related
Is it possible to increase edge size interactivePopGestureRecognizer of UINavigationController reacts user touches?
I.e. is it possible to make swipe right from middle of screen to move and pop current top Viewcontroller same way as from the left screen edge?
override func viewDidLoad() {
super.viewDidLoad()
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture))
swipeRight.direction = .right
self.view.addGestureRecognizer(swipeRight)
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture))
swipeLeft.direction = .left
self.view.addGestureRecognizer(swipeLeft)
}
// you can track all direction swipe like below, and to do that first you have to add gesture in the view you want to track.
#objc func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case .right:
// push pop here
print("Swiped right")
case .down:
print("Swiped down")
case .left:
// push pop here
print("Swiped left")
case .up:
print("Swiped up")
default:
break
}
}
}
Finally, found a solution to increase the edge touch size using this library: SwipeTransition
I'm trying to add gesture code for swipe up/down to my UIViewController in a tvOS app.
override func loadView() {
let swipeDown = UISwipeGestureRecognizer(
target: self,
action: #selector(self.respondToSwipeGesture)
)
swipeDown.direction = UISwipeGestureRecognizer.Direction.down
self.view.addGestureRecognizer(swipeDown)
let swipeUp = UISwipeGestureRecognizer(
target: self,
action: #selector(self.respondToSwipeGesture)
)
swipeUp.direction = UISwipeGestureRecognizer.Direction.up
self.view.addGestureRecognizer(swipeUp)
}
#objc private func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizer.Direction.down:
print("Swiped down")
case UISwipeGestureRecognizer.Direction.up:
print("Swiped up")
default:
break
}
}
}
When I run this I get a
Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffee7146fe8)
on the let swipeDown line in the loadView function.
I'm using Xcode 11. What am I missing here?
Yeah, missing super.loadView() in your code also, you should create the gestures in viewDidLoad instead loadView.
override func loadView() {
super.loadView() // ---> In this case always in the second line
let swipeDown = UISwipeGestureRecognizer(
target: self,
action: #selector(self.respondToSwipeGesture)
)
swipeDown.direction = UISwipeGestureRecognizer.Direction.down
self.view.addGestureRecognizer(swipeDown)
let swipeUp = UISwipeGestureRecognizer(
target: self,
action: #selector(self.respondToSwipeGesture)
)
swipeUp.direction = UISwipeGestureRecognizer.Direction.up
self.view.addGestureRecognizer(swipeUp)
}
#objc private func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizer.Direction.down:
print("Swiped down")
case UISwipeGestureRecognizer.Direction.up:
print("Swiped up")
default:
break
}
}
}
You have overridden the wrong method:
override func loadView() {
You meant:
override func viewDidLoad() {
So what I'm trying to do is have each cell in my tableview have the ability to be swiped left or right. If I can grab the finger location/translation I could change the content's x anchors and bingo bango, but I'm having a real hard time figuring it out. if anyone can help, it would be so so rad
You can check the apple documentation about UISwipeGestureRecognizer , The default direction is right, also explore this function UISwipeGestureRecognizerDirection it return you the direction is in right, left, top , or bottom . Here is demonstration
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
}
}
}
i am new to swift programming. So i was trying to add swipe gestures to my application for iOS. But when i try to swipe it stops app and says NSException caught. Here is the code, i have written.
override func viewDidLoad() {
super.viewDidLoad()
textLabels.append(label0)
textLabels.append(label1)
textLabels.append(label2)
textLabels.append(label3)
textLabels.append(label4)
textLabels.append(label5)
textLabels.append(label6)
textLabels.append(label7)
textLabels.append(label8)
textLabels.append(label9)
textLabels.append(label10)
textLabels.append(label11)
textLabels.append(label12)
textLabels.append(label13)
textLabels.append(label14)
textLabels.append(label15)
setupInitial()
print(textLabels.count)
var request = GADRequest()
request.testDevices = [kGADSimulatorID]
bannerView.adUnitID = "ca-app-pub-8950283126375215/1694851281"
bannerView.rootViewController = self
bannerView.load(request)
createAndLoadInterstitial()
let swipeRight = UISwipeGestureRecognizer(target: self, action: Selector(("gesture:")))
swipeRight.direction = UISwipeGestureRecognizerDirection.right
self.view.addGestureRecognizer(swipeRight)
let swipeDown = UISwipeGestureRecognizer(target: self, action: Selector(("gesture:")))
swipeDown.direction = UISwipeGestureRecognizerDirection.down
self.view.addGestureRecognizer(swipeDown)
//setView(view: hideView, hidden: false)
}
func gesture(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
}
}
}
I also tried adding UIGestureRecognizerDelegate to my viewController class like this
class ViewController: UIViewController, UIGestureRecognizerDelegate
but it did not work. I also added UIGestureRecognizer. Please help me, i am really stuck here. I am using Xcode 8 and OSX El Capitan. Thanks in advance.
The stringly-typed Selector() has been deprecated for the new #selector().
Update your action to #selector(gesture(gesture:)) and that should fix it
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(gesture(gesture:)))
swipeRight.direction = UISwipeGestureRecognizerDirection.right
self.view.addGestureRecognizer(swipeRight)
I have a screen with 6 buttons. All the buttons are connected to one IBAction. They are tagged and I use a switch statement to determine which one was tapped.
How can I add a Long and Tap gesture to each button? So for example when I tap button 1 it know if its a long gesture or a tap gesture?
So if I tap the button will do something different then when I long press.
Thanks.
#IBAction func playPauseAudioButton(sender: UIButton) {
switch sender.tag {
case 1:
//Tap Gesture
//Long Gesture
//I need this for every button
print("1")
case 2:
print("2")
case 3:
print("3")
case 4:
case 5:
print("5")
case 6:
print("6")
default:
print("Default")
}
}
Do it like this
#IBAction func playPauseAudioButton(sender: AnyObject) {
let tapGesture = UITapGestureRecognizer(target: self, action: "normalTap:")
let longGesture = UILongPressGestureRecognizer(target: self, action: "longTap:")
tapGesture.numberOfTapsRequired = 1
sender.addGestureRecognizer(tapGesture)
sender.addGestureRecognizer(longGesture)
}
func normalTap(sender : UIGestureRecognizer) {
let recognizer: UIGestureRecognizer = sender
let tag: Int = recognizer.view!.tag
switch tag {
case 1:
// Do some action for button 1
print("1")
case 2:
print("2")
case 3:
print("3")
case 4:
print("4")
case 5:
print("5")
case 6:
print("6")
default:
print("Default")
}
}
func longTap(sender : UIGestureRecognizer) {
let recognizer: UIGestureRecognizer = sender
let tag: Int = recognizer.view!.tag
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
}
switch tag {
case 1:
// Do some action for button 1
print("1")
case 2:
print("2")
case 3:
print("3")
case 4:
print("4")
case 5:
print("5")
case 6:
print("6")
default:
print("Default")
}
}
Define two IBActions and set one Gesture Recognizer to each of them. This way you can perform two different actions for each gesture.
You can set each Gesture Recognizer to different IBActions in the interface builder.
#IBAction func tapped(sender: UITapGestureRecognizer)
{
println("tapped")
//Your animation code.
}
#IBAction func longPressed(sender: UILongPressGestureRecognizer)
{
println("longpressed")
//Different code
}
Through code without interface builder
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tapped:")
self.view.addGestureRecognizer(tapGestureRecognizer)
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: "longPressed:")
self.view.addGestureRecognizer(longPressRecognizer)
func tapped(sender: UITapGestureRecognizer)
{
println("tapped")
}
func longPressed(sender: UILongPressGestureRecognizer)
{
println("longpressed")
}
Hope this helps you.