Using Gesture Recognizers - ios

Before you respond, keep in mind I am very new to swift (last time I coded was 2 years ago and that was OBJC), and I seem to be having an error. Here's a sample of my code:
init(sourceView:UIView, menuItems:Array<String>) {
originView = sourceView
sideBarTableViewController.tableData = menuItems
animator = UIDynamicAnimator(referenceView: originView)
let showGestureRecognizer:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "handleSwipe:")
showGestureRecognizer.direction = UISwipeGestureRecognizerDirection.Right
originView.addGestureRecognizer(showGestureRecognizer)
let hideGestureRecognizer:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "handleSwipe:")
}
I seem to be having errors with the UISwipeGestureRecognizer target. I can't set it to self, because it gives me this error: 'self' used before super.init call.
Any help is very much appreciated.

I don't think it's linked to the gesture recognisers. Try to modify your Init() method, by adding at the beginning (inside):
super.init()
This will initialize the object with the parameters of the class you are overriding. Also it will probably ask you to add override before your init method.

Related

Using "self" in UIGestureRecognizer

Sorry for stupid question, I'm newbie. Whole day I read a lot of answers and articles about using of self. But I still can't understand, why app crashes, if I change target from self to faceView in UITapRecognizer. What self refers to here? Is there any place in XCode to find it out? If you can advise any articles to read about this type of usage of self, it would be great too. Just in case, here is GitHub link to this project, if it's needed. Great thanks for help.
class FaceViewController: UIViewController {
#IBOutlet weak var faceView: FaceView!{
didSet {
let handlerPinch = #selector(FaceView.changeScale(byReactingOn:))
let pichRecognizer = UIPinchGestureRecognizer(target: faceView, action: handlerPinch)
faceView.addGestureRecognizer(pichRecognizer)
let handlerTap = #selector(openEyes(byReactingOn:))
let tapRecognizer = UITapGestureRecognizer(target: self, action: handlerTap)
faceView.addGestureRecognizer(tapRecognizer)

pick up all UIGestureRecognizer events in callback?

Is there a way to pickup all UIGestureRecognizer events in one method? (besides via directing all their selectors to the same method).
So for example:
// Add Gesture Recogniser (Long Press)
let longPressGR = UILongPressGestureRecognizer(target: self, action: #selector(GcMapView.longPressAction(_:)))
longPressGR.minimumPressDuration = 1
self.addGestureRecognizer(longPressGR)
// Add Gesture Recogniser (Pan)
let mapDragRecognizer = UIPanGestureRecognizer(target: self, action: #selector(GcMapView.panAction(_:)))
mapDragRecognizer.delegate = self
self.addGestureRecognizer(mapDragRecognizer)
// Add Gesture Recogniser (Pinch)
let pinchGestureRecogniser = UIPanGestureRecognizer(target: self, action: #selector(GcMapView.pinchAction(_:)))
pinchGestureRecogniser.delegate = self
self.addGestureRecognizer(pinchGestureRecogniser)
// SOME METHOD NOW TO PICKUP ALL EVENTS
func PICKUPALLEVENTS (sender:UIGestureRecognizer) {
print("(String(gestureRecognizer.dynamicType) - \(gestureRecognizer.state.hashValue) ")
}
No, I don't think there is any way to do that.
Have you tried adding a UIGestureRecognizer to your target, then checking the dynamic type of the UIGestureRecognizer in the selector called?

Detecting Swipe Gestures on TableView

Is there a way to detect up & down swipes on TableView in a ViewController?
in ViewDidLoad();
let upSwipe = UISwipeGestureRecognizer(target: self, action: Selector("swipeUp:"))
let downSwipe = UISwipeGestureRecognizer(target: self, action: Selector("swipeDown:"))
upSwipe.direction = .Up
downSwipe.direction = .Down
tableView.addGestureRecognizer(upSwipe)
tableView.addGestureRecognizer(downSwipe)
Unfortunately, this doesn't work.
func swipeDown() {
print("A")
}
nothing returned.
What is the appropriate way to detect gestures on TableView (not cell/as a whole)?
Implement UIScrollViewDelegate
Define scrollViewDidScroll(_:)
Set your tableview's scrollview's delegate to whatever object you've got the above defined in.

Pan gesture recognizer selector won't work

In a SpriteKit game, I am trying to create a UIPanGestureRecognizer without using Interface Builder, so in the didMoveToView method of my SKScene, I wrote this :
let panGestureRecogniser = UIPanGestureRecognizer(target: view, action: "didPan:")
view.addGestureRecognizer(panGestureRecogniser)
Still in my SKScene class, I wrote this function :
func didPan(sender:UIGestureRecognizer) {
println("Panned")
}
My issue is that when I run my app, and when I pan on the screen, this error in thrown :
2015-05-12 19:28:01.955 Game[7342:2394353] -[SKView didPan:]: unrecognized selector sent to instance 0x154520690
I don't understand what's wrong, I have tried to move the function in both my view controller and app delegate, but it doesn't seem to make any difference...
Change
let panGestureRecogniser = UIPanGestureRecognizer(target: view, action: "didPan:")
To
let panGestureRecogniser = UIPanGestureRecognizer(target: self, action: "didPan:")

Function is not being called using UISwipeGestureRecognizer

Exactly what the title implies. How do the gesture recognizers work, specifically UIGestureRecognizer. Here is a small snippet of my code
var keyboardDismiser: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "gestureRecognizer:")
keyboardDismiser.direction = .Right | .Left
noteView.addGestureRecognizer(keyboardDismiser)
and
func gestureRecognizer(sender: UISwipeGestureRecognizer!) {
println("swipe")
self.view.endEditing()
}
My goal is to dismiss the keyboard when switching from view to view in a UIScrollView with 3 pages. What am I doing wrong? There isn't much documentation on this in Swift.
First you setting the recognizer on the note view. It will only be active on the note view.
In addition, you are not setting direction correctly. You are setting then changing the it's value. To set it to both right and left, you use the | operator. Also direction knows it a UISwipeGestureRecognizerDirection so you don't need specify that.
var keyboardDismiser = UISwipeGestureRecognizer(target: self, action: "gestureRecognizer:")
keyboardDismiser.direction = .Right | .Left
self.view.addGestureRecognizer(keyboardDismiser)
Finally, I would use endEditing() instead of resignFirstResponder().
func gestureRecognizer(sender: UISwipeGestureRecognizer!) {
println("swipe")
self.view.endEditing(true)
}
Hope that helps.
I believe that selectors in Swift do not need the : at the end; they're just a string with the name of the function: gestureRecognizer. So this is what you should have:
var keyboardDismiser = UISwipeGestureRecognizer(target: self, action: "gestureRecognizer")
Relevant question here.

Resources