Hii everyone i'm just shifting from xcode8 to xcode9
and when i tryed to use gesture recognizer they xcode9 is showing some error
argument of #selector refers to instate method swipe(gesture) that is not >
expose to obj c
and here my code
override func viewDidLoad() {
super.viewDidLoad()
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(swipe(gestuer:)))
swipeLeft.direction = .left
self.view.addGestureRecognizer(swipeLeft)
}
func swipe(gestuer: UISwipeGestureRecognizer) {
if gestuer.direction == .left {
print("this is left swipe")
}
}
so is it xcode problem of something else
You need to use the #objc attribute on swipe(gestuer:) to use it with #selector.
#objc func swipe(gestuer: UISwipeGestureRecognizer) {
if gestuer.direction == .left {
print("this is left swipe")
}
}
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")
}
Hi i want to add tap gesture on UILabel inside a UITableViewCell . here i have implemented the code like this.
override func awakeFromNib()
{
super.awakeFromNib()
let tap = UITapGestureRecognizer(target: self, action:
#selector(self.tapFunction))
label.isUserInteractionEnabled = true
label.addGestureRecognizer(tap)
}
func tapFunction(sender:UITapGestureRecognizer) {
print("tap working")
}
but it is giving me error like this
fatal error: unexpectedly found nil while unwrapping an Optional value
Can anyone tell me what is wrong in this?
This should be
let tap = UITapGestureRecognizer(target: self, action:#selector(self.tapFunction(_:)))
If you use Swift 4 add #objc
#objc func tapFunction(sender:UITapGestureRecognizer) {
print("tap working")
}
its working now with the same code . Just i deleted the cell and created a new UITableviewCell. i guess it may be xcode bug
override func awakeFromNib()
{
super.awakeFromNib()
let tap = UITapGestureRecognizer(target: self, action:
#selector(self.tapFunction))
label.isUserInteractionEnabled = true
label.addGestureRecognizer(tap)
}
func tapFunction(sender:UITapGestureRecognizer) {
print("tap working")
}
I want to navigate between my tab bar using swipe gestures. What is the easiest way to do that? I tried something like this...
import UIKit
class postAdViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var leftSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
view.addGestureRecognizer(leftSwipe)
}
func handleSwipes(sender:UISwipeGestureRecognizer) {
if (sender.direction == .left) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "favourireviewcontroller") as! UIViewController
self.present(vc, animated: true, completion: nil)
}
if (sender.direction == .right) {
}
}
If I try to swipe right nothing happens. The app crashes when swiping left the following error message
unrecognized selector sent to instance 0x7f924380a730
Well if you want to navigate through you tabBar you should implement a swipeGestureRecognizer for .left and .right and then work with the tabBarController?.selectedIndex, something like this:
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(swiped))
swipeRight.direction = UISwipeGestureRecognizerDirection.right
self.view.addGestureRecognizer(swipeRight)
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(swiped))
swipeLeft.direction = UISwipeGestureRecognizerDirection.left
self.view.addGestureRecognizer(swipeLeft)
func swiped(_ gesture: UISwipeGestureRecognizer) {
if gesture.direction == .left {
if (self.tabBarController?.selectedIndex)! < 2 { // set your total tabs here
self.tabBarController?.selectedIndex += 1
}
} else if gesture.direction == .right {
if (self.tabBarController?.selectedIndex)! > 0 {
self.tabBarController?.selectedIndex -= 1
}
}
}
Here's a Swift4 example of swiping left and right right through a TabBar controller.
Things I don't like about this solution:
- seems like you should be able to register the handler once and distinguish the direction within the handler itself but I wasn't able to easily do that.
- I'm also curious how to do a gesture that includes a drag for visual effect. I'm think I need to include a PanGesture as well in order to pull that off.
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
self.view.addGestureRecognizer(leftSwipe)
self.view.addGestureRecognizer(rightSwipe)
}
And then the handler:
#objc func handleSwipes(_ sender:UISwipeGestureRecognizer) {
if sender.direction == .left {
self.tabBarController!.selectedIndex += 1
}
if sender.direction == .right {
self.tabBarController!.selectedIndex -= 1
}
}
Here is a slightly modified version of an earlier suggestion that is ready for Swift 5 and iOS 12.
The real advantages are:
it uses guard statements so you don't have to mess with unwrapping the tabBarController and the viewControllers in the handler
it doesn't require a hard-coded number of tabs — it just gets it from the viewControllers array.
I also cleaned it up a bit to make it a little less verbose. It has been tested with Swift 5 running on iOS 12 (and it should be fine on iOS 11.4, since that was my minimum deployment version that this was tested on).
Add these to viewDidLoad (or other appropriate location of your choosing):
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipeGesture))
swipeRight.direction = .right
self.view.addGestureRecognizer(swipeRight)
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipeGesture))
swipeLeft.direction = .left
self.view.addGestureRecognizer(swipeLeft)
Then add this as your handler for the events:
#objc func handleSwipeGesture(_ gesture: UISwipeGestureRecognizer) {
guard let tabBarController = tabBarController, let viewControllers = tabBarController.viewControllers else { return }
let tabs = viewControllers.count
if gesture.direction == .left {
if (tabBarController.selectedIndex) < tabs {
tabBarController.selectedIndex += 1
}
} else if gesture.direction == .right {
if (tabBarController.selectedIndex) > 0 {
tabBarController.selectedIndex -= 1
}
}
}
try using the Swift 4 selector syntax:
//below code write in view did appear()
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(swiped))
swipeRight.direction = UISwipeGestureRecognizerDirection.right
self.view.addGestureRecognizer(swipeRight)
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(swiped))
swipeLeft.direction = UISwipeGestureRecognizerDirection.left
self.view.addGestureRecognizer(swipeLeft)
// below code create swipe gestures function
// MARK: - swiped
#objc func swiped(_ gesture: UISwipeGestureRecognizer) {
if gesture.direction == .left {
if (self.tabBarController?.selectedIndex)! < 2
{ // set here your total tabs
self.tabBarController?.selectedIndex += 1
}
} else if gesture.direction == .right {
if (self.tabBarController?.selectedIndex)! > 0 {
self.tabBarController?.selectedIndex -= 1
}
}
}
Try using the Swift 3 selector syntax:
let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:))
like so
override func viewDidLoad() {
super.viewDidLoad()
nextButton.layer.cornerRadius = 7
let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:))
//leftSwipe.direction = .right
view.addGestureRecognizer(leftSwipe)
}
func handleSwipes(_ sender: UISwipeGestureRecognizer) {
if (sender.direction == .left) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "favourireviewcontroller") as! UIViewController
self.present(vc, animated: true, completion: nil)
}
if (sender.direction == .right) {
}
}
Swift 3 introduced this feature to enable the compiler to check whether the function you're specifying actually exists. It is therefore much saver than the concepts before.
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.
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.