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() {
Related
In my app, there is a scrollView(pageScrollView) which is a snapchat like interface (https://www.youtube.com/watch?v=1_daE3IL_1s)
However, when I try to use below code to get the gesture on the scroll view, it cannot detect, can anyone help me on this??
let leftSwipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(leftSwipedByUser(_:)))
leftSwipeGesture.direction = .left
self.pageScrollView.addGestureRecognizer(leftSwipeGesture)
let rightSwipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(rightSwipedByUser(_:)))
rightSwipeGesture.direction = .right
self.pageScrollView.addGestureRecognizer(rightSwipeGesture)
And the action is below:
#objc func leftSwipedByUser(_ gesture:UISwipeGestureRecognizer) {
print("left swipe")
}
#objc func rightSwipedByUser(_ gesture:UISwipeGestureRecognizer) {
print("right swipe")
}
private let swipeUp: UISwipeGestureRecognizer = {
let swiper = UISwipeGestureRecognizer(target: self, action: #selector(movedUp))
swiper.direction = .up
return swiper
}()
private let swipeDown: UISwipeGestureRecognizer = {
let swiper = UISwipeGestureRecognizer(target: self, action: #selector(movedDown))
swiper.direction = .down
return swiper
}()
creating my swipeUp and swipeDown gesture recognizers
func movedUp(sender: UISwipeGestureRecognizer){
print("UP")
}
func movedDown(sender: UISwipeGestureRecognizer){
print("DOWN")
}
my functions to be called when the swipe is recieved
override func viewDidLoad() {
self.view.addGestureRecognizer(swipeUp)
self.view.addGestureRecognizer(swipeDown)
}
adding my gesture recognizers in viewDidLoad
my program runs, but nothing happens when i swipe, what am i doing wrong?
Thankyou everyone
Try this:
private lazy var swipeUp: UISwipeGestureRecognizer = {
let swiper = UISwipeGestureRecognizer(target: self, action: #selector(movedUp))
swiper.direction = .up
return swiper
}()
private lazy var swipeDown: UISwipeGestureRecognizer = {
let swiper = UISwipeGestureRecognizer(target: self, action: #selector(movedDown))
swiper.direction = .down
return swiper
}()
override func viewDidLoad()
{
super.viewDidLoad()
self.view.addGestureRecognizer(swipeUp)
self.view.addGestureRecognizer(swipeDown)
}
func movedUp(sender: UISwipeGestureRecognizer)
{
print("UP")
}
func movedDown(sender: UISwipeGestureRecognizer)
{
print("DOWN")
}
Have you tried func movedUp(sender: UIGestureRecognizer)
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)
xcode creates some warnings since I updated to 7.3.1: "No method declared with Objective-C selector ('nextPage')". I'm not sure what I need to do now. These are the two lines that produce the warning:
let leftSwipe = UISwipeGestureRecognizer (target: self, action: Selector("nextPage"))
let rightSwipe = UISwipeGestureRecognizer (target: self, action: Selector("nextPage"))
This is the entire VC code:
import Foundation
import UIKit
class VC1 : UIViewController {
class MyCustomNavigationController: UINavigationController {
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return (visibleViewController?.supportedInterfaceOrientations())!
}
override func shouldAutorotate() -> Bool {
return (visibleViewController?.shouldAutorotate())!
}
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Portrait
}
override func shouldAutorotate() -> Bool {
return true
}
override func viewDidLoad() {
super.viewDidLoad()
func nextPage(sender:UISwipeGestureRecognizer) {
switch sender.direction {
case UISwipeGestureRecognizerDirection.Left:
print("SWIPED LEFT", terminator: "")
self.performSegueWithIdentifier("seg1", sender: nil)
default:
break
}
let leftSwipe = UISwipeGestureRecognizer (target: self, action: Selector("nextPage"))
let rightSwipe = UISwipeGestureRecognizer (target: self, action: Selector("nextPage"))
leftSwipe.direction = .Left
rightSwipe.direction = .Right
view.addGestureRecognizer(leftSwipe)
view.addGestureRecognizer(rightSwipe)
}
}
}
I found this when researching:
Selector("funcName") was changed to #selector(ClassName.funcName)
I imagine that "funcName" would be nextPage, but what do I have to set as ClassName?
Unfortunately, xcode doesn't provide any suggestions to fix this. Hope you can help. Thanks.
You need to put nextPage out of viewDidLoad() and then #selector(VC1.nextPage)
func nextPage(sender:UISwipeGestureRecognizer) {
switch sender.direction {
case UISwipeGestureRecognizerDirection.Left:
print("SWIPED LEFT", terminator: "")
self.performSegueWithIdentifier("seg1", sender: nil)
default:
break
}
}
override func viewDidLoad() {
super.viewDidLoad()
let leftSwipe = UISwipeGestureRecognizer (target: self, action: #selector(VC1.nextPage))
let rightSwipe = UISwipeGestureRecognizer (target: self, action: #selector(VC1.nextPage))
}
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.