Swipe Gesture gives NSException - ios

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)

Related

Swipe right to dismiss edge size

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

UISwipeGestureRecognizer on tvOS is giving me a EXC_BAD_ACCESS

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() {

How do I access finger location/movement information in a tableview?

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

UISwipeGestureRecognizer not working for swipe right

I'm working on an iOS app that's programmed in Swift and for some reason my app is unable to recognize/handle the swipe right gesture. Here is my code:
self.feedTableViewCell.productPreview.userInteractionEnabled = true
var swipeLeft = UISwipeGestureRecognizer(target: self, action: Selector("respondToSwipeGesture:"))
swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
self.feedTableViewCell.productPreview.addGestureRecognizer(swipeLeft)
var swipeRight = UISwipeGestureRecognizer(target: self, action: Selector("respondToSwipeGesture:"))
swipeRight.direction = UISwipeGestureRecognizerDirection.Right
self.feedTableViewCell.productPreview.addGestureRecognizer(swipeRight)
And here's my handler:
func respondToSwipeGesture(swipeGesture: UISwipeGestureRecognizer) {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.Left:
println("swipe left")
case UISwipeGestureRecognizerDirection.Right:
println("swipe right")
default:
println("Unknown gesture")
break
}
self.feedTableViewCell.pageControl.currentPage = self.currentFeedImage
}
I'm able to swipe to the left fine, but when I swipe right, the app crashes and I get an (lldp) error. Any help greatly appreciated!

Change iOS view / view controller using swipe gesture

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.

Resources